diff --git a/CHANGELOG.md b/CHANGELOG.md index ec66e03d..f8d551ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Accept loading OAD documents with numeric status codes. Fixes "Unknown reference" error. https://github.com/ahx/openapi_first/issues/367 + - Support QUERY request method OpenAPI 3.0, 3.1 does not support that, but this does diff --git a/lib/openapi_first.rb b/lib/openapi_first.rb index 07fb5405..3989cc1d 100644 --- a/lib/openapi_first.rb +++ b/lib/openapi_first.rb @@ -64,8 +64,9 @@ def self.load(filepath_or_definition, only: nil, &) # Parse a dereferenced Hash # @return [Definition] + # TODO: This needs to work with unresolved contents as well def self.parse(contents, only: nil, filepath: nil, &) - # TODO: This needs to work with unresolved contents as well + contents = JSON.parse(JSON.generate(contents)) # Deeply stringify keys, because of YAML. See https://github.com/ahx/openapi_first/issues/367 contents['paths'].filter!(&->(key, _) { only.call(key) }) if only Definition.new(contents, filepath, &) end diff --git a/lib/openapi_first/file_loader.rb b/lib/openapi_first/file_loader.rb index 531035c2..ab93e2f6 100644 --- a/lib/openapi_first/file_loader.rb +++ b/lib/openapi_first/file_loader.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require 'json' require 'yaml' module OpenapiFirst diff --git a/spec/data/numeric-status.yaml b/spec/data/numeric-status.yaml new file mode 100644 index 00000000..a9d5f1f5 --- /dev/null +++ b/spec/data/numeric-status.yaml @@ -0,0 +1,27 @@ +openapi: 3.0.2 +info: + title: Acme Control API + version: 0.0.1 +paths: + /roles: + get: + responses: + 200: + content: + application/json: + schema: + type: object + /roles/query: + post: + requestBody: + required: true + content: + application/json: + schema: + type: object + responses: + 200: + content: + application/json: + schema: + $ref: '#/paths/~1roles/get/responses/200/content/application~1json/schema' diff --git a/spec/openapi_first_spec.rb b/spec/openapi_first_spec.rb index 92feca36..d0ba9b21 100644 --- a/spec/openapi_first_spec.rb +++ b/spec/openapi_first_spec.rb @@ -47,6 +47,11 @@ expect(definition.paths).to include('/foo') end + it 'works with numeric statuses' do + definition = OpenapiFirst.load('./spec/data/numeric-status.yaml') + expect(definition.paths).to include('/roles') + end + it 'works with YAML' do definition = OpenapiFirst.load('./spec/data/petstore.yaml') expect(definition.paths).to include('/pets')