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
@@ -5,6 +5,8 @@ module Jsapi
5
5
  module OpenAPI
6
6
  # Represents an \OpenAPI object.
7
7
  class Root < Base
8
+ include Extensions
9
+
8
10
  ##
9
11
  # :attr: callbacks
10
12
  # The reusable Callback objects. Applies to \OpenAPI 3.0 and higher.
@@ -29,6 +31,11 @@ module Jsapi
29
31
  # The optional ExternalDocumentation object.
30
32
  attribute :external_docs, ExternalDocumentation
31
33
 
34
+ ##
35
+ # :attr: headers
36
+ # The reusable Header objects. Applies to \OpenAPI 3.0 and higher.
37
+ attribute :headers, { String => Header }
38
+
32
39
  ##
33
40
  # :attr: host
34
41
  # The host serving the API. Applies to \OpenAPI 2.0.
@@ -95,38 +102,43 @@ module Jsapi
95
102
  value.to_openapi(version)
96
103
  end
97
104
 
98
- if version.major == 2
99
- uri = servers&.first&.then { |server| URI(server.url) }
100
- {
101
- swagger: '2.0',
102
- info: info&.to_openapi,
103
- host: host || uri&.hostname,
104
- basePath: base_path || uri&.path,
105
- schemes: schemes || uri&.scheme&.then { |scheme| [scheme] },
106
- consumes: consumed_mime_types,
107
- produces: produced_mime_types,
108
- securityDefinitions: security_schemes,
109
- security: security_requirements&.map(&:to_openapi),
110
- tags: tags&.map(&:to_openapi),
111
- externalDocs: external_docs&.to_openapi
112
- }
113
- else
114
- {
115
- openapi: version.minor.zero? ? '3.0.3' : '3.1.0',
116
- info: info&.to_openapi,
117
- servers: servers&.map(&:to_openapi),
118
- components: {
119
- callbacks: callbacks&.transform_values do |callback|
120
- callback.to_openapi(version, definitions)
121
- end,
122
- links: links&.transform_values(&:to_openapi),
123
- securitySchemes: security_schemes
124
- }.compact.presence,
125
- security: security_requirements&.map(&:to_openapi),
126
- tags: tags&.map(&:to_openapi),
127
- externalDocs: external_docs&.to_openapi
128
- }
129
- end.compact
105
+ with_openapi_extensions(
106
+ if version.major == 2
107
+ uri = servers&.first&.then { |server| URI(server.url) }
108
+ {
109
+ swagger: '2.0',
110
+ info: info&.to_openapi,
111
+ host: host || uri&.hostname,
112
+ basePath: base_path || uri&.path,
113
+ schemes: schemes || uri&.scheme&.then { |scheme| [scheme] },
114
+ consumes: consumed_mime_types,
115
+ produces: produced_mime_types,
116
+ securityDefinitions: security_schemes,
117
+ security: security_requirements&.map(&:to_openapi),
118
+ tags: tags&.map(&:to_openapi),
119
+ externalDocs: external_docs&.to_openapi
120
+ }
121
+ else
122
+ {
123
+ openapi: version.minor.zero? ? '3.0.3' : '3.1.0',
124
+ info: info&.to_openapi,
125
+ servers: servers&.map(&:to_openapi),
126
+ components: {
127
+ callbacks: callbacks&.transform_values do |callback|
128
+ callback.to_openapi(version, definitions)
129
+ end,
130
+ headers: headers&.transform_values do |header|
131
+ header.to_openapi(version)
132
+ end,
133
+ links: links&.transform_values(&:to_openapi),
134
+ securitySchemes: security_schemes
135
+ }.compact.presence,
136
+ security: security_requirements&.map(&:to_openapi),
137
+ tags: tags&.map(&:to_openapi),
138
+ externalDocs: external_docs&.to_openapi
139
+ }
140
+ end
141
+ )
130
142
  end
131
143
  end
132
144
  end
@@ -17,7 +17,7 @@ module Jsapi
17
17
  # The schemes.
18
18
  attribute :schemes, { String => Scheme }
19
19
 
20
- # Returns a hash representing the security requirement object.
20
+ # Returns a hash representing the \OpenAPI security requirement object.
21
21
  def to_openapi(*)
