swagger-api / swagger-parser

Swagger Spec to Java POJOs
http://swagger.io
Apache License 2.0
782 stars 528 forks source link

Get API response schema from an oas2 file #1362

Open arunvangapelli opened 4 years ago

arunvangapelli commented 4 years ago

Below is the spec which I'm trying to parse.

  "swagger": "2.0",
  "info": {
    "title": "Test API",
    "version": "1.0",
    "description": "test",
    "contact": {
      "name": "Test",
      "url": "https://www.example.com/"
    }
  },
  "host": "example.com",
  "paths": {
    "/todos": {
      "get": {
        "responses": {
          "200": {
            "description": "",
            "schema": {
              "type": "object",
              "properties": {
                "id": {
                  "type": "string"
                },
                "name": {
                  "type": "string"
                },
                "dob": {
                  "type": "string"
                },
                "internalObject": {
                  "$ref": "#/definitions/test-model"
                }
              },
              "required": [
                "id",
                "name"
              ]
            }
          }
        },
        "summary": "Todos API summary",
        "description": "Todos description",
        "operationId": "getTodos",
        "tags": [
          "Todo"
        ]
      }
    }
  },
  "tags": [
    {
      "name": "Todo"
    }
  ],
  "schemes": [
    "http"
  ],
  "definitions": {
    "test-model": {
      "type": "object",
      "title": "Test Model",
      "properties": {
        "field1": {
          "type": "string"
        },
        "field2": {
          "type": "string"
        }
      }
    }
  },
  "responses": {
    "todos-response": {
      "description": "todos response object",
      "schema": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "dob": {
            "type": "string"
          },
          "internalObject": {
            "$ref": "#/definitions/test-model"
          }
        },
        "required": [
          "id",
          "name"
        ]
      }
    }
  }
}

The library I'm using:

implementation 'io.swagger.parser.v3:swagger-parser:2.0.19'

I have the spec as a String. So, my code is like below:

OpenAPIParser openApiParser = new OpenAPIParser();
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setFlatten(true);
SwaggerParseResult sr = openApiParser.readContents(spec, null, options);    

I have tried to print the schema in couple of ways, but I didn't get the schema as I expected.

Option 1:

Map<String, io.swagger.v3.oas.models.media.MediaType> map = apiResponse.getContent();
   for (String key : map.keySet()) {
    System.out.println("schema = " + map.get(key).getSchema().toString());
       }

Option 2:

ApiResponse apiResponse1 = sr.getOpenAPI().getComponents().getResponses().get("todos-response");

Map<String, io.swagger.v3.oas.models.media.MediaType> responsesMap = apiResponse1.getContent();
for (String key : responsesMap.keySet()) {
  System.out.println("response schema = " + responsesMap.get(key).getSchema().toString());
}

I'm trying to get schema in this format so that I can use it to validate against my API response.

{
  "type": "object",
  "properties": {
    "id": {
      "type": "string"
    },
    "name": {
      "type": "string"
    },
    "dob": {
      "type": "string"
    },
    "internalObject": {
      "type": "object",
      "title": "Test Model",
      "properties": {
        "field1": {
          "type": "string"
        },
        "field2": {
          "type": "string"
        }
      }
    }
  },
  "required": [
    "id",
    "name"
  ]
}

Please let me know, if I need to try anything else or other parsers. Thanks.

arunvangapelli commented 4 years ago

The results I got for both options Option 1:

class Schema {
    type: null
    format: null
    $ref: #/components/schemas/inline_response_200
    description: null
    title: null
    multipleOf: null
    maximum: null
    exclusiveMaximum: null
    minimum: null
    exclusiveMinimum: null
    maxLength: null
    minLength: null
    pattern: null
    maxItems: null
    minItems: null
    uniqueItems: null
    maxProperties: null
    minProperties: null
    required: null
    not: null
    properties: null
    additionalProperties: null
    nullable: null
    readOnly: null
    writeOnly: null
    example: null
    externalDocs: null
    deprecated: null
    discriminator: null
    xml: null
}

Option 2:

class ObjectSchema {
    class Schema {
        type: object
        format: null
        $ref: null
        description: null
        title: null
        multipleOf: null
        maximum: null
        exclusiveMaximum: null
        minimum: null
        exclusiveMinimum: null
        maxLength: null
        minLength: null
        pattern: null
        maxItems: null
        minItems: null
        uniqueItems: null
        maxProperties: null
        minProperties: null
        required: [id, name]
        not: null
        properties: {id=class StringSchema {
            class Schema {
                type: string
                format: null
                $ref: null
                description: null
                title: null
                multipleOf: null
                maximum: null
                exclusiveMaximum: null
                minimum: null
                exclusiveMinimum: null
                maxLength: null
                minLength: null
                pattern: null
                maxItems: null
                minItems: null
                uniqueItems: null
                maxProperties: null
                minProperties: null
                required: null
                not: null
                properties: null
                additionalProperties: null
                nullable: null
                readOnly: null
                writeOnly: null
                example: null
                externalDocs: null
                deprecated: null
                discriminator: null
                xml: null
            }
        }, name=class StringSchema {
            class Schema {
                type: string
                format: null
                $ref: null
                description: null
                title: null
                multipleOf: null
                maximum: null
                exclusiveMaximum: null
                minimum: null
                exclusiveMinimum: null
                maxLength: null
                minLength: null
                pattern: null
                maxItems: null
                minItems: null
                uniqueItems: null
                maxProperties: null
                minProperties: null
                required: null
                not: null
                properties: null
                additionalProperties: null
                nullable: null
                readOnly: null
                writeOnly: null
                example: null
                externalDocs: null
                deprecated: null
                discriminator: null
                xml: null
            }
        }, dob=class StringSchema {
            class Schema {
                type: string
                format: null
                $ref: null
                description: null
                title: null
                multipleOf: null
                maximum: null
                exclusiveMaximum: null
                minimum: null
                exclusiveMinimum: null
                maxLength: null
                minLength: null
                pattern: null
                maxItems: null
                minItems: null
                uniqueItems: null
                maxProperties: null
                minProperties: null
                required: null
                not: null
                properties: null
                additionalProperties: null
                nullable: null
                readOnly: null
                writeOnly: null
                example: null
                externalDocs: null
                deprecated: null
                discriminator: null
                xml: null
            }
        }, internalObject=class Schema {
            type: null
            format: null
            $ref: #/components/schemas/test-model
            description: null
            title: null
            multipleOf: null
            maximum: null
            exclusiveMaximum: null
            minimum: null
            exclusiveMinimum: null
            maxLength: null
            minLength: null
            pattern: null
            maxItems: null
            minItems: null
            uniqueItems: null
            maxProperties: null
            minProperties: null
            required: null
            not: null
            properties: null
            additionalProperties: null
            nullable: null
            readOnly: null
            writeOnly: null
            example: null
            externalDocs: null
            deprecated: null
            discriminator: null
            xml: null
        }}
        additionalProperties: null
        nullable: null
        readOnly: null
        writeOnly: null
        example: null
        externalDocs: null
        deprecated: null
        discriminator: null
        xml: null
    }
}
gracekarina commented 4 years ago

Hi @arunvangapelli please try: OpenAPIParser openApiParser = new OpenAPIParser(); ParseOptions options = new ParseOptions(); options.setResolve(true); options.setResolveFully(true); SwaggerParseResult sr = openApiParser.readContents(spec, null, options);

arunvangapelli commented 4 years ago

Hi @gracekarina Sorry for late response. I tried that but I still didn't get the fully resolved schema. When trying to get the schema anything else I need to try other than what I'm doing in option 1 or 2?