oas_core 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.
- checksums.yaml +4 -4
- data/lib/oas_core/builders/content_builder.rb +8 -1
- data/lib/oas_core/builders/operation_builder.rb +1 -1
- data/lib/oas_core/builders/parameters_builder.rb +2 -6
- data/lib/oas_core/builders/request_body_builder.rb +1 -2
- data/lib/oas_core/json_schema_generator.rb +20 -1
- data/lib/oas_core/oas_route.rb +1 -1
- data/lib/oas_core/version.rb +1 -1
- data/lib/oas_core/yard/oas_core_factory.rb +7 -5
- data/lib/oas_core/yard/request_body_tag.rb +3 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 342155f509e56cc99810dd3883b97d5d274546e2592108c937c746d47daec610
|
4
|
+
data.tar.gz: c4dd49a6427f7e9189b626359af59c245828073a94bda3a04c5870493620ac5f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '069f0e27323893c9195f244ed21739f8b4fe04e9a9e424aa9f3fe6b4b4f2cd45ee2bfb16882eb4196f2dcbdef8dca472a2979608c89f9a2ecadc5d987cc57d73'
|
7
|
+
data.tar.gz: 929d2ce828e2b9ae88473d6401d8b2f8d7c1199b3908ede351588c83b620c10f097012c19eb11a2dc076b65130e09def523c30a567d06423ccbd764bcc0a454b
|
@@ -7,6 +7,7 @@ module OasCore
|
|
7
7
|
@context = context || :incoming
|
8
8
|
@specification = specification
|
9
9
|
@media_type = Spec::MediaType.new(specification)
|
10
|
+
@content_type = 'application/json'
|
10
11
|
end
|
11
12
|
|
12
13
|
def with_schema(schema)
|
@@ -21,6 +22,12 @@ module OasCore
|
|
21
22
|
self
|
22
23
|
end
|
23
24
|
|
25
|
+
def with_content_type(content_type)
|
26
|
+
@content_type = content_type if content_type && !content_type.empty?
|
27
|
+
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
24
31
|
def with_examples_from_tags(tags)
|
25
32
|
@media_type.examples = @media_type.examples.merge(tags.each_with_object({}).with_index(1) do |(example, result), _index|
|
26
33
|
key = example.text.downcase.gsub(' ', '_')
|
@@ -36,7 +43,7 @@ module OasCore
|
|
36
43
|
|
37
44
|
def build
|
38
45
|
{
|
39
|
-
|
46
|
+
@content_type => @media_type
|
40
47
|
}
|
41
48
|
end
|
42
49
|
end
|
@@ -10,13 +10,9 @@ module OasCore
|
|
10
10
|
|
11
11
|
def from_oas_route(oas_route)
|
12
12
|
parameters_from_tags(tags: oas_route.tags(:parameter))
|
13
|
+
|
13
14
|
oas_route.path_params.try(:map) do |p|
|
14
|
-
unless @parameters.any?
|
15
|
-
param.name.to_s == p.to_s
|
16
|
-
end
|
17
|
-
@parameters << ParameterBuilder.new(@specification).from_path(oas_route.path,
|
18
|
-
p).build
|
19
|
-
end
|
15
|
+
@parameters << ParameterBuilder.new(@specification).from_path(oas_route.path, p).build unless @parameters.any? { |param| param.name.to_s == p.to_s }
|
20
16
|
end
|
21
17
|
|
22
18
|
self
|
@@ -19,8 +19,7 @@ module OasCore
|
|
19
19
|
|
20
20
|
def from_tags(tag:, examples_tags: [])
|
21
21
|
@request_body.description = tag.text
|
22
|
-
@request_body.content = ContentBuilder.new(@specification,
|
23
|
-
:incoming).with_schema(tag.schema).with_examples_from_tags(examples_tags).build
|
22
|
+
@request_body.content = ContentBuilder.new(@specification, :incoming).with_schema(tag.schema).with_examples_from_tags(examples_tags).with_content_type(tag.content_type).build
|
24
23
|
@request_body.required = tag.required
|
25
24
|
|
26
25
|
self
|
@@ -6,6 +6,16 @@ module OasCore
|
|
6
6
|
# The JsonSchemaGenerator module provides methods to transform string representations
|
7
7
|
# of data types into JSON schema formats.
|
8
8
|
module JsonSchemaGenerator
|
9
|
+
@custom_type_parsers = {}
|
10
|
+
|
11
|
+
# Registers a custom type parser.
|
12
|
+
#
|
13
|
+
# @param type_matcher [Proc] A proc that matches the type string.
|
14
|
+
# @param parser [Proc] A proc that processes the type string.
|
15
|
+
def self.register_type_parser(type_matcher, parser)
|
16
|
+
@custom_type_parsers[type_matcher] = parser
|
17
|
+
end
|
18
|
+
|
9
19
|
# Processes a string representing a data type and converts it into a JSON schema.
|
10
20
|
#
|
11
21
|
# @param str [String] The string representation of a data type.
|
@@ -22,6 +32,7 @@ module OasCore
|
|
22
32
|
#
|
23
33
|
# @param str [String] The string representation of a data type.
|
24
34
|
# @return [Hash] A hash containing the type, whether it's required, and any additional properties.
|
35
|
+
# Registry for custom type parsers
|
25
36
|
def self.parse_type(str)
|
26
37
|
required = str.start_with?('!')
|
27
38
|
type = str.sub(/^!/, '').strip
|
@@ -32,7 +43,12 @@ module OasCore
|
|
32
43
|
when /^Array<(.+)>$/i
|
33
44
|
{ type: :array, required:, items: parse_type(::Regexp.last_match(1)) }
|
34
45
|
else
|
35
|
-
|
46
|
+
custom_parser = @custom_type_parsers.find { |matcher, _| matcher.call(type) }
|
47
|
+
if custom_parser
|
48
|
+
custom_parser.last.call(type, required)
|
49
|
+
else
|
50
|
+
{ type: type.downcase.to_sym, required: }
|
51
|
+
end
|
36
52
|
end
|
37
53
|
end
|
38
54
|
|
@@ -116,6 +132,7 @@ module OasCore
|
|
116
132
|
#
|
117
133
|
# @param type [Symbol, String] The Ruby data type.
|
118
134
|
# @return [Hash, String] The JSON schema type or a hash with additional format information.
|
135
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
119
136
|
def self.ruby_type_to_json_schema_type(type)
|
120
137
|
case type.to_s.downcase
|
121
138
|
when 'string' then { type: 'string' }
|
@@ -127,8 +144,10 @@ module OasCore
|
|
127
144
|
when 'nil' then { type: 'null' }
|
128
145
|
when 'date' then { type: 'string', format: 'date' }
|
129
146
|
when 'datetime' then { type: 'string', format: 'date-time' }
|
147
|
+
when 'file' then { type: 'string', format: 'binary' }
|
130
148
|
else type.to_s.downcase
|
131
149
|
end
|
132
150
|
end
|
151
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
133
152
|
end
|
134
153
|
end
|
data/lib/oas_core/oas_route.rb
CHANGED
data/lib/oas_core/version.rb
CHANGED
@@ -8,8 +8,8 @@ module OasCore
|
|
8
8
|
# @param text [String] The tag text to parse.
|
9
9
|
# @return [RequestBodyTag] The parsed request body tag object.
|
10
10
|
def parse_tag_with_request_body(tag_name, text)
|
11
|
-
description, klass, schema, required = extract_description_and_schema(text.squish)
|
12
|
-
RequestBodyTag.new(tag_name, description, klass, schema:, required:)
|
11
|
+
description, klass, schema, required, content_type = extract_description_and_schema(text.squish)
|
12
|
+
RequestBodyTag.new(tag_name, description, klass, schema:, required:, content_type:)
|
13
13
|
end
|
14
14
|
|
15
15
|
# Parses a tag that represents a request body example.
|
@@ -69,11 +69,13 @@ module OasCore
|
|
69
69
|
|
70
70
|
# Specific method to extract description and schema for request body tags.
|
71
71
|
# @param text [String] The text to parse.
|
72
|
-
# @return [Array] An array containing the description, class, schema, and
|
72
|
+
# @return [Array] An array containing the description, class, schema, required flag and content type.
|
73
73
|
def extract_description_and_schema(text)
|
74
74
|
description, type, = extract_description_type_and_content(text)
|
75
|
+
description, content_type = extract_text_and_parentheses_content(description)
|
76
|
+
|
75
77
|
klass, schema, required = type_text_to_schema(type)
|
76
|
-
[description, klass, schema, required]
|
78
|
+
[description, klass, schema, required, content_type]
|
77
79
|
end
|
78
80
|
|
79
81
|
# Specific method to extract name, location, and schema for parameters.
|
@@ -122,7 +124,7 @@ module OasCore
|
|
122
124
|
# @param input [String] The input text to parse.
|
123
125
|
# @return [Array] An array containing the name and location.
|
124
126
|
def extract_text_and_parentheses_content(input)
|
125
|
-
return unless input =~ /^(.+?)\(([^)]+)\)/
|
127
|
+
return input unless input =~ /^(.+?)\(([^)]+)\)/
|
126
128
|
|
127
129
|
text = ::Regexp.last_match(1).strip
|
128
130
|
parenthesis_content = ::Regexp.last_match(2).strip
|
@@ -3,14 +3,15 @@
|
|
3
3
|
module OasCore
|
4
4
|
module YARD
|
5
5
|
class RequestBodyTag < ::YARD::Tags::Tag
|
6
|
-
attr_accessor :klass, :schema, :required
|
6
|
+
attr_accessor :klass, :schema, :required, :content_type
|
7
7
|
|
8
|
-
def initialize(tag_name, text, klass, schema: {}, required: false)
|
8
|
+
def initialize(tag_name, text, klass, schema: {}, required: false, content_type: 'application/json')
|
9
9
|
# initialize(tag_name, text, types = nil, name = nil)
|
10
10
|
super(tag_name, text, nil, nil)
|
11
11
|
@klass = klass
|
12
12
|
@schema = schema
|
13
13
|
@required = required
|
14
|
+
@content_type = content_type
|
14
15
|
end
|
15
16
|
end
|
16
17
|
end
|