22
22
  schemes&.transform_values(&:scopes) || {}
23
23
  end
@@ -6,6 +6,8 @@ module Jsapi
6
6
  module SecurityScheme
7
7
  # Represents a security scheme based on an API key.
8
8
  class APIKey < Base
9
+ include Extensions
10
+
9
11
  ##
10
12
  # :attr: in
11
13
  # The location of the API key. Possible values are:
@@ -22,14 +24,14 @@ module Jsapi
22
24
  # API key is sent by.
23
25
  attribute :name, String
24
26
 
25
- # Returns a hash representing the security scheme object.
27
+ # Returns a hash representing the \OpenAPI security scheme object.
26
28
  def to_openapi(*)
27
- {
29
+ with_openapi_extensions(
28
30
  type: 'apiKey',
29
31
  name: name,
30
32
  in: self.in,
31
33
  description: description
32
- }.compact
34
+ )
33
35
  end
34
36
  end
35
37
  end
@@ -7,21 +7,26 @@ module Jsapi
7
7
  module HTTP
8
8
  # Represents a security scheme based on \HTTP basic authentication.
9
9
  class Basic < Base
10
- # Returns a hash representing the security scheme object.
10
+ include Extensions
11
+
12
+ # Returns a hash representing the \OpenAPI security scheme object.
11
13
  def to_openapi(version)
12
14
  version = OpenAPI::Version.from(version)
13
- if version.major == 2
14
- {
15
- type: 'basic',
16
- description: description
17
- }
18
- else
19
- {
20
- type: 'http',
21
- scheme: 'basic',
22
- description: description
23
- }
24
- end.compact
15
+
16
+ with_openapi_extensions(
17
+ if version.major == 2
18
+ {
19
+ type: 'basic',
20
+ description: description
21
+ }
22
+ else
23
+ {
24
+ type: 'http',
25
+ scheme: 'basic',
26
+ description: description
27
+ }
28
+ end
29
+ )
25
30
  end
26
31
  end
27
32
  end
@@ -11,23 +11,25 @@ module Jsapi
11
11
  # Thus, a security scheme of this class is skipped when generating
12
12
  # an \OpenAPI 2.0 document.
13
13
  class Bearer < Base
14
+ include Extensions
15
+
14
16
  ##
15
17
  # :attr: bearer_format
16
18
  # The optional format of the bearer token.
17
19
  attribute :bearer_format, String
18
20
 
19
- # Returns a hash representing the security scheme object, or
21
+ # Returns a hash representing the \OpenAPI security scheme object, or
20
22
  # +nil+ if <code>version.major</code> is 2.
21
23
  def to_openapi(version)
22
24
  version = OpenAPI::Version.from(version)
23
25
  return if version.major == 2
24
26
 
25
- {
27
+ with_openapi_extensions(
26
28
  type: 'http',
27
29
  scheme: 'bearer',
28
30
  bearerFormat: bearer_format,
29
31
  description: description
30
- }.compact
32
+ )
31
33
  end
32
34
  end
33
35
  end
@@ -12,22 +12,24 @@ module Jsapi
12
12
  # a security scheme of this class is skipped when generating an
13
13
  # \OpenAPI 2.0 document.
14
14
  class Other < Base
15
+ include Extensions
16
+
15
17
  ##
16
18
  # :attr: scheme
17
19
  # The mandatory \HTTP authentication scheme.
18
20
  attribute :scheme, String
19
21
 
20
- # Returns a hash representing the security scheme object, or +nil+
22
+ # Returns a hash representing the \OpenAPI security scheme object, or +nil+
21
23
  # if <code>version.major</code> is 2.
22
24
  def to_openapi(version)
23
25
  version = OpenAPI::Version.from(version)
24
26
  return if version.major == 2
25
27
 
26
- {
28
+ with_openapi_extensions(
27
29
  type: 'http',
28
30
  scheme: scheme,
29
31
  description: description
30
- }.compact
32
+ )
31
33
  end
32
34
  end
33
35
  end
@@ -6,6 +6,8 @@ module Jsapi
6
6
  module SecurityScheme
7
7
  # Represents a security scheme based on \OAuth2.
8
8
  class OAuth2 < Base
