swagger-api / swagger-api/swagger-parser

Get API response schema from an oas2 file

Open
#1,362 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
867
Forks
560
Avg merge
2d 21h
Merged PRs (30d)
7

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with OpenAPIParser.readContents using the provided OAS2 string and inspect the parsed operation response rather than only the components response map. Compare the resolved response schema and its $ref to the requested nested JSON shape; done means documenting or implementing a supported way to obtain that schema for response validation, with an appropriate parser test if the repository provides one.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.