openapi3_parser 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ca568bd4f56414a9a7467ba929750fe26ebe2ff6
4
- data.tar.gz: 3e0a2532c02e114140e3ea53458e637308b1ab53
3
+ metadata.gz: e4d2e3dac62b0199c892ea6d0e9ed34dcea67c9d
4
+ data.tar.gz: 93839047c652d0342d0a6a6700863c4a143e3c21
5
5
  SHA512:
6
- metadata.gz: 916678f780b8d71fd83c09a64fc6725e678dd8356a137d4b7295c7c4ca185d824c4803bae74c46de755faa5c52736ddcbada93fc0c88ef12d8b2e0e583edc56a
7
- data.tar.gz: effde9d9458daf6d68bc5c9d79d137c32d583246f620f039b4606f1cf678d6f10bea2553a23b0930b2b73ecb0a665a66c9f5ac227e15956629095746363d634e
6
+ metadata.gz: 06c8d624f8a4a3e95f8838f60682a7a2861e333cfcb4c43a5f9fc4961406c1aeb5ad91da57e1664fa9a9d62eb7deaade81314ec924dec8e09b6eda1b00fb0d2b
7
+ data.tar.gz: 1304885f64188113d219df891a3584259cff0a628c696ad74902fd5667da994e9f2116ab880436f4e3c5a31a21605bb3f0c21a1202499780b6a861bb482713ad
@@ -1,3 +1,16 @@
1
+ # 0.7.0
2
+
3
+ - Add `#values` method to `Node::Object` and `Node#Map` to have a method that
4
+ pairs with `#keys`
5
+ - Add `Node::Schema#requires?` method to simplify checking whether a property
6
+ is required by a particular schema.
7
+ - Add `#==` methods to Node objects. This allows checking whether two nodes
8
+ are from the same source location even if they're referenced in different
9
+ places.
10
+ - Add `Node::Schema#name` method that looks up the name of a Schema based
11
+ on it's contextual position in a document. Allows accessing the `Pet` value
12
+ from `#/components/schemas/Pet`.
13
+
1
14
  # 0.6.1
2
15
 
3
16
  - Fix bug where Node::Object and Node::Map iterated arrays rather than hashes
data/README.md CHANGED
@@ -118,7 +118,7 @@ You can install this gem into your bundler application by adding this line to
118
118
  your Gemfile:
119
119
 
120
120
  ```
121
- gem "openapi3_parser", "~> 0.6.0"
121
+ gem "openapi3_parser", "~> 0.7.0"
122
122
  ```
123
123
 
124
124
  and then running `$ bundle install`
@@ -60,7 +60,7 @@ module Openapi3Parser
60
60
  # @!method external_docs
61
61
  # The value of the external_docs field on the OpenAPI document
62
62
  # @see Node::Openapi#external_docs
63
- # @return [Node::ExternalDocumentation]
63
+ # @return [Node::ExternalDocumentation, nil]
64
64
  # @!method extension
65
65
  # Look up an extension field provided for the root object of the document
66
66
  # @see Node::Object#extension
@@ -35,6 +35,14 @@ module Openapi3Parser
35
35
  Placeholder.each(node_data, &block)
36
36
  end
37
37
 
38
+ # @param [Any] other
39
+ #
40
+ # @return [Boolean]
41
+ def ==(other)
42
+ other.instance_of?(self.class) &&
43
+ node_context.same_data_and_source?(other.node_context)
44
+ end
45
+
38
46
  # Used to access a node relative to this node
39
47
  # @param [Source::Pointer, ::Array, ::String] pointer_like
40
48
  # @return anything
@@ -65,10 +65,19 @@ module Openapi3Parser
65
65
  @source_location = source_location
66
66
  end
67
67
 
68
+ # @param [Context] other
68
69
  # @return [Boolean]
69
70
  def ==(other)
71
+ document_location == other.document_location &&
72
+ same_data_and_source?(other)
73
+ end
74
+
75
+ # Check that contexts are the same without concern for document location
76
+ #
77
+ # @param [Context] other
78
+ # @return [Boolean]
79
+ def same_data_and_source?(other)
70
80
  input == other.input &&
71
- document_location == other.document_location &&
72
81
  source_location == other.source_location
73
82
  end
74
83
 
@@ -48,6 +48,14 @@ module Openapi3Parser
48
48
  self["x-#{value}"]