9
+ include Extensions
10
+
9
11
  ##
10
12
  # :attr: oauth_flows
11
13
  # The hash containing the OAuth flows. Possible keys are:
@@ -19,13 +21,11 @@ module Jsapi
19
21
  attribute :oauth_flows, { String => OAuthFlow },
20
22
  keys: %w[authorization_code client_credentials implicit password]
21
23
 
22
- # Returns a hash representing the security scheme object.
24
+ # Returns a hash representing the \OpenAPI security scheme object.
23
25
  def to_openapi(version)
24
26
  version = Version.from(version)
25
- {
26
- type: 'oauth2',
27
- description: description
28
- }.tap do |h|
27
+
28
+ with_openapi_extensions(type: 'oauth2', description: description).tap do |h|
29
29
  if oauth_flows&.any?
30
30
  if version.major == 2
31
31
  key, oauth_flow = oauth_flows.first
@@ -37,7 +37,7 @@ module Jsapi
37
37
  end
38
38
  end
39
39
  end
40
- end.compact
40
+ end
41
41
  end
42
42
  end
43
43
  end
@@ -9,21 +9,23 @@ module Jsapi
9
9
  # OpenID Connect was introduced with \OpenAPI 3.0. Thus, a security scheme of
10
10
  # this class is skipped when generating an \OpenAPI 2.0 document.
11
11
  class OpenIDConnect < Base
12
+ include Extensions
13
+
12
14
  ##
13
15
  # :attr: open_id_connect_url
14
16
  attribute :open_id_connect_url, String
15
17
 
16
- # Returns a hash representing the security scheme object, or +nil+
18
+ # Returns a hash representing the \OpenAPI security scheme object, or +nil+
17
19
  # if <code>version.major</code> is 2.
18
20
  def to_openapi(version)
19
21
  version = Version.from(version)
20
22
  return if version.major == 2
21
23
 
22
- {
24
+ with_openapi_extensions(
23
25
  type: 'openIdConnect',
24
26
  openIdConnectUrl: open_id_connect_url,
25
27
  description: description
26
- }.compact
28
+ )
27
29
  end
28
30
  end
29
31
  end
@@ -5,6 +5,8 @@ module Jsapi
5
5
  module OpenAPI
6
6
  # Represents a server object.
7
7
  class Server < Base
8
+ include Extensions
9
+
8
10
  ##
9
11
  # :attr: description
10
12
  # The optional description of the server.
@@ -20,13 +22,13 @@ module Jsapi
20
22
  # The optional server variables.
21
23
  attribute :variables, { String => ServerVariable }
22
24
 
23
- # Returns a hash representing the server object.
25
+ # Returns a hash representing the \OpenAPI server object.
24
26
  def to_openapi(*)
25
- {
27
+ with_openapi_extensions(
26
28
  url: url,
27
29
  description: description,
28
30
  variables: variables&.transform_values(&:to_openapi)
29
- }.compact
31
+ )
30
32
  end
31
33
  end
32
34
  end
@@ -5,6 +5,8 @@ module Jsapi
5
5
  module OpenAPI
6
6
  # Represents a server variable object.
7
7
  class ServerVariable < Base
8
+ include Extensions
9
+
8
10
  ##
9
11
  # :attr: default
10
12
  # The default value of the server variable.
@@ -20,13 +22,13 @@ module Jsapi
20
22
  # The values of the server variable.
21
23
  attribute :enum, [String]
22
24
 
23
- # Returns a hash representing the server variable object.
25
+ # Returns a hash representing the \OpenAPI server variable object.
24
26
  def to_openapi(*)
25
- {
27
+ with_openapi_extensions(
26
28
  default: default,
27
29
  enum: enum.presence, # must not be empty
28
30
  description: description
29
- }.compact
31
+ )
30
32
  end
31
33
  end
32
34
  end
@@ -5,6 +5,8 @@ module Jsapi
5
5
  module OpenAPI
6
6
  # Represents a tag object.
7
7
  class Tag < Base
8
+ include Extensions
9
+
8
10
  ##
9
11
  # :attr: description
10
12
  # The description of the tag.
@@ -20,13 +22,13 @@ module Jsapi
20
22
  # The name of the tag.
