jsapi 0.2.0 → 0.4.0

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.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jsapi/controller/methods.rb +13 -3
  3. data/lib/jsapi/controller/parameters.rb +2 -5
  4. data/lib/jsapi/controller/response.rb +46 -24
  5. data/lib/jsapi/dsl/class_methods.rb +5 -0
  6. data/lib/jsapi/dsl/definitions.rb +7 -0
  7. data/lib/jsapi/json/object.rb +5 -8
  8. data/lib/jsapi/meta/definitions.rb +20 -11
  9. data/lib/jsapi/meta/openapi/callback/model.rb +1 -1
  10. data/lib/jsapi/meta/openapi/contact.rb +4 -6
  11. data/lib/jsapi/meta/openapi/example/model.rb +44 -0
  12. data/lib/jsapi/meta/openapi/example/reference.rb +16 -0
  13. data/lib/jsapi/meta/openapi/example.rb +21 -0
  14. data/lib/jsapi/meta/openapi/extensions.rb +31 -0
  15. data/lib/jsapi/meta/openapi/external_documentation.rb +4 -5
  16. data/lib/jsapi/meta/openapi/header/model.rb +64 -0
  17. data/lib/jsapi/meta/openapi/header/reference.rb +16 -0
  18. data/lib/jsapi/meta/openapi/header.rb +21 -0
  19. data/lib/jsapi/meta/openapi/info.rb +5 -3
  20. data/lib/jsapi/meta/openapi/license.rb +4 -5
  21. data/lib/jsapi/meta/openapi/link/model.rb +5 -3
  22. data/lib/jsapi/meta/openapi/oauth_flow.rb +5 -3
  23. data/lib/jsapi/meta/openapi/root.rb +44 -32
  24. data/lib/jsapi/meta/openapi/security_requirement.rb +1 -1
  25. data/lib/jsapi/meta/openapi/security_scheme/api_key.rb +5 -3
  26. data/lib/jsapi/meta/openapi/security_scheme/http/basic.rb +18 -13
  27. data/lib/jsapi/meta/openapi/security_scheme/http/bearer.rb +5 -3
  28. data/lib/jsapi/meta/openapi/security_scheme/http/other.rb +5 -3
  29. data/lib/jsapi/meta/openapi/security_scheme/oauth2.rb +6 -6
  30. data/lib/jsapi/meta/openapi/security_scheme/open_id_connect.rb +5 -3
  31. data/lib/jsapi/meta/openapi/server.rb +5 -3
  32. data/lib/jsapi/meta/openapi/server_variable.rb +5 -3
  33. data/lib/jsapi/meta/openapi/tag.rb +5 -3
  34. data/lib/jsapi/meta/openapi.rb +3 -0
  35. data/lib/jsapi/meta/operation.rb +6 -3
  36. data/lib/jsapi/meta/parameter/model.rb +54 -48
  37. data/lib/jsapi/meta/request_body/model.rb +6 -4
  38. data/lib/jsapi/meta/response/model.rb +37 -23
  39. data/lib/jsapi/meta/schema/additional_properties.rb +29 -0
  40. data/lib/jsapi/meta/schema/base.rb +34 -29
  41. data/lib/jsapi/meta/schema/object.rb +9 -1
  42. data/lib/jsapi/meta/schema.rb +2 -1
  43. data/lib/jsapi/meta.rb +0 -1
  44. data/lib/jsapi/version.rb +1 -1
  45. metadata +10 -5
  46. data/lib/jsapi/meta/example/model.rb +0 -43
  47. data/lib/jsapi/meta/example/reference.rb +0 -14
  48. data/lib/jsapi/meta/example.rb +0 -19
@@ -4,6 +4,8 @@ module Jsapi
4
4
  module Meta
5
5
  module Response
6
6
  class Model < Base
7
+ include OpenAPI::Extensions
8
+
7
9
  ##
8
10
  # :attr: description
9
11
  # The optional description of the response.
@@ -12,7 +14,12 @@ module Jsapi
12
14
  ##
13
15
  # :attr: examples
14
16
  # The optional examples.
