Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow references for responses #84

Open
ArthurWD opened this issue May 9, 2016 · 4 comments
Open

Allow references for responses #84

ArthurWD opened this issue May 9, 2016 · 4 comments

Comments

@ArthurWD
Copy link

ArthurWD commented May 9, 2016

I would like to be able to reuse responses by setting references, like this:

"paths": {
  "/path.json": {
    "get": {
      "resource": {
        "401": {
          "$ref": "#/responses/NotLoggedIn"
        }
      }
    }
  },
"responses": {
  "NotLoggedIn": {
    "schema": {
      "$ref": "#/definitions/Error"
    }
  }

Currently, it ignores the reference, letting any value pass. My current attempt is to fix this is by altering Apivore::Swagger.each_response:

method_data.responses.each do |response_code, response_data|
  schema_location = nil
  if response_data.schema
    schema_location = Fragment.new ['#', 'paths', path, verb, 'responses', response_code, 'schema']
  elsif response_data.send(:$ref)
    schema_location = Fragment.new response_data.send(:$ref).split('/').push('schema')
  end
  block.call(path, verb, response_code, schema_location)
end

But this raises "NoMethodError: undefined method `validate' for {"$ref"=>"#/definitions/Error"}:Hash"

  • /.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/json-schema-2.6.1/lib/json-schema/validator.rb:114:in `validate'
  • /.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/json-schema-2.6.1/lib/json-schema/validator.rb:276:in `fully_validate'
  • /.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/apivore-1.6.0/lib/apivore/swagger_checker.rb:26:in `has_matching_document_for'
  • /.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/apivore-1.6.0/lib/apivore/validator.rb:95:in `check_response_is_valid'
  • /.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/apivore-1.6.0/lib/apivore/validator.rb:65:in `post_checks'
  • /.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/apivore-1.6.0/lib/apivore/validator.rb:28:in `matches?'
  • ./spec/requests/api_spec.rb:18:in 'block (3 levels) in <top (required)>'

Any plans for supporting this?

@ashoda
Copy link
Collaborator

ashoda commented Jan 3, 2017

@gwshaw @jreece1567 this seems pretty reasonable. Any reservations about supporting this?

If we're in agreement about supporting this, there is an outstanding PR that we should review and get moving. #89

@jreece1567
Copy link

I'm fine with supporting this. We must not use this capability in our internal westfield swagger, but Apivore should support it as it is part of the public swagger-specification.

cc: @ashoda @gwshaw

@lucasrenan
Copy link

looks like it's more json-schema gem limitation than apivore. when using fragments, json-schema seems not being able to understand the $ref:

https://github.com/ruby-json-schema/json-schema/blob/master/lib/json-schema/validator.rb#L85

anyways, #89 should fix it.

@Deuteu
Copy link

Deuteu commented Jul 27, 2018

@ashoda Any update about this issue ?

trekdemo added a commit to trekdemo/apivore that referenced this issue Oct 21, 2019
trekdemo added a commit to trekdemo/apivore that referenced this issue Oct 22, 2019
trekdemo added a commit to trekdemo/apivore that referenced this issue Oct 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants