oapi 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e4421c069d115418bd789cf9da9d9e6d29286d7eaae99cf3bda9b5791bae4308
4
- data.tar.gz: 137785b55b2c6b8a7c3c05246ed1843472f039b790905d380b7e0b49725593a6
3
+ metadata.gz: 51585744c9a5a8787b4cbc8235ca2f613fdad0fc8d28aea14ad8242b2d39d107
4
+ data.tar.gz: b68db8b06768f24ead377f085cd5cb546aba3a9912ebe550229f3ca037621621
5
5
  SHA512:
6
- metadata.gz: e573b3a84d1577baea6a85e4f3624f18af8fe24e3431317913c6e12afc51b7f8d2fb702bfce2454b7b2c505261ba4f6b142b92abe3eb3636696d601036d6270d
7
- data.tar.gz: 20c09ec8fc131fd731fdbe6b03f9a11c81d2e96587152eb47f666ee21d40f704ddd6cdcb95c2c5b898d5854124bf55d025dd5edff8ce5ab9eb9ba2cbeb52e379
6
+ metadata.gz: 96f581933db469a307cfe53e22611a5821540d1b6fdadb77d9e81ab70deaf2097c748108c559105ca0bab6c6097f8d80baeb4d0f36f63ad50fb3709e7d06c0af
7
+ data.tar.gz: a2ea340668ebd92716f3434094ae809e5c8c25f1e07e0fe6fc329f225c88125c2d62cb35c378f388ae9e1ab755ed88adf86d5a084f080544eb2960298512f9e6
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- oapi (0.1.1)
4
+ oapi (0.1.3)
5
5
  zeitwerk (~> 2.6)
6
6
 
7
7
  GEM
@@ -43,20 +43,6 @@ class OAPI::OpenAPI::V30::Components < OAPI::Types::Object
43
43
  item :scheme, Scheme
44
44
  end
45
45
 
46
- class Links < OAPI::Types::Map
47
- class Link < OAPI::Types::Object
48
- property :operation_ref
49
- property :operation_id
50
- property :request_body
51
- property :description
52
-
53
- property :parameters # TODO: a custom type? https://spec.openapis.org/oas/v3.0.3#fixed-fields-16
54
- property :server, OAPI::OpenAPI::V30::Servers::Server
55
- end
56
-
57
- item :link, Link
58
- end
59
-
60
46
  property :schemas, Schemas
61
47
  property :responses, OAPI::OpenAPI::V30::Responses
62
48
  property :parameters, Parameters
@@ -64,6 +50,6 @@ class OAPI::OpenAPI::V30::Components < OAPI::Types::Object
64
50
  property :request_bodies, RequestBodies
65
51
  property :headers, OAPI::OpenAPI::V30::Headers
66
52
  property :security_schemes, SecuritySchemes
67
- property :links, Links
53
+ property :links, OAPI::OpenAPI::V30::Links
68
54
  property :callbacks, OAPI::OpenAPI::V30::Callbacks
69
55
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::OpenAPI::V30::Links < OAPI::Types::Map
4
+ class Link < OAPI::Types::Object
5
+ property :operation_ref
6
+ property :operation_id
7
+ property :request_body
8
+ property :description
9
+
10
+ property :parameters # TODO: a custom type? https://spec.openapis.org/oas/v3.0.3#fixed-fields-16
11
+ property :server, OAPI::OpenAPI::V30::Servers::Server
12
+ end
13
+
14
+ item :link, Link
15
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::OpenAPI::V30::Parsers::JSON
4
+ using OAPI::Monkey
5
+
6
+ class << self
7
+ def parse(input) = parse_object(input, OAPI::OpenAPI::V30::Definition)
8
+
9
+ def parse_type(input, type)
10
+ return input if type.nil?
11
+
12
+ return parse_array(input, type) if type <= OAPI::Types::Array
13
+
14
+ return parse_map(input, type) if type <= OAPI::Types::Map
15
+
16
+ return OAPI::Ref.new(input[:$ref]) if input[:$ref]
17
+
18
+ return type.new { input } if type <= OAPI::Schema
19
+
20
+ return parse_object(input, type) if type <= OAPI::Types::Object
21
+
22
+ raise ArgumentError, "unknown type #{type}"
23
+ end
24
+
25
+ def parse_object(input, klass)
26
+ klass.new.tap do |obj|
27
+ klass.properties.each do |name, type|
28
+ value = input[name.camelize]
29
+
30
+ obj.send(name, parse_type(value, type)) unless value.nil?
31
+ end
32
+ end
33
+ end
34
+
35
+ def parse_array(input, klass) = klass.new(input.map { parse_type(_1, klass.item_type) })
36
+ def parse_map(input, klass) = klass.new(input.transform_values { parse_type(_1, klass.item_type) })
37
+ end
38
+ end
@@ -6,6 +6,8 @@ class OAPI::OpenAPI::V30::Responses < OAPI::Types::Map
6
6
 
7
7
  property :content, OAPI::OpenAPI::V30::Content
8
8
  property :headers, OAPI::OpenAPI::V30::Headers
9
+
10
+ property :links, OAPI::OpenAPI::V30::Links
9
11
  end
10
12
 
11
13
  item :response, Response
@@ -5,9 +5,13 @@ class OAPI::OpenAPI::V30::Serializers::Ruby < OAPI::OpenAPI::V30::Serializers::S
5
5
 
