Search in sources :

Example 31 with OpenAPIV3Parser

use of io.swagger.v3.parser.OpenAPIV3Parser in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testPathsWithRefResponse.

@Test
public void testPathsWithRefResponse() {
    String json = "{\n" + "  \"openapi\": \"3.0.0\",\n" + "  \"paths\": {\n" + "    \"/pet\": {\n" + "      \"get\": {\n" + "        \"responses\": {\n" + "          \"200\": {\n" + "            \"$ref\": \"#/components/responses/OK\"" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(json, null, null);
    OpenAPI openAPI = result.getOpenAPI();
    PathItem path = openAPI.getPaths().get("/pet");
    assertNotNull(path);
    Operation operation = path.getGet();
    assertNotNull(operation);
    assertTrue(operation.getResponses().containsKey("200"));
    assertEquals(ApiResponse.class, operation.getResponses().get("200").getClass());
    ApiResponse refResponse = operation.getResponses().get("200");
    assertEquals("#/components/responses/OK", refResponse.get$ref());
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Operation(io.swagger.v3.oas.models.Operation) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 32 with OpenAPIV3Parser

use of io.swagger.v3.parser.OpenAPIV3Parser in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testIssue151.

@Test
public void testIssue151() throws Exception {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + "  version: 2.0.0\n" + "  title: Test Issue 151\n" + "  description: Tests that ComposedSchema vendor extensions are deserialized correctly.\n" + "paths:\n" + "  /:\n" + "    get:\n" + "      responses:\n" + "        '200':\n" + "          description: OK\n" + "components:\n" + "  schemas:\n" + "    Pet:\n" + "      type: object\n" + "      required:\n" + "        - id\n" + "      properties:\n" + "        id:\n" + "          type: integer\n" + "          format: int64\n" + "    Dog:\n" + "      type: object\n" + "      allOf:\n" + "        - $ref: '#/components/schemas/Pet'\n" + "        - required:\n" + "            - name\n" + "          properties:\n" + "            name:\n" + "              type: string\n" + "      x-vendor-ext: some data";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    assertTrue(result.getMessages().isEmpty());
    OpenAPI openAPI = result.getOpenAPI();
    Map<String, Schema> definitions = openAPI.getComponents().getSchemas();
    assertTrue(definitions.size() == 2);
    Schema allOfModel = definitions.get("Dog");
    assertTrue(allOfModel instanceof ComposedSchema);
    assertFalse(allOfModel.getExtensions().isEmpty());
    assertEquals("some data", allOfModel.getExtensions().get("x-vendor-ext"));
}
Also used : DateSchema(io.swagger.v3.oas.models.media.DateSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) BinarySchema(io.swagger.v3.oas.models.media.BinarySchema) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 33 with OpenAPIV3Parser

use of io.swagger.v3.parser.OpenAPIV3Parser in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testSecurityDefinition.

@Test
public void testSecurityDefinition() {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "paths:\n" + "  /pet:\n" + "    get:\n" + "      security:\n" + "        - basic_auth: []\n" + "          api_key: []\n" + "      responses:\n" + "        default:\n" + "          description: Default response\n" + "info:\n" + "  version: ''\n" + "  title: ''\n" + "components:\n" + "  securitySchemes:\n" + "    basic_auth:\n" + "      type: http\n" + "      x-foo: basicBar\n" + "      scheme: basic\n" + "    api_key:\n" + "      type: apiKey\n" + "      name: api_key\n" + "      in: header\n" + "      description: api key description\n" + "      x-foo: apiKeyBar";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    OpenAPI openAPI = result.getOpenAPI();
    assertNotNull(openAPI.getComponents().getSecuritySchemes());
    assertTrue(openAPI.getComponents().getSecuritySchemes().keySet().size() == 2);
    // Basic Authentication
    SecurityScheme definitionBasic = openAPI.getComponents().getSecuritySchemes().get("basic_auth");
    assertNotNull(definitionBasic);
    assertEquals(definitionBasic.getType(), SecurityScheme.Type.HTTP);
    assertEquals(definitionBasic.getExtensions().get("x-foo"), "basicBar");
    // API Key Authentication
    SecurityScheme definition = openAPI.getComponents().getSecuritySchemes().get("api_key");
    assertNotNull(definition);
    assertEquals(definition.getType(), SecurityScheme.Type.APIKEY);
    SecurityScheme apiKey = definition;
    assertEquals(apiKey.getName(), "api_key");
    assertEquals(apiKey.getIn(), SecurityScheme.In.HEADER);
    assertEquals(apiKey.getDescription(), "api key description");
    assertEquals(apiKey.getExtensions().get("x-foo"), "apiKeyBar");
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) Test(org.testng.annotations.Test)

Example 34 with OpenAPIV3Parser

use of io.swagger.v3.parser.OpenAPIV3Parser in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testAlmostEmpty.

@Test
public void testAlmostEmpty(@Injectable List<AuthorizationValue> auths) {
    String yaml = "openapi: '3.0.1'\n" + "new: extra";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = parser.readContents(yaml, auths, options);
    List<String> messageList = result.getMessages();
    Set<String> messages = new HashSet<>(messageList);
    assertTrue(messages.contains("attribute info is missing"));
    assertTrue(messages.contains("attribute paths is missing"));
    assertTrue(messages.contains("attribute new is unexpected"));
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 35 with OpenAPIV3Parser

use of io.swagger.v3.parser.OpenAPIV3Parser in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testContact.

@Test
public void testContact() {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + "  title: title\n" + "  bad: bad\n" + "  x-foo: bar\n" + "  description: description\n" + "  termsOfService: tos\n" + "  contact:\n" + "    name: tony\n" + "    url: url\n" + "    email: email\n" + "    invalid: invalid\n" + "    x-fun: true\n" + "  version: version\n" + "paths: {}";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    List<String> messageList = result.getMessages();
    Set<String> messages = new HashSet<>(messageList);
    assertEquals(result.getOpenAPI().getInfo().getTitle(), "title");
    assertEquals(result.getOpenAPI().getInfo().getDescription(), "description");
    assertEquals(result.getOpenAPI().getInfo().getTermsOfService(), "tos");
    assertEquals(result.getOpenAPI().getInfo().getVersion(), "version");
    Contact contact = result.getOpenAPI().getInfo().getContact();
    assertEquals(contact.getName(), "tony");
    assertEquals(contact.getUrl(), "url");
    assertEquals(contact.getEmail(), "email");
    assertTrue(messages.contains("attribute info.bad is unexpected"));
    assertTrue(messages.contains("attribute info.contact.invalid is unexpected"));
    assertEquals(result.getOpenAPI().getInfo().getExtensions().get("x-foo").toString(), "bar");
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) HashSet(java.util.HashSet) Contact(io.swagger.v3.oas.models.info.Contact) Test(org.testng.annotations.Test)

Aggregations

OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)295 Test (org.testng.annotations.Test)279 OpenAPI (io.swagger.v3.oas.models.OpenAPI)219 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)184 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)169 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)83 Schema (io.swagger.v3.oas.models.media.Schema)83 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)80 StringSchema (io.swagger.v3.oas.models.media.StringSchema)77 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)76 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)75 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)66 MapSchema (io.swagger.v3.oas.models.media.MapSchema)63 BinarySchema (io.swagger.v3.oas.models.media.BinarySchema)25 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)25 DateSchema (io.swagger.v3.oas.models.media.DateSchema)24 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)24 Parameter (io.swagger.v3.oas.models.parameters.Parameter)23 HashSet (java.util.HashSet)22 PathParameter (io.swagger.v3.oas.models.parameters.PathParameter)17