21
23
  attribute :name, String
22
24
 
23
- # Returns a hash representing the tag object.
25
+ # Returns a hash representing the \OpenAPI tag object.
24
26
  def to_openapi(*)
25
- {
27
+ with_openapi_extensions(
26
28
  name: name,
27
29
  description: description,
28
30
  externalDocs: external_docs&.to_openapi
29
- }.compact
31
+ )
30
32
  end
31
33
  end
32
34
  end
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'openapi/extensions'
3
4
  require_relative 'openapi/version'
4
5
  require_relative 'openapi/callback'
5
6
  require_relative 'openapi/contact'
6
7
  require_relative 'openapi/license'
7
8
  require_relative 'openapi/info'
9
+ require_relative 'openapi/example'
8
10
  require_relative 'openapi/external_documentation'
11
+ require_relative 'openapi/header'
9
12
  require_relative 'openapi/oauth_flow'
10
13
  require_relative 'openapi/security_scheme'
11
14
  require_relative 'openapi/security_requirement'
@@ -3,6 +3,8 @@
3
3
  module Jsapi
4
4
  module Meta
5
5
  class Operation < Base
6
+ include OpenAPI::Extensions
7
+
6
8
  ##
7
9
  # :attr: callbacks
8
10
  # The optional callbacks. Applies to \OpenAPI 3.0 and higher.
@@ -133,7 +135,8 @@ module Jsapi
133
135
  # Returns a hash representing the \OpenAPI operation object.
134
136
  def to_openapi(version, definitions)
135
137
  version = OpenAPI::Version.from(version)
136
- {
138
+
139
+ with_openapi_extensions(
137
140
  operationId: name,
138
141
  tags: tags,
139
142
  summary: summary,
@@ -141,7 +144,7 @@ module Jsapi
141
144
  externalDocs: external_docs&.to_openapi,
142
145
  deprecated: deprecated?.presence,
143
146
  security: security_requirements&.map(&:to_openapi)
144
- }.tap do |hash|
147
+ ).tap do |hash|
145
148
  if version.major == 2
146
149
  hash[:consumes] = consumed_mime_types if consumed_mime_types
147
150
  hash[:produces] = produced_mime_types if produced_mime_types
@@ -170,7 +173,7 @@ module Jsapi
170
173
  callback.to_openapi(version, definitions)
171
174
  end
172
175
  end
173
- end.compact
176
+ end
174
177
  end
175
178
  end
176
179
  end
@@ -4,6 +4,8 @@ module Jsapi
4
4
  module Meta
5
5
  module Parameter
6
6
  class Model < Base
7
+ include OpenAPI::Extensions
8
+
7
9
  ##
8
10
  # :attr: deprecated
9
11
  # Specifies whether or not the parameter is deprecated.
@@ -17,7 +19,7 @@ module Jsapi
17
19
  ##
18
20
  # :attr_reader: examples
19
21
  # The examples.
20
- attribute :examples, { String => Example }, default_key: 'default'
22
+ attribute :examples, { String => OpenAPI::Example }, default_key: 'default'
21
23
 
22
24
  ##
23
25
  # :attr: in
@@ -87,32 +89,34 @@ module Jsapi
87
89
  else
88
90
  parameter_name = schema.array? ? "#{name}[]" : name