15
- attribute :examples, { String => Example }, default_key: 'default'
17
+ attribute :examples, { String => OpenAPI::Example }, default_key: 'default'
18
+
19
+ ##
20
+ # :attr: headers
21
+ # The response headers.
22
+ attribute :headers, { String => OpenAPI::Header }
16
23
 
17
24
  ##
18
25
  # :attr: links
@@ -44,28 +51,35 @@ module Jsapi
44
51
  # Returns a hash representing the \OpenAPI response object.
45
52
  def to_openapi(version, definitions)
46
53
  version = OpenAPI::Version.from(version)
47
- if version.major == 2
48
- {
49
- description: description,
50
- schema: schema.to_openapi(version),
51
- examples: (
52
- if (example = examples&.values&.first).present?
53
- { 'application/json' => example.resolve(definitions).value }
54
- end
55
- )
56
- }
57
- else
58
- {
59
- description: description,
60
- content: {
61
- 'application/json' => {
62
- schema: schema.to_openapi(version),
63
- examples: examples&.transform_values(&:to_openapi)
64
- }.compact
65
- },
66
- links: links&.transform_values(&:to_openapi)
67
- }
68
- end.compact
54
+
55
+ with_openapi_extensions(
56
+ if version.major == 2
57
+ {
58
+ description: description,
59
+ schema: schema.to_openapi(version),
60
+ headers: headers&.transform_values do |header|
61
+ header.to_openapi(version) unless header.reference?
62
+ end&.compact,
63
+ examples: (
64
+ if (example = examples&.values&.first).present?
65
+ { 'application/json' => example.resolve(definitions).value }
66
+ end
67
+ )
68
+ }
69
+ else
70
+ {
71
+ description: description,
72
+ content: {
73
+ 'application/json' => {
74
+ schema: schema.to_openapi(version),
75
+ examples: examples&.transform_values(&:to_openapi)
76
+ }.compact
77
+ },
78
+ headers: headers&.transform_values { |header| header.to_openapi(version) },
79
+ links: links&.transform_values(&:to_openapi)
80
+ }
81
+ end
82
+ )
69
83
  end
70
84
  end
71
85
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jsapi
4
+ module Meta
5
+ module Schema
6
+ class AdditionalProperties < Meta::Base
7
+ #
8
+ # :attr: schema
9
+ # The Schema of additional properties.
10
+ attribute :schema, Schema, writer: false
11
+
12
+ ##
13
+ # :attr: source
14
+ # The method to read additional properties when serializing an object.
15
+ # The default method is +additional_properties+.
16
+ attribute :source, Symbol, default: :additional_properties
17
+
18
+ delegate_missing_to :schema
19
+
20
+ def initialize(keywords = {})
21
+ keywords = keywords.dup
22
+ super(keywords.extract!(:source))
23
+
24
+ @schema = Schema.new(keywords)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -4,6 +4,8 @@ module Jsapi
4
4
  module Meta
5
5
  module Schema
6
6
  class Base < Meta::Base
7
+ include OpenAPI::Extensions
8
+
7
9
  TYPES = %w[array boolean integer number object string].freeze # :nodoc:
8
10
 
9
11
  TYPES.each do |type|
@@ -96,37 +98,40 @@ module Jsapi
96
98
  # Returns a hash representing the \OpenAPI schema object.
97
99
  def to_openapi(version)
98
100
  version = OpenAPI::Version.from(version)
99
- if version.major == 2
100
- # OpenAPI 2.0
101
- {
102
- type: type,
103
- example: examples&.first
104
- }
105
- elsif version.minor.zero?
106
- # OpenAPI 3.0
107
- {
108
- type: type,
109
- nullable: nullable?.presence,
110
- examples: examples,
111
- deprecated: deprecated?.presence
112
- }
113
- else
114
- # OpenAPI 3.1
115
- {
116
- type: nullable? ? [type, 'null'] : type,
117
- examples: examples,
118
- deprecated: deprecated?.presence
119
- }
120
- end.tap do |hash|
121
- hash[:title] = title
122
- hash[:description] = description
123
- hash[:default] = default
124
- hash[:externalDocs] = external_docs&.to_openapi
125
101
 