49
49
  end
50
50
 
51
+ # @param [Any] other
52
+ #
53
+ # @return [Boolean]
54
+ def ==(other)
55
+ other.instance_of?(self.class) &&
56
+ node_context.same_data_and_source?(other.node_context)
57
+ end
58
+
51
59
  # Iterates through the data of this node, used by Enumerable
52
60
  #
53
61
  # @return [Object]
@@ -55,6 +63,14 @@ module Openapi3Parser
55
63
  Placeholder.each(node_data, &block)
56
64
  end
57
65
 
66
+ # Provide an array of values for this object, a partner to the #keys
67
+ # method
68
+ #
69
+ # @return [Array]
70
+ def values
71
+ map(&:last)
72
+ end
73
+
58
74
  # Used to access a node relative to this node
59
75
  # @param [Source::Pointer, ::Array, ::String] pointer_like
60
76
  # @return anything
@@ -48,6 +48,14 @@ module Openapi3Parser
48
48
  self["x-#{value}"]
49
49
  end
50
50
 
51
+ # @param [Any] other
52
+ #
53
+ # @return [Boolean]
54
+ def ==(other)
55
+ other.instance_of?(self.class) &&
56
+ node_context.same_data_and_source?(other.node_context)
57
+ end
58
+
51
59
  # Iterates through the data of this node, used by Enumerable
52
60
  #
53
61
  # @return [Object]
@@ -55,6 +63,14 @@ module Openapi3Parser
55
63
  Placeholder.each(node_data, &block)
56
64
  end
57
65
 
66
+ # Provide an array of values for this object, a partner to the #keys
67
+ # method
68
+ #
69
+ # @return [Array]
70
+ def values
71
+ map(&:last)
72
+ end
73
+
58
74
  # Used to render fields that can be in markdown syntax into HTML
59
75
  # @param [String, nil] value
60
76
  # @return [String, nil]
@@ -7,6 +7,37 @@ module Openapi3Parser
7
7
  # @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#schemaObject
8
8
  # rubocop:disable ClassLength
9
9
  class Schema < Node::Object
10
+ # This is used to provide a name for the schema based on it's position in
11
+ # an OpenAPI document.
12
+ #
13
+ # For example it's common to have an OpenAPI document structured like so:
14
+ # components:
15
+ # schemas:
16
+ # Product:
17
+ # properties:
18
+ # product_id:
19
+ # type: string
20
+ # description:
21
+ # type: string
22
+ #
23
+ # and there is then implied meaning in the field name of Product, ie
24
+ # that schema now represents a product. This data is not easily or
25
+ # consistently made available as it is part of the path to the data
26
+ # rather than the data itself. Instead the field that would be more
27
+ # appropriate would be "title" within a schema.
28
+ #
29
+ # As this is a common pattern in OpenAPI docs this provides a method
30
+ # to look up this contextual name of the schema so it can be referenced
31
+ # when working with the document, it only considers a field to be
32
+ # name if it is within a group called schemas (as is the case
33
+ # in #/components/schemas)
34
+ #
35
+ # @return [String, nil]
36
+ def name
37
+ segments = node_context.source_location.pointer.segments
38
+ segments[-1] if segments[-2] == "schemas"
39
+ end
40
+
10
41
  # @return [String, nil]
11
42
  def title
12
43
  self["title"]
@@ -82,6 +113,21 @@ module Openapi3Parser
82
113
  self["required"]
83
114
  end
84
115
 
116
+ # Returns whether a property is a required field or not. Can accept the
117
+ # property name or a schema
118
+ #
119
+ # @param [String, Schema] property
120
+ # @return [Boolean]
121
+ def requires?(property)
122
+ if property.is_a?(Schema)
123
+ properties.to_h
124
+ .select { |k, _| required.to_a.include?(k) }
125
+ .any? { |_, schema| schema == property }
126
+ else
127
+ required.to_a.include?(property)
128
+ end
129
+ end
130
+
85
131
  # @return [Node::Array<Object>, nil]
86
132
  def enum
87
133
  self["enum"]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Openapi3Parser
4
- VERSION = "0.6.1"
4
+ VERSION = "0.7.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openapi3_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Dew
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-09 00:00:00.000000000 Z
11
+ date: 2019-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: commonmarker