89
91
  [
90
- if version.major == 2
91
- {
92
- name: parameter_name,
93
- in: self.in,
94
- description: description,
95
- required: required?.presence,
96
- allowEmptyValue: allow_empty_value?.presence,
97
- collectionFormat: ('multi' if schema.array?)
98
- }.merge(schema.to_openapi(version))
99
- else
100
- {
101
- name: parameter_name,
102
- in: self.in,
103
- description: description,
104
- required: required?.presence,
105
- allowEmptyValue: allow_empty_value?.presence,
106
- deprecated: deprecated?.presence,
107
- schema: schema.to_openapi(version),
108
- examples: examples&.transform_values(&:to_openapi)
109
-
110
- # NOTE: collectionFormat is replaced by style and explode.
111
- # The default values for query parameters are:
112
- # - style: 'form'
113
- # - explode: true
114
- }
115
- end.compact
92
+ with_openapi_extensions(
93
+ if version.major == 2
94
+ {
95
+ name: parameter_name,
96
+ in: self.in,
97
+ description: description,
98
+ required: required?.presence,
99
+ allowEmptyValue: allow_empty_value?.presence,
100
+ collectionFormat: ('multi' if schema.array?)
101
+ }.merge(schema.to_openapi(version))
102
+ else
103
+ {
104
+ name: parameter_name,
105
+ in: self.in,
106
+ description: description,
107
+ required: required?.presence,
108
+ allowEmptyValue: allow_empty_value?.presence,
109
+ deprecated: deprecated?.presence,
110
+ schema: schema.to_openapi(version),
111
+ examples: examples&.transform_values(&:to_openapi)
112
+
113
+ # NOTE: collectionFormat is replaced by style and explode.
114
+ # The default values for query parameters are:
115
+ # - style: 'form'
116
+ # - explode: true
117
+ }
118
+ end
119
+ )
116
120
  ]
117
121
  end
118
122
  end
@@ -141,27 +145,29 @@ module Jsapi
141
145
  description = property_schema.description
142
146
  allow_empty_value = property.schema.existence <= Existence::ALLOW_EMPTY
143
147
  [
144
- if version.major == 2
145
- {
146
- name: parameter_name,
147
- in: self.in,
148
- description: description,
149
- required: required,
150
- allowEmptyValue: allow_empty_value.presence,
151
- collectionFormat: ('multi' if property_schema.array?)
152
- }.merge(property_schema.to_openapi(version))
153
- else
154
- {
155
- name: parameter_name,
156
- in: self.in,
157
- description: description,
158
- required: required,
159
- allowEmptyValue: allow_empty_value.presence,
160
- deprecated: deprecated,
161
- schema: property_schema.to_openapi(version).except(:deprecated),
162
- examples: examples&.transform_values(&:to_openapi)
163
- }
164
- end.compact
148
+ with_openapi_extensions(
149
+ if version.major == 2
150
+ {
151
+ name: parameter_name,
152
+ in: self.in,
153
+ description: description,
154
+ required: required,
155
+ allowEmptyValue: allow_empty_value.presence,
156
+ collectionFormat: ('multi' if property_schema.array?)
157
+ }.merge(property_schema.to_openapi(version))
158
+ else
159
+ {
160
+ name: parameter_name,
161
+ in: self.in,
162
+ description: description,
163
+ required: required,
164
+ allowEmptyValue: allow_empty_value.presence,
165
+ deprecated: deprecated,
166
+ schema: property_schema.to_openapi(version).except(:deprecated),
167
+ examples: examples&.transform_values(&:to_openapi)
168
+ }
169
+ end
170
+ )
165
171
  ]
166
172
  end
167
173
  end
@@ -4,6 +4,8 @@ module Jsapi
4
4
  module Meta
5
5
  module RequestBody
6
6
  class Model < Base
7
+ include OpenAPI::Extensions
8
+
7
9
  ##
8
10
  # :attr: description
9
11
  # The optional description of the request body.
@@ -12,7 +14,7 @@ module Jsapi
12
14
  ##
13
15
  # :attr_reader: examples
14
16
  # The optional examples.
15
- attribute :examples, { String => Example }, default_key: 'default'
17
+ attribute :examples, { String => OpenAPI::Example }, default_key: 'default'
16
18
 
17
19
  ##
18
20
  # :attr_reader: schema
@@ -44,12 +46,12 @@ module Jsapi
44
46
  in: 'body',
45
47
  description: description,
46
48
  required: required?
47
- }.merge(schema.to_openapi('2.0')).compact
49
+ }.merge(schema.to_openapi('2.0')).merge(openapi_extensions).compact
48
50
  end
49
51
 
50
52
  # Returns a hash representing the \OpenAPI 3.x request body object.
51
53
  def to_openapi(version)
52
- {
54
+ with_openapi_extensions(
53
55
  description: description,
54
56
  content: {
55
57
  'application/json' => {
@@ -58,7 +60,7 @@ module Jsapi
58
60
  }.compact
59
61
  },
60
62
  required: required?
61
- }.compact
63
+ )
62
64
  end
63
65
  end
64
66
  end