openapi_parser_firetail 1.0.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 (93) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/ci.yaml +25 -0
  3. data/.gitignore +15 -0
  4. data/.rspec +3 -0
  5. data/.rubocop.yml +13 -0
  6. data/.rubocop_ignore.yml +6 -0
  7. data/.ruby-version +1 -0
  8. data/CHANGELOG.md +132 -0
  9. data/CODE_OF_CONDUCT.md +74 -0
  10. data/Gemfile +6 -0
  11. data/LICENSE.txt +21 -0
  12. data/README.md +112 -0
  13. data/Rakefile +10 -0
  14. data/Steepfile +11 -0
  15. data/bin/console +14 -0
  16. data/bin/setup +8 -0
  17. data/lib/openapi_parser/concern.rb +5 -0
  18. data/lib/openapi_parser/concerns/expandable.rb +87 -0
  19. data/lib/openapi_parser/concerns/findable.rb +54 -0
  20. data/lib/openapi_parser/concerns/media_type_selectable.rb +29 -0
  21. data/lib/openapi_parser/concerns/parameter_validatable.rb +62 -0
  22. data/lib/openapi_parser/concerns/parser/core.rb +21 -0
  23. data/lib/openapi_parser/concerns/parser/hash.rb +10 -0
  24. data/lib/openapi_parser/concerns/parser/hash_body.rb +12 -0
  25. data/lib/openapi_parser/concerns/parser/list.rb +10 -0
  26. data/lib/openapi_parser/concerns/parser/object.rb +14 -0
  27. data/lib/openapi_parser/concerns/parser/value.rb +14 -0
  28. data/lib/openapi_parser/concerns/parser.rb +45 -0
  29. data/lib/openapi_parser/concerns/schema_loader/base.rb +28 -0
  30. data/lib/openapi_parser/concerns/schema_loader/creator.rb +48 -0
  31. data/lib/openapi_parser/concerns/schema_loader/hash_body_loader.rb +37 -0
  32. data/lib/openapi_parser/concerns/schema_loader/hash_objects_loader.rb +29 -0
  33. data/lib/openapi_parser/concerns/schema_loader/list_loader.rb +28 -0
  34. data/lib/openapi_parser/concerns/schema_loader/objects_loader.rb +21 -0
  35. data/lib/openapi_parser/concerns/schema_loader/values_loader.rb +10 -0
  36. data/lib/openapi_parser/concerns/schema_loader.rb +58 -0
  37. data/lib/openapi_parser/config.rb +55 -0
  38. data/lib/openapi_parser/errors.rb +281 -0
  39. data/lib/openapi_parser/parameter_validator.rb +33 -0
  40. data/lib/openapi_parser/path_item_finder.rb +161 -0
  41. data/lib/openapi_parser/reference_expander.rb +9 -0
  42. data/lib/openapi_parser/request_operation.rb +90 -0
  43. data/lib/openapi_parser/schema_validator/all_of_validator.rb +40 -0
  44. data/lib/openapi_parser/schema_validator/any_of_validator.rb +18 -0
  45. data/lib/openapi_parser/schema_validator/array_validator.rb +32 -0
  46. data/lib/openapi_parser/schema_validator/base.rb +39 -0
  47. data/lib/openapi_parser/schema_validator/boolean_validator.rb +29 -0
  48. data/lib/openapi_parser/schema_validator/enumable.rb +13 -0
  49. data/lib/openapi_parser/schema_validator/float_validator.rb +34 -0
  50. data/lib/openapi_parser/schema_validator/integer_validator.rb +32 -0
  51. data/lib/openapi_parser/schema_validator/minimum_maximum.rb +38 -0
  52. data/lib/openapi_parser/schema_validator/nil_validator.rb +11 -0
  53. data/lib/openapi_parser/schema_validator/object_validator.rb +56 -0
  54. data/lib/openapi_parser/schema_validator/one_of_validator.rb +22 -0
  55. data/lib/openapi_parser/schema_validator/options.rb +29 -0
  56. data/lib/openapi_parser/schema_validator/string_validator.rb +108 -0
  57. data/lib/openapi_parser/schema_validator/unspecified_type_validator.rb +8 -0
  58. data/lib/openapi_parser/schema_validator.rb +164 -0
  59. data/lib/openapi_parser/schemas/base.rb +28 -0
  60. data/lib/openapi_parser/schemas/classes.rb +22 -0
  61. data/lib/openapi_parser/schemas/components.rb +32 -0
  62. data/lib/openapi_parser/schemas/discriminator.rb +11 -0
  63. data/lib/openapi_parser/schemas/header.rb +18 -0
  64. data/lib/openapi_parser/schemas/info.rb +6 -0
  65. data/lib/openapi_parser/schemas/media_type.rb +18 -0
  66. data/lib/openapi_parser/schemas/openapi.rb +63 -0
  67. data/lib/openapi_parser/schemas/operation.rb +50 -0
  68. data/lib/openapi_parser/schemas/parameter.rb +20 -0
  69. data/lib/openapi_parser/schemas/path_item.rb +22 -0
  70. data/lib/openapi_parser/schemas/paths.rb +7 -0
  71. data/lib/openapi_parser/schemas/reference.rb +7 -0
  72. data/lib/openapi_parser/schemas/request_body.rb +34 -0
  73. data/lib/openapi_parser/schemas/response.rb +54 -0
  74. data/lib/openapi_parser/schemas/responses.rb +56 -0
  75. data/lib/openapi_parser/schemas/schema.rb +117 -0
  76. data/lib/openapi_parser/schemas/security.rb +7 -0
  77. data/lib/openapi_parser/schemas/security_schemes.rb +20 -0
  78. data/lib/openapi_parser/schemas.rb +20 -0
  79. data/lib/openapi_parser/version.rb +3 -0
  80. data/lib/openapi_parser.rb +108 -0
  81. data/openapi_parser.gemspec +43 -0
  82. data/sig/openapi_parser/config.rbs +19 -0
  83. data/sig/openapi_parser/errors.rbs +22 -0
  84. data/sig/openapi_parser/reference_expander.rbs +3 -0
  85. data/sig/openapi_parser/schema_validator.rbs +46 -0
  86. data/sig/openapi_parser/schema_validators/base.rbs +18 -0
  87. data/sig/openapi_parser/schema_validators/options.rbs +17 -0
  88. data/sig/openapi_parser/schemas/base.rbs +17 -0
  89. data/sig/openapi_parser/version.rbs +3 -0
  90. data/sig/openapi_parser.rbs +19 -0
  91. data/sig/types.rbs +13 -0
  92. data/sig/wip_types.rbs +64 -0
  93. metadata +288 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b580aba6c2c13cc921baea19d6404cefa07009d1167a1274735e6e836658d725
