openapi_parser icon indicating copy to clipboard operation
openapi_parser copied to clipboard

Failed to use `$ref` reference to whole file

Open yckimura opened this issue 2 years ago • 2 comments

When using $ref to reference to whole yaml file, NoMethodError is thrown.

RSpec example that can reproduce error: https://github.com/ota42y/openapi_parser/compare/master...yckimura:ref-whole-file Failed logs: https://github.com/yckimura/openapi_parser/runs/5725197901

openapi.yaml:

openapi: 3.0.0
info:
  version: 1.0.0
  title: OpenAPI3 Test
paths:
  /whole_local_file:
    post:
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: schema-object.yaml
      responses:
        '200':
          description: correct

schema-object.yaml:

type: object
required:
  - name
properties:
  name:
    type: string
  tag:
    type: string

Stacktrace:

NoMethodError:
  undefined method `any_of' for #
# ./lib/openapi_parser/schema_validator.rb:98:in `validator'
# ./lib/openapi_parser/schema_validator.rb:74:in `validate_schema'
# ./lib/openapi_parser/schema_validator.rb:62:in `validate_data'
# ./lib/openapi_parser/schema_validator.rb:45:in `validate'
# ./lib/openapi_parser/schemas/media_type.rb:15:in `validate_parameter'
# ./lib/openapi_parser/schemas/request_body.rb:24:in `validate_request_body'
# ./lib/openapi_parser/schemas/operation.rb:25:in `validate_request_body'
# ./lib/openapi_parser/request_operation.rb:52:in `validate_request_body'
# ./spec/openapi_parser/concerns/findable_spec.rb:40:in `block (4 levels) in <top (required)>'

yckimura avatar Mar 28 '22 18:03 yckimura

I found that reference parser inherits owner's schema.

https://github.com/ota42y/openapi_parser/blob/1e036a7b37235fce3315e2b400d5cebe4e41c1f5/lib/openapi_parser/concerns/parser.rb#L41

On this situation, schema-object.yaml should have paths components on top level. other props will be ignored.

kuboon avatar Apr 14 '22 05:04 kuboon

Basically it's a use case described in the OpenAPI docs so it'd be nice to have it 👍

paths:
  /users:
    $ref: '../resources/users.yaml'
  /users/{userId}:
    $ref: '../resources/users-by-id.yaml'

nerixim avatar May 21 '23 06:05 nerixim