Skip to content

Conversation

@Helmsdown
Copy link
Contributor

Using swagger-parser to convert a 1.X resource listing to a Swagger 2.0 is a great feature, but I discovered this small annoyance.

If this is one of my 1.X resource listings (e.g. /api-docs/v2/employees)

{
  "apiVersion": "2.1.2",
  "swaggerVersion": "1.2",
  "basePath": "/",
  "resourcePath": "/v2/employees",
  "produces": ["application/json"],
  "consumes": ["application/json"],
  "apis": [
    {
      "path": "/v2/employees",
      "operations": [
        {
          "method": "GET",
          "summary": "Find all employees for company",
          "notes": "",
          "type": "Employee",
          "nickname": "getEmployees",
          "authorizations": {"privateAuthUser": []},
          "parameters": [],
          "responseMessages": [
            {
              "code": 200,
              "message": "Ok",
              "responseModel": "Employee"
            }
          ] //end of responseMessages
        } //end of operation
      ] //end of operations
    } //end of api
  ] //end of apis
} //end of ApiDeclaration

The following Swagger 2.0 YAML is produced:

swagger: "2.0"
basePath: "/"
schemes:
- "http"
paths:
  /v2/employees:
    get:
      tags:
      - "v2employees"
      summary: "Find all employees for company"
      description: ""
      operationId: "getEmployees"
      parameters:
      responses:
        200:
          description: "success"
          schema:
            $ref: "#/definitions/Employee"
      security:
      - privateAuthUser: []

Notice that the consumes and produces defined at the ApiDeclaration level are not inherited by the Swagger 2.0 operation object.

A few things to note:

  • The Swagger 1.X JSON was generated from a JAX-RS (and Swagger) annotated endpoint
  • It is common practice to define the @produces and @consumes annotations at the Class level
  • This is why the ApiDeclaration object has the content types specified and the actual operations do not
  • Swagger Parser should honor the inherited behavior the same way JAX-RS does.

@Helmsdown
Copy link
Contributor Author

Could someone take a look at this? We would like to stop using an internally published fork of swagger-parser and use the real version published in maven central.

@fehguy
Copy link
Contributor

fehguy commented Jul 16, 2015

Hi @Helmsdown sorry for the delay. Your fixes look great--merging.

fehguy added a commit that referenced this pull request Jul 16, 2015
Operations should inherit consumes and produces from ApiDeclaration object during Swagger 1.0 -> 2.0 conversion
@fehguy fehguy merged commit af8d947 into swagger-api:develop Jul 16, 2015
@webron webron added this to the v1.0.9 milestone Jul 16, 2015
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 this pull request may close these issues.

3 participants