openapi3_parser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +9 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +4 -0
  7. data/Gemfile +5 -0
  8. data/LICENCE +19 -0
  9. data/README.md +32 -0
  10. data/Rakefile +10 -0
  11. data/TODO.md +30 -0
  12. data/lib/openapi3_parser.rb +39 -0
  13. data/lib/openapi3_parser/context.rb +54 -0
  14. data/lib/openapi3_parser/document.rb +48 -0
  15. data/lib/openapi3_parser/error.rb +5 -0
  16. data/lib/openapi3_parser/fields/map.rb +83 -0
  17. data/lib/openapi3_parser/node.rb +115 -0
  18. data/lib/openapi3_parser/node/map.rb +24 -0
  19. data/lib/openapi3_parser/node/object.rb +28 -0
  20. data/lib/openapi3_parser/node_factories/array.rb +105 -0
  21. data/lib/openapi3_parser/node_factories/callback.rb +26 -0
  22. data/lib/openapi3_parser/node_factories/components.rb +83 -0
  23. data/lib/openapi3_parser/node_factories/contact.rb +24 -0
  24. data/lib/openapi3_parser/node_factories/discriminator.rb +31 -0
  25. data/lib/openapi3_parser/node_factories/encoding.rb +34 -0
  26. data/lib/openapi3_parser/node_factories/example.rb +25 -0
  27. data/lib/openapi3_parser/node_factories/external_documentation.rb +23 -0
  28. data/lib/openapi3_parser/node_factories/header.rb +36 -0
  29. data/lib/openapi3_parser/node_factories/info.rb +28 -0
  30. data/lib/openapi3_parser/node_factories/license.rb +22 -0
  31. data/lib/openapi3_parser/node_factories/link.rb +40 -0
  32. data/lib/openapi3_parser/node_factories/map.rb +110 -0
  33. data/lib/openapi3_parser/node_factories/media_type.rb +44 -0
  34. data/lib/openapi3_parser/node_factories/oauth_flow.rb +24 -0
  35. data/lib/openapi3_parser/node_factories/oauth_flows.rb +31 -0
  36. data/lib/openapi3_parser/node_factories/openapi.rb +50 -0
  37. data/lib/openapi3_parser/node_factories/operation.rb +76 -0
  38. data/lib/openapi3_parser/node_factories/parameter.rb +43 -0
  39. data/lib/openapi3_parser/node_factories/parameter/parameter_like.rb +37 -0
  40. data/lib/openapi3_parser/node_factories/path_item.rb +75 -0
  41. data/lib/openapi3_parser/node_factories/paths.rb +32 -0
  42. data/lib/openapi3_parser/node_factories/reference.rb +33 -0
  43. data/lib/openapi3_parser/node_factories/request_body.rb +31 -0
  44. data/lib/openapi3_parser/node_factories/response.rb +45 -0
  45. data/lib/openapi3_parser/node_factories/responses.rb +32 -0
  46. data/lib/openapi3_parser/node_factories/schema.rb +106 -0
  47. data/lib/openapi3_parser/node_factories/security_requirement.rb +25 -0
  48. data/lib/openapi3_parser/node_factories/security_scheme.rb +35 -0
  49. data/lib/openapi3_parser/node_factories/server.rb +32 -0
  50. data/lib/openapi3_parser/node_factories/server_variable.rb +28 -0
  51. data/lib/openapi3_parser/node_factories/tag.rb +24 -0
  52. data/lib/openapi3_parser/node_factories/xml.rb +25 -0
  53. data/lib/openapi3_parser/node_factory.rb +126 -0
  54. data/lib/openapi3_parser/node_factory/field_config.rb +88 -0
  55. data/lib/openapi3_parser/node_factory/map.rb +39 -0
  56. data/lib/openapi3_parser/node_factory/object.rb +80 -0
  57. data/lib/openapi3_parser/node_factory/object/node_builder.rb +85 -0
  58. data/lib/openapi3_parser/node_factory/object/validator.rb +102 -0
  59. data/lib/openapi3_parser/node_factory/optional_reference.rb +23 -0
  60. data/lib/openapi3_parser/nodes/array.rb +26 -0
  61. data/lib/openapi3_parser/nodes/callback.rb +11 -0
  62. data/lib/openapi3_parser/nodes/components.rb +47 -0
  63. data/lib/openapi3_parser/nodes/contact.rb +23 -0
  64. data/lib/openapi3_parser/nodes/discriminator.rb +19 -0
  65. data/lib/openapi3_parser/nodes/encoding.rb +31 -0
  66. data/lib/openapi3_parser/nodes/example.rb +27 -0
  67. data/lib/openapi3_parser/nodes/external_documentation.rb +19 -0
  68. data/lib/openapi3_parser/nodes/header.rb +13 -0
  69. data/lib/openapi3_parser/nodes/info.rb +35 -0
  70. data/lib/openapi3_parser/nodes/license.rb +19 -0
  71. data/lib/openapi3_parser/nodes/link.rb +35 -0
  72. data/lib/openapi3_parser/nodes/map.rb +11 -0
  73. data/lib/openapi3_parser/nodes/media_type.rb +27 -0
  74. data/lib/openapi3_parser/nodes/oauth_flow.rb +27 -0
  75. data/lib/openapi3_parser/nodes/oauth_flows.rb +27 -0
  76. data/lib/openapi3_parser/nodes/openapi.rb +44 -0
  77. data/lib/openapi3_parser/nodes/operation.rb +59 -0
  78. data/lib/openapi3_parser/nodes/parameter.rb +21 -0
  79. data/lib/openapi3_parser/nodes/parameter/parameter_like.rb +53 -0
  80. data/lib/openapi3_parser/nodes/path_item.rb +59 -0
  81. data/lib/openapi3_parser/nodes/paths.rb +11 -0
  82. data/lib/openapi3_parser/nodes/request_body.rb +23 -0
  83. data/lib/openapi3_parser/nodes/response.rb +27 -0
  84. data/lib/openapi3_parser/nodes/responses.rb +15 -0
  85. data/lib/openapi3_parser/nodes/schema.rb +159 -0
  86. data/lib/openapi3_parser/nodes/security_requirement.rb +11 -0
  87. data/lib/openapi3_parser/nodes/security_scheme.rb +44 -0
  88. data/lib/openapi3_parser/nodes/server.rb +25 -0
  89. data/lib/openapi3_parser/nodes/server_variable.rb +23 -0
  90. data/lib/openapi3_parser/nodes/tag.rb +23 -0
  91. data/lib/openapi3_parser/nodes/xml.rb +31 -0
  92. data/lib/openapi3_parser/validation/error.rb +14 -0
  93. data/lib/openapi3_parser/validation/error_collection.rb +36 -0
  94. data/lib/openapi3_parser/version.rb +5 -0
  95. data/openapi3_parser.gemspec +28 -0
  96. metadata +208 -0
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openapi3Parser
4
+ module Node
5
+ module Map
6
+ include Enumerable
7
+
8
+ attr_reader :node_data, :node_context
9
+
10
+ def initialize(data, context)
11
+ @node_data = data
12
+ @node_context = context
13
+ end
14
+
15
+ def [](value)
16
+ node_data[value]
17
+ end
18
+
19
+ def each(&block)
20
+ node_data.each(&block)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openapi3Parser
4
+ module Node
5
+ module Object
6
+ include Enumerable
7
+
8
+ attr_reader :node_data, :node_context
9
+
10
+ def initialize(data, context)
11
+ @node_data = data
12
+ @node_context = context
13
+ end
14
+
15
+ def [](value)
16
+ node_data[value]
17
+ end
18
+
19
+ def extension(value)
20
+ node_data["x-#{value}"]
21
+ end
22
+
23
+ def each(&block)
24
+ node_data.each(&block)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openapi3_parser/node_factory"
4
+ require "openapi3_parser/nodes/array"
5
+ require "openapi3_parser/validation/error"
6
+
7
+ module Openapi3Parser
8
+ module NodeFactories
9
+ class Array
10
+ include NodeFactory
11
+ input_type ::Array
12
+
13
+ def initialize(
14
+ context,
15
+ value_input_type: nil,
16
+ value_factory: nil,
17
+ validate: nil
18
+ )
19
+ super(context)
20
+ @given_value_input_type = value_input_type
21
+ @given_value_factory = value_factory
22
+ @given_validate = validate
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :given_value_input_type, :given_value_factory,
28
+ :given_validate
29
+
30
+ def process_input(input)
31
+ input.each_with_index.map do |value, i|
32
+ if value_factory?
33
+ initialize_value_factory(context.next_namespace(i))
34
+ else
35
+ value
36
+ end
37
+ end
38
+ end
39
+
40
+ def validate(input, context)
41
+ given_validate&.call(input, context)
42
+ end
43
+
44
+ def validate_input(error_collection)
45
+ super(error_collection)
46
+ processed_input.each do |value|
47
+ next unless value.respond_to?(:errors)
48
+ error_collection.merge(value.errors)
49
+ end
50
+ error_collection.merge(
51
+ validate_value_input_type(processed_input, context)
52
+ )
53
+ end
54
+
55
+ def build_node(input)
56
+ check_value_input_type(input)
57
+ data = input.map do |value|
58
+ value.respond_to?(:node) ? value.node : value
59
+ end
60
+ build_array(data, context)
61
+ end
62
+
63
+ def build_array(data, context)
64
+ Nodes::Array.new(data, context)
65
+ end
66
+
67
+ def value_factory?
68
+ !given_value_factory.nil?
69
+ end
70
+
71
+ def initialize_value_factory(context)
72
+ factory = given_value_factory
73
+ return factory.new(context) if factory.is_a?(Class)
74
+ factory.call(context)
75
+ end
76
+
77
+ def validate_value_input_type(input, context)
78
+ input.each_with_index.each_with_object([]) do |(value, i), memo|
79
+ error = error_for_value_input_type(value)
80
+ next unless error
81
+ memo << Validation::Error.new(
82
+ context.next_namespace(i), error
83
+ )
84
+ end
85
+ end
86
+
87
+ def check_value_input_type(input)
88
+ input.each_with_index do |value, i|
89
+ error = error_for_value_input_type(value)
90
+ next unless error
91
+ next_context = context.next_namespace(i)
92
+ raise Openapi3Parser::Error,
93
+ "Invalid type for #{next_context.stringify_namespace}. "\
94
+ "#{error}"
95
+ end
96
+ end
97
+
98
+ def error_for_value_input_type(value)
99
+ type = given_value_input_type
100
+ return unless type
101
+ "Expected #{type}" unless value.is_a?(type)
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openapi3_parser/node_factory/map"
4
+ require "openapi3_parser/node_factories/path_item"
5
+ require "openapi3_parser/nodes/callback"
6
+
7
+ module Openapi3Parser
8
+ module NodeFactories
9
+ class Callback
10
+ include NodeFactory::Map
11
+
12
+ private
13
+
14
+ def process_input(input)
15
+ input.each_with_object({}) do |(key, value), memo|
16
+ memo[key] = value if extension?(key)
17
+ memo[key] = NodeFactories::PathItem.new(context.next_namespace(key))
18
+ end
19
+ end
20
+
21
+ def build_map(data, context)
22
+ Nodes::Callback.new(data, context)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openapi3_parser/nodes/components"
4
+ require "openapi3_parser/node_factory/object"
5
+ require "openapi3_parser/node_factory/optional_reference"
6
+ require "openapi3_parser/node_factories/map"
7
+ require "openapi3_parser/node_factories/schema"
8
+ require "openapi3_parser/node_factories/response"
9
+ require "openapi3_parser/node_factories/parameter"
10
+ require "openapi3_parser/node_factories/example"
11
+ require "openapi3_parser/node_factories/request_body"
12
+ require "openapi3_parser/node_factories/header"
13
+ require "openapi3_parser/node_factories/security_scheme"
14
+ require "openapi3_parser/node_factories/link"
15
+ require "openapi3_parser/node_factories/callback"
16
+
17
+ module Openapi3Parser
18
+ module NodeFactories
19
+ class Components
20
+ include NodeFactory::Object
21
+
22
+ allow_extensions
23
+ field "schemas", factory: :schemas_factory
24
+ field "responses", factory: :responses_factory
25
+ field "parameters", factory: :parameters_factory
26
+ field "examples", factory: :examples_factory
27
+ field "requestBodies", factory: :request_bodies_factory
28
+ field "headers", factory: :headers_factory
29
+ field "securitySchemes", factory: :security_schemes_factory
30
+ field "links", factory: :links_factory
31
+ field "callbacks", factory: :callbacks_factory
32
+
33
+ private
34
+
35
+ def build_object(data, context)
36
+ Nodes::Components.new(data, context)
37
+ end
38
+
39
+ def schemas_factory(context)
40
+ referenceable_map_factory(context, NodeFactories::Schema)
41
+ end
42
+
43
+ def responses_factory(context)
44
+ referenceable_map_factory(context, NodeFactories::Response)
45
+ end
46
+
47
+ def parameters_factory(context)
48
+ referenceable_map_factory(context, NodeFactories::Parameter)
49
+ end
50
+
51
+ def examples_factory(context)
52
+ referenceable_map_factory(context, NodeFactories::Example)
53
+ end
54
+
55
+ def request_bodies_factory(context)
56
+ referenceable_map_factory(context, NodeFactories::RequestBody)
57
+ end
58
+
59
+ def headers_factory(context)
60
+ referenceable_map_factory(context, NodeFactories::Header)
61
+ end
62
+
63
+ def security_schemes_factory(context)
64
+ referenceable_map_factory(context, NodeFactories::SecurityScheme)
65
+ end
66
+
67
+ def links_factory(context)
68
+ referenceable_map_factory(context, NodeFactories::Link)
69
+ end
70
+
71
+ def callbacks_factory(context)
72
+ referenceable_map_factory(context, NodeFactories::Callback)
73
+ end
74
+
75
+ def referenceable_map_factory(context, factory)
76
+ NodeFactories::Map.new(
77
+ context,
78
+ value_factory: NodeFactory::OptionalReference.new(factory)
79
+ )
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openapi3_parser/nodes/contact"
4
+ require "openapi3_parser/node_factory/object"
5
+
6
+ module Openapi3Parser
7
+ module NodeFactories
8
+ class Contact
9
+ include NodeFactory::Object
10
+
11
+ allow_extensions
12
+
13
+ field "name", input_type: String
14
+ field "url", input_type: String
15
+ field "email", input_type: String
16
+
17
+ private
18
+
19
+ def build_object(data, context)
20
+ Nodes::Contact.new(data, context)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openapi3_parser/nodes/discriminator"
4
+ require "openapi3_parser/node_factory/object"
5
+
6
+ module Openapi3Parser
7
+ module NodeFactories
8
+ class Discriminator
9
+ include NodeFactory::Object
10
+
11
+ field "propertyName", input_type: String, required: true
12
+ field "mapping", input_type: Hash,
13
+ validate: :validate_mapping,
14
+ default: -> { {}.freeze }
15
+
16
+ private
17
+
18
+ def build_object(data, context)
19
+ Nodes::Discriminator.new(data, context)
20
+ end
21
+
22
+ def validate_mapping(input)
23
+ return if input.empty?
24
+ string_keys = input.keys.map(&:class).uniq == [String]
25
+ string_values = input.values.map(&:class).uniq == [String]
26
+ valid = string_keys && string_values
27
+ return "Expected string keys and string values" unless valid
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openapi3_parser/nodes/encoding"
4
+ require "openapi3_parser/node_factory/object"
5
+ require "openapi3_parser/node_factory/optional_reference"
6
+ require "openapi3_parser/node_factories/map"
7
+ require "openapi3_parser/node_factories/header"
8
+
9
+ module Openapi3Parser
10
+ module NodeFactories
11
+ class Encoding
12
+ include NodeFactory::Object
13
+
14
+ allow_extensions
15
+
16
+ field "contentType", input_type: String
17
+ field "headers", factory: :headers_factory
18
+ field "style", input_type: String
19
+ field "explode", input_type: :boolean, default: true
20
+ field "allowReserved", input_type: :boolean, default: false
21
+
22
+ private
23
+
24
+ def build_object(data, context)
25
+ Nodes::Encoding.new(data, context)
26
+ end
27
+
28
+ def headers_factory(context)
29
+ factory = NodeFactory::OptionalReference.new(NodeFactories::Header)
30
+ NodeFactories::Map.new(context, value_factory: factory)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openapi3_parser/nodes/example"
4
+ require "openapi3_parser/node_factory/object"
5
+
6
+ module Openapi3Parser
7
+ module NodeFactories
8
+ class Example
9
+ include NodeFactory::Object
10
+
11
+ allow_extensions
12
+
13
+ field "summary", input_type: String
14
+ field "description", input_type: String
15
+ field "value"
16
+ field "externalValue", input_type: String
17
+
18
+ private
19
+
20
+ def build_object(data, context)
21
+ Nodes::Example.new(data, context)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openapi3_parser/nodes/external_documentation"
4
+ require "openapi3_parser/node_factory/object"
5
+
6
+ module Openapi3Parser
7
+ module NodeFactories
8
+ class ExternalDocumentation
9
+ include NodeFactory::Object
10
+
11
+ allow_extensions
12
+
13
+ field "description", input_type: String
14
+ field "url", required: true, input_type: String
15
+
16
+ private
17
+
18
+ def build_object(data, context)
19
+ Nodes::ExternalDocumentation.new(data, context)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openapi3_parser/nodes/header"
4
+ require "openapi3_parser/node_factories/parameter/parameter_like"
5
+ require "openapi3_parser/node_factory/object"
6
+
7
+ module Openapi3Parser
8
+ module NodeFactories
9
+ class Header
10
+ include NodeFactory::Object
11
+ include Parameter::ParameterLike
12
+
13
+ allow_extensions
14
+
15
+ field "description", input_type: String
16
+ field "required", input_type: :boolean, default: false
17
+ field "deprecated", input_type: :boolean, default: false
18
+ field "allowEmptyValue", input_type: :boolean, default: false
19
+
20
+ field "style", input_type: String, default: "simple"
21
+ field "explode", input_type: :boolean, default: :default_explode
22
+ field "allowReserved", input_type: :boolean, default: false
23
+ field "schema", factory: :schema_factory
24
+ field "example"
25
+ field "examples", factory: :examples_factory
26
+
27
+ field "content", factory: :content_factory
28
+
29
+ private
30
+
31
+ def build_object(data, context)
32
+ Nodes::Header.new(data, context)
33
+ end
34
+ end
35
+ end
36
+ end