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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '092cef0cfe920698acf4b55412febd5e0ac30f31ed14e0b77169847c6bcfe1bd'
4
- data.tar.gz: 6c2bfbbe7d7b513c7f1f2223a1064c716fd74780515e45d10e11e662ae6bc07d
3
+ metadata.gz: 342155f509e56cc99810dd3883b97d5d274546e2592108c937c746d47daec610
4
+ data.tar.gz: c4dd49a6427f7e9189b626359af59c245828073a94bda3a04c5870493620ac5f
5
5
  SHA512:
6
- metadata.gz: 568ea83f14c6e19523e2eb8aa70d3b8533f18200824c2e18cbe7e0c858c50f8a987110ece244f107c907d68f2324ae2d4473ae1bd6641122cdc3044c60130b05
7
- data.tar.gz: dc35e960540a2a532bb4374662c8b977ead04128425abced1ec22cce64090470fbc5adf8ae41d2d0dd95bbd34bb20ed603c8661faf51e06572fec30a32faaf01
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
- 'application/json': @media_type
46
+ @content_type => @media_type
40
47
  }
41
48
  end
42
49
  end
@@ -35,7 +35,7 @@ module OasCore
35
35
  end
36
36
 
37
37
  def extract_operation_id(oas_route:)
38
- "#{oas_route.method_name}#{oas_route.path.gsub('/', '_').gsub(/[{}]/, '')}"
38
+ "#{oas_route.verb}_#{oas_route.path.gsub('/', '_')}"
39
39
  end
40
40
 
41
41
  def extract_tags(oas_route:)
@@ -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? do |param|
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
- { type: type.downcase.to_sym, required: }
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
@@ -12,7 +12,7 @@ module OasCore
12
12
  end
13
13
 
14
14
  def path_params
15
- @path.to_s.scan(/:(\w+)/).flatten.reject! { |e| e == 'format' }
15
+ @path.to_s.scan(/\{(\w+)\}/).flatten
16
16
  end
17
17
 
18
18
  def tags(name = nil)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OasCore
4
- VERSION = '0.2.0'
4
+ VERSION = '0.4.0'
5
5
  end
@@ -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 required flag.
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oas_core
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
  - a-chacon