6
6
  MAP_TEMPLATE = %(
7
7
  <% object.store.each do |key, value| -%>
8
- <%= object.class.item_name %>("<%= key %>") do
9
- <%= serialize_if_supported(value) %>
10
- end
8
+ <% if value.is_a?(OAPI::Ref) -%>
9
+ <%= object.class.item_name %>("<%= key %>", ref: <%= serialize(value) %>)
10
+ <% else -%>
11
+ <%= object.class.item_name %>("<%= key %>") do
12
+ <%= serialize_if_supported(value) %>
13
+ end
14
+ <% end -%>
11
15
  <% end -%>
12
16
  )
13
17
 
@@ -22,13 +26,13 @@ end
22
26
  OBJECT_TEMPLATE = %(
23
27
  <% object.properties.each do |prop, value| -%>
24
28
  <% if value.is_a?(OAPI::Ref) -%>
25
- <%= prop%>(ref: <%= serialize(value) %>)
29
+ <%= prop %>(ref: <%= serialize(value) %>)
26
30
  <% elsif supported?(value) -%>
27
31
  <%= prop %> do
28
32
  <%= serialize(value) %>
29
33
  end
30
34
  <% elsif !value.nil? -%>
31
- <%= prop %> <%= value.inspect %>
35
+ <%= prop %> <%= value_to_ruby(value) %>
32
36
  <% end -%>
33
37
  <% end -%>
34
38
  )
@@ -43,6 +47,13 @@ end
43
47
  def serialize(node) = OAPI::OpenAPI::V30::Serializers::Ruby.serialize(node)
44
48
  def supported?(node) = OAPI::OpenAPI::V30::Serializers::Ruby.supported?(node)
45
49
  def serialize_if_supported(node) = OAPI::OpenAPI::V30::Serializers::Ruby.serialize_if_supported(node)
50
+
51
+ def value_to_ruby(value)
52
+ return value.inspect if [Numeric, String, Array, TrueClass, FalseClass].any? { value.is_a?(_1) }
53
+ return "(#{value.inspect})" if value.is_a?(Hash)
54
+
55
+ raise ArgumentError, "value: #{value}"
56
+ end
46
57
  end
47
58
 
48
59
  def serialize_ref(ref) = ref.ref.inspect
data/lib/oapi/schema.rb CHANGED
@@ -6,13 +6,4 @@ class OAPI::Schema
6
6
  def initialize
7
7
  @schema = yield if block_given?
8
8
  end
9
-
10
- class << self
11
- def parse(json)
12
- ref = json[:$ref]
13
- return OAPI::Ref.new(ref) if ref
14
-
15
- new { json }
16
- end
17
- end
18
9
  end
@@ -19,7 +19,5 @@ class OAPI::Types::Array < OAPI::Types::Object
19
19
  @store << type.new(&block)
20
20
  end
21
21
  end
22
-
23
- def parse(json) = new(json.map { item_type.parse(_1) })
24
22
  end
25
23
  end
@@ -15,18 +15,15 @@ class OAPI::Types::Map < OAPI::Types::Object
15
15
  @item_name = name
16
16
  @item_type = type
17
17
 
18
- define_method(name) do |key, &block|
18
+ define_method(name) do |key, ref: nil, &block|
19
19
  raise ArgumentError, "'#{key}' already exists" if @store.include?(key.to_s)
20
20
 
21
- @store[key] = type.new(&block)
22
- end
23
- end
21
+ raise ArgumentError, "ref and block are mutual exclusive" if ref && block
24
22
 
25
- def parse(json)
26
- store = json.transform_values do |value|
27
- item_type ? item_type.parse(value) : value
23
+ return @store[key] = OAPI::Ref.new(ref) if ref
24
+
25
+ @store[key] = type.new(&block)
28
26
  end
29
- new(store)
30
27
  end
31
28
  end
32
29
  end
@@ -1,21 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class OAPI::Types::Object
4
- using OAPI::Monkey
5
-
6
4
  include OAPI::Properties
7
-
8
- class << self
9
- def parse(json)
10
- new.tap do |obj|
11
- (properties || []).each do |name, type|
12
- value = json[name.camelize]
13
- next if value.nil?
14
-
15
- value = type.parse(value) if value.is_a?(Hash) || (value.is_a?(Array) && type)
16
- obj.send(name, value)
17
- end
18
- end
19
- end
20
- end
21
5
  end
data/lib/oapi/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OAPI
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
5
5
  end
data/lib/oapi.rb CHANGED
@@ -34,7 +34,7 @@ module OAPI
34
34
  version = json[:openapi]
35
35
  raise UnsupportedSpecVersion, "unsupported version #{version}" unless version.start_with?("3.0")
36
36
 
37
- OAPI::OpenAPI::V30::Definition.parse(json)
37
+ OAPI::OpenAPI::V30::Parsers::JSON.parse(json)
38
38
  end
39
39
  end
40
40
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gleb Sinyavskiy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-06-24 00:00:00.000000000 Z
11
+ date: 2024-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zeitwerk
@@ -50,8 +50,10 @@ files:
50
50
  - lib/oapi/open_api/v30/external_docs.rb
51
51
  - lib/oapi/open_api/v30/headers.rb
52
52
  - lib/oapi/open_api/v30/info.rb
53
+ - lib/oapi/open_api/v30/links.rb
53
54
  - lib/oapi/open_api/v30/media_type.rb
54
55
  - lib/oapi/open_api/v30/parameters.rb
56
+ - lib/oapi/open_api/v30/parsers/json.rb
55
57
  - lib/oapi/open_api/v30/paths.rb
56
58
  - lib/oapi/open_api/v30/request_body.rb
57
59
  - lib/oapi/open_api/v30/responses.rb