civisanalytics / swagger-diff

Utility for comparing two Swagger specifications.
BSD 3-Clause "New" or "Revised" License
264 stars 32 forks source link

Unsupported response $ref #30

Closed botchniaque closed 8 years ago

botchniaque commented 8 years ago

I have a swagger file containing $refs to pre-defined responses. swagger-diff fails with an error:

My resp-ref.yaml file:


---
swagger: '2.0'
info:
  version: 0.0.0
  title: Simple API
paths:
  /:
    get:
      responses:
        202:
          description: OK
        default:
          $ref: "#/responses/apiError"
responses:
  apiError:
    description: "Any erroneous status code: 400 (parse error), 401 (auth error),\
      \ 402 (trial access), 403 (insufficient permissions), 404 (not found), 405 (unsupported\
      \ HTTP method), 415 (unsupported content type, 422 (validation error), 429 (request\
      \ rate limit exceeded), 500 (server error), 503 (maintenance)"
    schema:
      type: 'object'

When calling swagger-diff resp-ref.yaml resp-ref.yaml (I use same file for simplicity) Error I am getting:

/var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/extensions/coercion.rb:46:in `rescue in set_value_with_coercion': Cannot coerce property "responses" from Hash to {String=>Swagger::V2::Response}: The property '$ref' is not defined for Swagger::V2::Response. (Hashie::CoercionError)
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/extensions/coercion.rb:34:in `set_value_with_coercion'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/extensions/indifferent_access.rb:107:in `indifferent_writer'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/dash.rb:168:in `block in initialize_attributes'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/dash.rb:167:in `each_pair'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/dash.rb:167:in `initialize_attributes'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/dash.rb:99:in `initialize'
    from /var/lib/gems/2.1.0/gems/swagger-core-0.2.3/lib/swagger/swagger_object.rb:16:in `initialize'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/extensions/coercion.rb:68:in `new'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/extensions/coercion.rb:68:in `block in coerce_or_init'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/extensions/coercion.rb:35:in `call'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/extensions/coercion.rb:35:in `set_value_with_coercion'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/extensions/indifferent_access.rb:107:in `indifferent_writer'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/dash.rb:168:in `block in initialize_attributes'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/dash.rb:167:in `each_pair'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/dash.rb:167:in `initialize_attributes'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/dash.rb:99:in `initialize'
    from /var/lib/gems/2.1.0/gems/swagger-core-0.2.3/lib/swagger/swagger_object.rb:16:in `initialize'
    from /var/lib/gems/2.1.0/gems/swagger-core-0.2.3/lib/swagger/v2/path.rb:20:in `initialize'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/extensions/coercion.rb:68:in `new'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/extensions/coercion.rb:68:in `block in coerce_or_init'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/extensions/coercion.rb:40:in `call'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/extensions/coercion.rb:40:in `block in set_value_with_coercion'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/extensions/coercion.rb:40:in `each'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/extensions/coercion.rb:40:in `map'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/extensions/coercion.rb:40:in `set_value_with_coercion'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/extensions/indifferent_access.rb:107:in `indifferent_writer'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/dash.rb:168:in `block in initialize_attributes'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/dash.rb:167:in `each_pair'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/dash.rb:167:in `initialize_attributes'
    from /var/lib/gems/2.1.0/gems/hashie-3.3.2/lib/hashie/dash.rb:99:in `initialize'
    from /var/lib/gems/2.1.0/gems/swagger-core-0.2.3/lib/swagger/swagger_object.rb:16:in `initialize'
    from /var/lib/gems/2.1.0/gems/swagger-core-0.2.3/lib/swagger/api.rb:21:in `initialize'
    from /var/lib/gems/2.1.0/gems/swagger-core-0.2.3/lib/swagger/api.rb:9:in `new'
    from /var/lib/gems/2.1.0/gems/swagger-core-0.2.3/lib/swagger/api.rb:9:in `build_api'
    from /var/lib/gems/2.1.0/gems/swagger-core-0.2.3/lib/swagger.rb:24:in `build'
    from /var/lib/gems/2.1.0/gems/swagger-core-0.2.3/lib/swagger.rb:35:in `load'
    from /var/lib/gems/2.1.0/gems/swagger-diff-1.0.5/lib/swagger/diff/specification.rb:54:in `parse_swagger'
    from /var/lib/gems/2.1.0/gems/swagger-diff-1.0.5/lib/swagger/diff/specification.rb:6:in `initialize'
    from /var/lib/gems/2.1.0/gems/swagger-diff-1.0.5/lib/swagger/diff/diff.rb:5:in `new'
    from /var/lib/gems/2.1.0/gems/swagger-diff-1.0.5/lib/swagger/diff/diff.rb:5:in `initialize'
    from /var/lib/gems/2.1.0/gems/swagger-diff-1.0.5/exe/swagger-diff:6:in `new'
    from /var/lib/gems/2.1.0/gems/swagger-diff-1.0.5/exe/swagger-diff:6:in `<top (required)>'
    from /usr/local/bin/swagger-diff:23:in `load'
    from /usr/local/bin/swagger-diff:23:in `<main>'
jeffreyc commented 8 years ago

Thank you for your feedback! It looks like this is an issue with swagger-rb, the underlying library swagger-diff uses to parse Swagger specifications. Unfortunately, we’re juggling a few different priorities at this time, and probably won’t get to this quickly. If you’re interested, we’d welcome a PR addressing this issue. It looks like there are two possible ways to approach this:

  1. File an issue with swagger-rb and wait for them to fix it. However, swagger-rb appears to be inactive, or possibly abandoned, so I don’t anticipate any issue being addressed quickly.
  2. Replace the Swagger parser. When I last looked into this, ruby_swagger looked to be a viable candidate that was under active development, though it's also pretty young, so may not be feature-complete yet. swagger_parser was another candidate. It was less active, though also had no outstanding issues/PRs, so may just be stable. If you wanted to attempt replacing the Swagger parser, it would be worth investigating the current state of parsers, in case someone released a better candidate since I last looked.

If you opt for 2, you may also want to file an issue with swagger-rb so they’re aware and can address the underlying problem if and when they have time.

Either way, we’d be happy to review a PR, and appreciate the contribution!

jeffreyc commented 8 years ago

When looking at #31, I discovered that swagger-rb does implement support for $ref, but does not include it in the response object. So adding another monkey patch to patch.rb and a new spec to specification_spec.rb is also an option here.

jeffreyc commented 8 years ago

I just released Swagger::Diff 1.1.0. Among other improvements, it replaces the underlying parser, and should now support this Swagger specification, too. If you encounter any other problems, please let us know.