ota42y/openapi_parser

NoMethodError thrown when $ref path cant be resolved

aaroncouture opened this issue ยท 5 comments

When referencing a ref path that cannot be resolved the following error is thrown:

#<NoMethodError: undefined method `any_of' for #OpenAPIParser::Schemas::Reference:0x000055bceb4393f8>

This error should be translated to something that better indicates the root cause.

This is a pretty minor problem since the schema should be validated against OpenApi specs to begin with.

Sample YAML file:

openapi: 3.0.0
paths:
  /api/v1/events:
    get:
      parameters:
        - in: query
          name: start_time
          description: start time
          required: true
          schema:
            $ref: "#/components/schemas/time"

Ok, I will try to validate the OpenAPI definition itself
(Because other definition errors are similarly prevented)

@ota42y I had this problem again. I think if we are validating, and there is a OpenAPIParser::Schemas::Reference object, it means that the reference could not be resolved, and thus maybe a simple raise with the name of the reference would help OAS3 authors find out where their "spec bug" is. I can contribute a PR on this topic if you're favorable to work in that direction.

Something like:

def validator(value, schema)
return any_of_validator if schema.any_of
return all_of_validator if schema.all_of
return one_of_validator if schema.one_of
return nil_validator if value.nil?

    def validator(value, schema)
      raise "#{schema} could not be resolved for validation" unless schema.respond_to(:any_of)

      return any_of_validator if schema.any_of
      return all_of_validator if schema.all_of

I don't think that's the most elegant solution, but effectively something like it.

Thanks to feedback!!

I think we can find this error when we load the schema, so instead of raising an exception at runtime, raising an exception on loading phease like this line.

obj = root.find_object(reference.ref)

@ota42y I have changed my PR from a draft PR. Please confirm if it's acceptable for you ๐Ÿ‘

We have released a version with a fix for this issue! ( Thanks @makdad !!!)
https://rubygems.org/gems/openapi_parser/versions/1.0.0.beta1