4
+ data.tar.gz: 9793c88ea20858b72b68918bb6b4a99b5f1ce6af3051db1cb1ca7d37b9265fb4
5
+ SHA512:
6
+ metadata.gz: 964b669bc097f851d62669e45c637302a312562611b2f3475cb530d3a6b07cbab65fb182db7b9b42432dc43cb5b836f34d2b3e37966919acc449178df3dfb43f
7
+ data.tar.gz: d8df564537cc69f4fcee809e66889c201b8dab5942f312fbd1dd68e7dd379655350dce51286cf2c60868b4cdcb098f52614f9c036bf6c975937656a8eae1b65c
@@ -0,0 +1,25 @@
1
+ name: ci
2
+ on: [push]
3
+ jobs:
4
+ test:
5
+ strategy:
6
+ fail-fast: false
7
+ matrix:
8
+ os:
9
+ - ubuntu-latest
10
+ - macos-latest
11
+ ruby:
12
+ - "2.6"
13
+ - "2.7"
14
+ - "3.0"
15
+ - "3.1"
16
+ - "3.2"
17
+ - ruby-head
18
+ runs-on: ${{ matrix.os }}
19
+ steps:
20
+ - uses: actions/checkout@v2
21
+ - uses: ruby/setup-ruby@v1
22
+ with:
23
+ ruby-version: ${{ matrix.ruby }}
24
+ bundler-cache: true
25
+ - run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # RubyMine
11
+ .idea
12
+
13
+ # rspec failure tracking
14
+ .rspec_status
15
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ inherit_gem:
2
+ fincop:
3
+ # - 'config/rails.yml'
4
+ - 'config/rspec.yml'
5
+ - 'config/rubocop.yml'
6
+ # If you are using Rails 4, activate this cop.
7
+ # - 'config/disabled_for_rails_4.yml'
8
+
9
+ inherit_from:
10
+ - '.rubocop_ignore.yml'
11
+
12
+ AllCops:
13
+ TargetRubyVersion: 2.5
@@ -0,0 +1,6 @@
1
+ Metrics/ParameterLists:
2
+ Exclude:
3
+ - lib/openapi_parser/concerns/parser.rb
4
+
5
+ RSpec/FilePath:
6
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,132 @@
1
+ ## Unreleased
2
+
3
+ ## 1.0.0 (2021-02-03)
4
+ ### Added
5
+ * Add date-time format validation #126
6
+
7
+ ## 1.0.0.beta1 (2021-12-15)
8
+ ### Added
9
+ * Add strict_reference_validation config and implementation to address/implement #29 #123
10
+
11
+ ## 0.15.0 (2021-09-27)
12
+ ### Added
13
+ * support: relative file path escape. #117
14
+
15
+ ## 0.14.1 (2021-07-9)
16
+ ### Fixed
17
+ * Fix bug for using path parameter and coerce option #115
18
+
19
+ ## 0.14.0 (2021-05-24)
20
+
21
+ ### Added
22
+ * Add basic polymorphism handling #103
23
+ * Support empty schema as any type #109
24
+ * Add date format validation for string #102
25
+
26
+ ### Fixed
27
+ * Fix anyOf coercion to float and integer when value is a non-string type #110
28
+
29
+ ## 0.13.0 (2021-05-01)
30
+ * Fix a problem with remote reference to path items which have path parameters #95
31
+ * Support enum for booleans. #104
32
+
33
+ ## 0.12.1 (2020-08-27)
34
+ * Use CGI.unescape (warning fix) #92
35
+
36
+ ## 0.12.0 (2020-08-26)
37
+ * Find path by extracted params than path length #84
38
+ * Unescape ref URI before lookup in OpenAPIParser::Findable #85
39
+ * Improved path parameter matching code to allow file extensions, multiple parameters inside one path element, etc #90
40
+
41
+ ## 0.11.2 (2020-05-23)
42
+ * Allow date and time content in YAML #81
43
+
44
+ ## 0.11.1 (2020-05-09)
45
+ * fix too many warning
46
+
47
+ ## 0.11.0 (2020-05-09)
48
+ * Add committee friendly interface to use remote references. #74
49
+ * Prevent SystemStackError on recursive schema reference #76
50
+ * support newest ruby versions #78
51
+
52
+ ## 0.10.0 (2020-04-01)
53
+ * Support $ref to objects in other OpenAPI yaml files #66
54
+ * Allow $ref for path item objects #71
55
+
56
+ ## 0.9.0 (2020-03-22)
57
+ * Added support for validating UUID formatted strings #67
58
+
59
+ ## 0.8.0 (2020-01-21)
60
+ * Append the example to the Pattern validator error message #64
61
+
62
+ ## 0.7.0 (2020-01-15)
63
+ * Avoid potential `.send(:exit)` #58
64
+ * Improve PathItemFinder #44
65
+
66
+ ## 0.6.1 (2019-10-12)
67
+ * Bugfix: validate non-nullable response header #54
68
+ * Improve grammar in error messages #55
69
+ * fix object validator in case of properties missing #56
70
+
71
+ ## 0.6.0 (2019-10-05)
72
+ * add email format validation on string #51
73
+
74
+ ## 0.5.0 (2019-09-28)
75
+ * Add max and min length validators for string. #45
76
+ * Support for minItems and maxItems in array #49
77
+
78
+ ## 0.4.1 (2019-07-27)
79
+ * release missed
80
+
81
+ ## 0.4.0 (2019-07-27)
82
+ * Add minimum and maximum checks for `integer` and `number` data types (#43)
83
+
84
+ ## 0.3.1 (2019-06-04)
85
+ * Add additionalProperties default value (#40)
86
+
87
+ ## 0.3.0 (2019-06-01)
88
+
89
+ ### features
90
+ * Perform a strict check on object properties (#33)
91
+
92
+ ### Bugfix
93
+ * Support discriminator without mapping (#35)
94
+ * Fix upper case request param validation (#38)
95
+
96
+ ## 0.2.7 (2019-05-20)
97
+ * Fix for release miss
98
+
99
+ ## 0.2.6 (2019-05-20)
100
+ * Add support for discriminator (#32)
101
+
102
+ ## 0.2.5 (2019-04-12)
103
+ * Support one of validator (#26)
104
+
105
+ ## 0.2.3 (2019-03-18)
106
+ * validate_header_parameter support case incentive (#25)
107
+
108
+ ## 0.2.2 (2019-01-06)
109
+ * bugfix for datetime validate (#20)
110
+
111
+ ## 0.2.1 (2019-01-03)
112
+ * raise error when invalid datetime format (#19)
113
+
114
+ ## 0.2.0 (2019-01-02)
115
+ * support header validate (#18)
116
+
117
+ ## 0.1.9 (2019-01-01)
118
+ * add strict option (#17)
119
+
120
+ ## 0.1.8 (2018-12-31)
121
+ * add select_media_type method(#16)
122
+
123
+ ## 0.1.7 (2018-12-30)
124
+ * Float value validate bugfix (#15)
125
+
126
+ ## 0.1.6 (2018-12-29)
127
+ * Support allOf definition (#11)
128
+ * Support wildcard status code (#12)
129
+ * Support wildcard content type (#13)
130
+
131
+ ## 0.1.5 (2018-12-23)
132
+ * First release for production usage
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at ota42y@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in openapi_parser.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 ota42y
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,112 @@
1
+ # OpenAPI Parser
2
+ [![ci](https://github.com/ota42y/openapi_parser/actions/workflows/ci.yaml/badge.svg)](https://github.com/ota42y/openapi_parser/actions/workflows/ci.yaml)
3
+ [![Gem Version](https://badge.fury.io/rb/openapi_parser.svg)](https://badge.fury.io/rb/openapi_parser)
4
+ [![Yard Docs](https://img.shields.io/badge/yard-docs-blue.svg)](https://www.rubydoc.info/gems/openapi_parser)
5
+ [![Inch CI](https://inch-ci.org/github/ota42y/openapi_parser.svg?branch=master)](https://inch-ci.org/github/ota42y/openapi_parser)
6
+
7
+ This is OpenAPI3 parser and validator.
8
+
9
+ ## Usage
10
+
11
+ ```ruby
12
+ root = OpenAPIParser.parse(YAML.load_file('open_api_3/schema.yml'))
13
+
14
+ # request operation combine path parameters and OpenAPI3's Operation Object
15
+ request_operation = root.request_operation(:post, '/validate')
16
+
17
+ ret = request_operation.validate_request_body('application/json', {"integer" => 1})
18
+ # => {"integer" => 1}
19
+
20
+ # invalid parameter
21
+ request_operation.validate_request_body('application/json', {"integer" => '1'})
22
+ # => OpenAPIParser::ValidateError: 1 class is String but it's not valid integer in #/paths/~1validate/post/requestBody/content/application~1json/schema/properties/integer
23
+
24
+ # path parameter
25
+ request_operation = root.request_operation(:get, '/path_template_test/1')
26
+ request_operation.path_params
27
+ # => {"template_name"=>"1"}
28
+
29
+ # coerce parameter
30
+ root = OpenAPIParser.parse(YAML.load_file('open_api_3/schema.yml'), {coerce_value: true, datetime_coerce_class: DateTime})
31
+ request_operation = root.request_operation(:get, '/string_params_coercer')
32
+ request_operation.validate_request_parameter({'integer_1' => '1', 'datetime_string' => '2016-04-01T16:00:00+09:00'})
33
+ # => {"integer_1"=>1, "datetime_string"=>#<DateTime: 2016-04-01T16:00:00+09:00 ((2457480j,25200s,0n),+32400s,2299161j)>
34
+ # convert number string to Integer and datetime string to DateTime class
35
+
36
+ ```
37
+
38
+ ## Installation
39
+
40
+ Add this line to your application's Gemfile:
41
+
42
+ ```ruby
43
+ gem 'openapi_parser'
44
+ ```
45
+
46
+ And then execute:
47
+
48
+ $ bundle
49
+
50
+ Or install it yourself as:
51
+
52
+ $ gem install openapi_parser
53
+
54
+ ## Additional features
55
+ OpenAPI Parser's validation based on [OpenAPI spec](https://github.com/OAI/OpenAPI-Specification)
56
+ But we support few useful features.
57
+
58
+ ### type validation
59
+ We support additional type validation.
60
+
61
+ |type|format|description|
62
+ |---|---|---|
63
+ |string|uuid|validate uuid string. But we don't check uuid layout|
64
+
65
+ ### Reference Validation on Schema Load
66
+ Invalid references (missing definitions, typos, etc.) can cause validation to fail in runtime,
67
+ and these errors can be difficult to debug (see: https://github.com/ota42y/openapi_parser/issues/29).
68
+
69
+ Pass the `strict_reference_validation: true` option to detect invalid references.
70
+ An `OpenAPIError::MissingReferenceError` exception will be raised when a reference cannot be resolved.
71
+
72
+ If the `expand_reference` configuration is explicitly `false` (default is `true`), then
73
+ this configuration has no effect.
74
+
75
+ DEPRECATION NOTICE: To maintain compatibility with the previous behavior, this version sets `false` as a default.
76
+ This behavior will be changed to `true` in a later version, so you should explicitly pass `strict_reference_validation: false`
77
+ if you wish to keep the old behavior (and please let the maintainers know your use-case for this configuration!).
78
+
79
+ ```ruby
80
+ yaml_file = YAML.load_file('open_api_3/schema_with_broken_references.yml')
81
+ options = {
82
+ coerce_value: true,
83
+ datetime_coerce_class: DateTime,
84
+ # This defaults to false (for now) - passing `true` provides load-time validation of refs
85
+ strict_reference_validation: true
86
+ }
87
+
88
+ # Will raise with OpenAPIParser::MissingReferenceError
89
+ OpenAPIParser.parse(yaml_file, options)
90
+ ```
91
+
92
+ ## ToDo
93
+ - correct schema checker
94
+ - more detailed validator
95
+
96
+ ## Development
97
+
98
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
99
+
100
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
101
+
102
+ ## Contributing
103
+
104
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ota42y/openapi_parser. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
105
+
106
+ ## License
107
+
108
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
109
+
110
+ ## Code of Conduct
111
+
112
+ Everyone interacting in the OpenAPIParser project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/openapi_parser/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :steep do
7
+ sh 'steep check'
8
+ end
9
+
10
+ task :default => [:steep, :spec]
data/Steepfile ADDED
@@ -0,0 +1,11 @@
1
+ target :lib do
2
+ signature "sig"
3
+ #check "lib"
4
+
5
+ check "lib/openapi_parser.rb"
6
+ check "lib/openapi_parser/config.rb"
7
+ check "lib/openapi_parser/schema_validator/options.rb"
8
+ check "lib/openapi_parser/schema_validator/base.rb"
9
+
10
+ library 'uri', 'json', 'pathname'
11
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "openapi_parser"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ require_relative 'concerns/parser'
2
+ require_relative 'concerns/findable'
3
+ require_relative 'concerns/expandable'
4
+ require_relative 'concerns/media_type_selectable'
5
+ require_relative 'concerns/parameter_validatable'
@@ -0,0 +1,87 @@
1
+ module OpenAPIParser::Expandable
2
+ # expand refs
3
+ # @param [OpenAPIParser::Schemas::Base] root
4
+ # @return nil
5
+ def expand_reference(root, validate_references)
6
+ expand_list_objects(root, self.class._openapi_attr_list_objects.keys, validate_references)
7
+ expand_objects(root, self.class._openapi_attr_objects.keys, validate_references)
8
+ expand_hash_objects(root, self.class._openapi_attr_hash_objects.keys, validate_references)
9
+ expand_hash_objects(root, self.class._openapi_attr_hash_body_objects.keys, validate_references)
10
+ nil
11
+ end
12
+
13
+ private
14
+
15
+ def expand_hash_objects(root, attribute_names, validate_references)
16
+ return unless attribute_names
17
+
18
+ attribute_names.each { |name| expand_hash_attribute(root, name, validate_references) }
19
+ end
20
+
21
+ def expand_hash_attribute(root, name, validate_references)
22
+ h = send(name)
23
+ return if h.nil?
24
+
25
+ update_values = h.map do |k, v|
26
+ new_object = expand_object(root, v, validate_references)
27
+ new_object.nil? ? nil : [k, new_object]
28
+ end
29
+
30
+ update_values.compact.each do |k, v|
31
+ _update_child_object(h[k], v)
32
+ h[k] = v
33
+ end
34
+ end
35
+
36
+ def expand_objects(root, attribute_names, validate_references)
37
+ return unless attribute_names
38
+
39
+ attribute_names.each do |name|
40
+ v = send(name)
41
+ next if v.nil?
42
+
43
+ new_object = expand_object(root, v, validate_references)
44
+ next if new_object.nil?
45
+
46
+ _update_child_object(v, new_object)
47
+ self.instance_variable_set("@#{name}", new_object)
48
+ end
49
+ end
50
+
51
+ def expand_list_objects(root, attribute_names, validate_references)
52
+ return unless attribute_names
53
+
54
+ attribute_names.each do |name|
55
+ l = send(name)
56
+ next if l.nil?
57
+
58
+ l.each_with_index do |v, idx|
59
+ new_object = expand_object(root, v, validate_references)
60
+ next if new_object.nil?
61
+
62
+ _update_child_object(v, new_object)
63
+ l[idx] = new_object
64
+ end
65
+ end
66
+ end
67
+
68
+ def expand_object(root, object, validate_references)
69
+ if object.kind_of?(OpenAPIParser::Schemas::Reference)
70
+ ref_object = referenced_object(root, object)
71
+ raise OpenAPIParser::MissingReferenceError.new(object.ref) if ref_object.nil? && validate_references
72
+
73
+ return ref_object
74
+ end
75
+
76
+ object.expand_reference(root, validate_references) if object.kind_of?(OpenAPIParser::Expandable)
77
+ nil
78
+ end
79
+
80
+ # @param [OpenAPIParser::Schemas::OpenAPI] root
81
+ # @param [OpenAPIParser::Schemas::Reference] reference
82
+ def referenced_object(root, reference)
83
+ obj = root.find_object(reference.ref)
84
+
85
+ obj.kind_of?(OpenAPIParser::Schemas::Reference) ? referenced_object(root, obj) : obj
86
+ end
87
+ end
@@ -0,0 +1,54 @@
1
+ require 'cgi'
2
+ require 'uri'
3
+
4
+ module OpenAPIParser::Findable
5
+ # @param [String] reference
6
+ # @return [OpenAPIParser::Findable]
7
+ def find_object(reference)
8
+ return self if object_reference == reference
9
+ remote_reference = !reference.start_with?('#')
10
+ return find_remote_object(reference) if remote_reference
11
+ return nil unless reference.start_with?(object_reference)
12
+
13
+ unescaped_reference = CGI.unescape(reference)
14
+
15
+ @find_object_cache = {} unless defined? @find_object_cache
16
+ if (obj = @find_object_cache[unescaped_reference])
17
+ return obj
18
+ end
19
+
20
+ if (child = _openapi_all_child_objects[unescaped_reference])
21
+ @find_object_cache[unescaped_reference] = child
22
+ return child
23
+ end
24
+
25
+ _openapi_all_child_objects.values.each do |c|
26
+ if (obj = c.find_object(unescaped_reference))
27
+ @find_object_cache[unescaped_reference] = obj
28
+ return obj
29
+ end
30
+ end
31
+
32
+ nil
33
+ end
34
+
35
+ def purge_object_cache
36
+ @purged = false unless defined? @purged
37
+
38
+ return if @purged
39
+
40
+ @find_object_cache = {}
41
+ @purged = true
42
+
43
+ _openapi_all_child_objects.values.each(&:purge_object_cache)
44
+ end
45
+
46
+ private
47
+
48
+ def find_remote_object(reference)
49
+ uri, fragment = reference.split("#", 2)
50
+ reference_uri = URI(uri)
51
+ reference_uri.fragment = nil
52
+ root.load_another_schema(reference_uri)&.find_object("##{fragment}")
53
+ end
54
+ end
@@ -0,0 +1,29 @@
1
+ module OpenAPIParser::MediaTypeSelectable
2
+ private
3
+
4
+ # select media type by content_type (consider wild card definition)
5
+ # @param [String] content_type
6
+ # @param [Hash{String => OpenAPIParser::Schemas::MediaType}] content
7
+ # @return [OpenAPIParser::Schemas::MediaType, nil]
8
+ def select_media_type_from_content(content_type, content)
9
+ return nil unless content_type
10
+ return nil unless content
11
+
12
+ if (media_type = content[content_type])
13
+ return media_type
14
+ end
15
+
16
+ # application/json => [application, json]
17
+ splited = content_type.split('/')
18
+
19
+ if (media_type = content["#{splited.first}/*"])
20
+ return media_type
21
+ end
22
+
23
+ if (media_type = content['*/*'])
24
+ return media_type
25
+ end
26
+
27
+ nil
28
+ end
29
+ end