westfieldlabs / apivore

Tests your rails API against its Swagger description of end-points, models, and query parameters.
Apache License 2.0
213 stars 66 forks source link

Allow references for responses #84

Open ArthurWD opened 8 years ago

ArthurWD commented 8 years ago

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"

Any plans for supporting this?

ashoda commented 7 years ago

@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. https://github.com/westfieldlabs/apivore/pull/89

jreece1567 commented 7 years ago

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 commented 7 years ago

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, https://github.com/westfieldlabs/apivore/pull/89 should fix it.

Deuteu commented 6 years ago

@ashoda Any update about this issue ?