126
- validations.each_value do |validation|
127
- hash.merge!(validation.to_openapi_validation(version))
102
+ with_openapi_extensions(
103
+ if version.major == 2
104
+ # OpenAPI 2.0
105
+ {
106
+ type: type,
107
+ example: examples&.first
108
+ }
109
+ elsif version.minor.zero?
110
+ # OpenAPI 3.0
111
+ {
112
+ type: type,
113
+ nullable: nullable?.presence,
114
+ examples: examples,
115
+ deprecated: deprecated?.presence
116
+ }
117
+ else
118
+ # OpenAPI 3.1
119
+ {
120
+ type: nullable? ? [type, 'null'] : type,
121
+ examples: examples,
122
+ deprecated: deprecated?.presence
123
+ }
124
+ end.tap do |hash|
125
+ hash[:title] = title
126
+ hash[:description] = description
127
+ hash[:default] = default
128
+ hash[:externalDocs] = external_docs&.to_openapi
129
+
130
+ validations.each_value do |validation|
131
+ hash.merge!(validation.to_openapi_validation(version))
132
+ end
128
133
  end
129
- end.compact
134
+ )
130
135
  end
131
136
 
132
137
  def type # :nodoc:
@@ -4,15 +4,21 @@ module Jsapi
4
4
  module Meta
5
5
  module Schema
6
6
  class Object < Base
7
+ ##
8
+ # :attr: additional_properties
9
+ # The AdditionalProperties.
10
+ attribute :additional_properties, AdditionalProperties
11
+
7
12
  ##
8
13
  # :attr: all_of_references
9
14
  attribute :all_of_references, [Reference], default: []
10
15
 
11
- alias :all_of= :all_of_references=
16
+ alias :all_of= :all_of_references= # :nodoc:
12
17
  alias :add_all_of :add_all_of_reference
13
18
 
14
19
  ##
15
20
  # :attr: discriminator
21
+ # The Discriminator.
16
22
  attribute :discriminator, Discriminator
17
23
 
18
24
  ##
@@ -47,6 +53,7 @@ module Jsapi
47
53
  super.merge(
48
54
  allOf: all_of_references.map(&:to_json_schema).presence,
49
55
  properties: properties.transform_values(&:to_json_schema),
56
+ additionalProperties: additional_properties&.to_json_schema,
50
57
  required: properties.values.select(&:required?).map(&:name)
51
58
  ).compact
52
59
  end
@@ -60,6 +67,7 @@ module Jsapi
60
67
  properties: properties.transform_values do |property|
61
68
  property.to_openapi(version)
62
69
  end,
70
+ additionalProperties: additional_properties&.to_openapi(version),
63
71
  required: properties.values.select(&:required?).map(&:name)
64
72
  ).compact
65
73
  end
@@ -4,13 +4,14 @@ require_relative 'schema/conversion'
4
4
  require_relative 'schema/boundary'
5
5
  require_relative 'schema/delegator'
6
6
  require_relative 'schema/reference'
7
- require_relative 'schema/discriminator'
8
7
  require_relative 'schema/base'
9
8
  require_relative 'schema/boolean'
10
9
  require_relative 'schema/array'
11
10
  require_relative 'schema/numeric'
12
11
  require_relative 'schema/integer'
13
12
  require_relative 'schema/number'
13
+ require_relative 'schema/additional_properties'
14
+ require_relative 'schema/discriminator'
14
15
  require_relative 'schema/object'
15
16
  require_relative 'schema/string'
16
17
  require_relative 'schema/validation'
data/lib/jsapi/meta.rb CHANGED
@@ -6,7 +6,6 @@ require_relative 'meta/attributes'
6
6
  require_relative 'meta/base'
7
7
  require_relative 'meta/base_reference'
8
8
  require_relative 'meta/openapi'
9
- require_relative 'meta/example'
10
9
  require_relative 'meta/existence'
11
10
  require_relative 'meta/property'
12
11
  require_relative 'meta/schema'
data/lib/jsapi/version.rb CHANGED
@@ -5,6 +5,6 @@ module Jsapi
5
5
  # NOTE: See https://bundler.io/guides/creating_gem.html
6
6
 
7
7
  # The current GEM version.
8
- VERSION = '0.2.0'
8
+ VERSION = '0.4.0'
9
9
  end
10
10
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Göller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-05 00:00:00.000000000 Z
11
+ date: 2024-08-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Jsapi can be used to read requests, produce responses and create OpenAPI
14
14
  documents
@@ -56,9 +56,6 @@ files:
56
56
  - lib/jsapi/meta/base.rb
57
57
  - lib/jsapi/meta/base_reference.rb
58
58
  - lib/jsapi/meta/definitions.rb
59
- - lib/jsapi/meta/example.rb
60
- - lib/jsapi/meta/example/model.rb
61
- - lib/jsapi/meta/example/reference.rb
62
59
  - lib/jsapi/meta/existence.rb
63
60
  - lib/jsapi/meta/invalid_argument_error.rb
64
61
  - lib/jsapi/meta/openapi.rb
@@ -66,7 +63,14 @@ files:
66
63
  - lib/jsapi/meta/openapi/callback/model.rb
67
64
  - lib/jsapi/meta/openapi/callback/reference.rb
68
65
  - lib/jsapi/meta/openapi/contact.rb
66
+ - lib/jsapi/meta/openapi/example.rb
67
+ - lib/jsapi/meta/openapi/example/model.rb
68
+ - lib/jsapi/meta/openapi/example/reference.rb
69
+ - lib/jsapi/meta/openapi/extensions.rb
69
70
  - lib/jsapi/meta/openapi/external_documentation.rb
71
+ - lib/jsapi/meta/openapi/header.rb
72
+ - lib/jsapi/meta/openapi/header/model.rb
73
+ - lib/jsapi/meta/openapi/header/reference.rb
70
74
  - lib/jsapi/meta/openapi/info.rb
71
75
  - lib/jsapi/meta/openapi/license.rb
72
76
  - lib/jsapi/meta/openapi/link.rb
@@ -102,6 +106,7 @@ files:
102
106
  - lib/jsapi/meta/response/model.rb
103
107
  - lib/jsapi/meta/response/reference.rb
104
108
  - lib/jsapi/meta/schema.rb
109
+ - lib/jsapi/meta/schema/additional_properties.rb
105
110
  - lib/jsapi/meta/schema/array.rb
106
111
  - lib/jsapi/meta/schema/base.rb
107
112
  - lib/jsapi/meta/schema/boolean.rb
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Jsapi
4
- module Meta
5
- module Example
6
- class Model < Base
7
- ##
8
- # :attr: description
9
- # The optional description of the example.
10
- attribute :description, String
11
-
12
- ##
13
- # :attr: external
14
- # If true, +value+ is interpreted as a URI pointing to an external sample value.
15
- attribute :external, values: [true, false]
16
-
17
- ##
18
- # :attr: summary
19
- # The optional summary of the example.
20
- attribute :summary, String
21
-
22
- ##
23
- # :attr: value
24
- # The sample value.
25
- attribute :value
26
-
27
- # Returns a hash representing the \OpenAPI example object.
28
- def to_openapi(*)
29
- {}.tap do |hash|
30
- hash[:summary] = summary if summary.present?
31
- hash[:description] = description if description.present?
32
-
33
- if external?
34
- hash[:external_value] = value
35
- else
36
- hash[:value] = value
37
- end
38
- end
39
- end
40
- end
41
- end
42
- end
43
- end
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Jsapi
4
- module Meta
5
- module Example
6
- class Reference < BaseReference
7
- # Returns a hash representing the \OpenAPI reference object.
8
- def to_openapi(*)
9
- { '$ref': "#/components/examples/#{ref}" }
10
- end
11
- end
12
- end
13
- end
14
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'example/model'
4
- require_relative 'example/reference'
5
-
6
- module Jsapi
7
- module Meta
8
- module Example
9
- class << self
10
- # Creates a new example model or reference.
11
- def new(keywords = {})
12
- return Reference.new(keywords) if keywords.key?(:ref)
13
-
14
- Model.new(keywords)
15
- end
16
- end
17
- end
18
- end
19
- end