Search in sources :

Example 1 with Pet

use of io.swagger.v3.jaxrs2.resources.model.Pet in project swagger-parser by swagger-api.

the class FileReferenceTest method testRelativeRefIssue421.

@Test
public void testRelativeRefIssue421() {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = new OpenAPIV3Parser().readLocation("./src/test/resources/main.yaml", null, options);
    assertNotNull(result.getOpenAPI());
    OpenAPI swagger = result.getOpenAPI();
    assertNotNull(swagger);
    assertNotNull(swagger.getPaths().get("pets"));
    assertNotNull(swagger.getPaths().get("pets").getGet());
    assertNotNull(swagger.getPaths().get("pets").getGet().getResponses());
    assertNotNull(swagger.getPaths().get("pets").getGet().getResponses().get("200"));
    assertNotNull(swagger.getPaths().get("pets").getGet().getResponses().get("200").getContent().get("*/*").getSchema());
    assertTrue(swagger.getPaths().get("pets").getGet().getResponses().get("200").getContent().get("*/*").getSchema().get$ref() != null);
    assertEquals(swagger.getPaths().get("pets").getGet().getResponses().get("200").getContent().get("*/*").getSchema().get$ref(), "#/components/schemas/Pet");
    assertTrue(swagger.getComponents().getSchemas().get("Pet") instanceof Schema);
    assertTrue(swagger.getComponents().getSchemas().get("Pet").getProperties().size() == 2);
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 2 with Pet

use of io.swagger.v3.jaxrs2.resources.model.Pet in project swagger-parser by swagger-api.

the class FileReferenceTest method testIssue421.

@Test
public void testIssue421() {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = new OpenAPIV3Parser().readLocation("./src/test/resources/nested-file-references/issue-421.yaml", null, options);
    assertNotNull(result.getOpenAPI());
    OpenAPI swagger = result.getOpenAPI();
    assertNotNull(swagger.getPaths().get("/pet/{petId}"));
    assertNotNull(swagger.getPaths().get("/pet/{petId}").getGet());
    assertNotNull(swagger.getPaths().get("/pet/{petId}").getGet().getParameters());
    assertTrue(swagger.getPaths().get("/pet/{petId}").getGet().getParameters().size() == 1);
    assertTrue(swagger.getPaths().get("/pet/{petId}").getGet().getParameters().get(0).getName().equals("petId"));
    assertTrue(swagger.getComponents().getSchemas().get("Pet") instanceof Schema);
    assertTrue(swagger.getComponents().getSchemas().get("Pet").getProperties().size() == 6);
    assertNotNull(swagger.getPaths().get("/pet/{petId}").getPost());
    assertNotNull(swagger.getPaths().get("/pet/{petId}").getPost().getParameters());
    assertTrue(swagger.getPaths().get("/pet/{petId}").getPost().getParameters().size() == 1);
    assertTrue(swagger.getPaths().get("/pet/{petId}").getPost().getRequestBody() != null);
    assertTrue(swagger.getPaths().get("/pet/{petId}").getPost().getRequestBody().get$ref() != null);
    assertEquals(swagger.getPaths().get("/pet/{petId}").getPost().getRequestBody().get$ref(), "#/components/requestBodies/requestBody");
    assertTrue(swagger.getPaths().get("/pet/{petId}").getPost().getRequestBody().get$ref().equals("#/components/requestBodies/requestBody"));
    assertNotNull(swagger.getPaths().get("/store/order"));
    assertNotNull(swagger.getPaths().get("/store/order").getPost());
    assertNotNull(swagger.getPaths().get("/store/order").getPost().getRequestBody());
    assertNotNull(swagger.getPaths().get("/store/order").getPost().getRequestBody().getContent().get("application/json").getSchema());
    assertTrue(swagger.getPaths().get("/store/order").getPost().getRequestBody().getContent().get("application/json").getSchema().get$ref() != null);
    assertTrue(swagger.getPaths().get("/store/order").getPost().getRequestBody().getContent().get("application/json").getSchema().get$ref().equals("#/components/schemas/Order"));
    assertTrue(swagger.getComponents().getSchemas().get("Order") instanceof Schema);
    assertTrue(swagger.getComponents().getSchemas().get("Order").getProperties().size() == 6);
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 3 with Pet

use of io.swagger.v3.jaxrs2.resources.model.Pet in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testIssue1157.

@Test
public void testIssue1157(@Injectable final List<AuthorizationValue> auths) {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveFully(true);
    OpenAPI openAPIAnyOf = new OpenAPIV3Parser().readLocation("/issue-1157/anyOf-example.yaml", auths, options).getOpenAPI();
    Schema petSchemaAnyOf = openAPIAnyOf.getComponents().getSchemas().get("Pet");
    assertTrue(petSchemaAnyOf instanceof ComposedSchema);
    assertTrue(((ComposedSchema) petSchemaAnyOf).getAnyOf() != null);
    OpenAPI openAPIOneOf = new OpenAPIV3Parser().readLocation("/issue-1157/oneOf-example.yaml", auths, options).getOpenAPI();
    Schema petSchemaOneOf = openAPIOneOf.getComponents().getSchemas().get("Pet");
    assertTrue(petSchemaOneOf instanceof ComposedSchema);
    assertTrue(((ComposedSchema) petSchemaOneOf).getOneOf() != null);
    OpenAPI openAPIAllOf = new OpenAPIV3Parser().readLocation("/issue-1157/allOf-example.yaml", auths, options).getOpenAPI();
    Schema petSchemaAllOf = openAPIAllOf.getComponents().getSchemas().get("Pet");
    assertFalse(petSchemaAllOf instanceof ComposedSchema);
    assertTrue(petSchemaAllOf.getProperties() != null);
}
Also used : ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) 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) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) 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 4 with Pet

use of io.swagger.v3.jaxrs2.resources.model.Pet in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testOneOfSchema.

@Test
public void testOneOfSchema(@Injectable List<AuthorizationValue> auths) {
    String yaml = "openapi: '3.0'\n" + "components:\n" + "  schemas:\n" + "    Cat:\n" + "      type: object\n" + "      # all properties specific to a `Cat`\n" + "      properties:\n" + "        purring:\n" + "          type: string\n" + "    Dog:\n" + "      type: object\n" + "      # all properties specific to a `Dog`\n" + "      properties:\n" + "        bark:\n" + "          type: string\n" + "    Pet:\n" + "      oneOf: \n" + "       - $ref: '#/components/schemas/Cat'\n" + "       - $ref: '#/components/schemas/Dog'\n" + "       - type: object\n" + "         # neither a `Cat` nor a `Dog`\n" + "         properties:\n" + "           name:\n" + "             type: string\n";
    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);
    Schema petSchema = result.getOpenAPI().getComponents().getSchemas().get("Pet");
    assertTrue(petSchema != null);
    assertTrue(petSchema instanceof ComposedSchema);
    ComposedSchema petCompSchema = (ComposedSchema) petSchema;
    List<Schema> oneOfSchemas = petCompSchema.getOneOf();
    assertTrue(oneOfSchemas != null);
    assertEquals(oneOfSchemas.size(), 3);
    Schema refCatSchema = oneOfSchemas.get(0);
    assertTrue(refCatSchema != null);
    assertEquals(refCatSchema.get$ref(), "#/components/schemas/Cat");
    Schema refDogSchema = oneOfSchemas.get(1);
    assertTrue(refDogSchema != null);
    assertEquals(refDogSchema.get$ref(), "#/components/schemas/Dog");
    Schema otherSchema = oneOfSchemas.get(2);
    assertTrue(otherSchema != null);
    Schema nameProp = (Schema) otherSchema.getProperties().get("name");
    assertTrue(nameProp != null);
    assertEquals(nameProp.getType(), "string");
}
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) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 5 with Pet

use of io.swagger.v3.jaxrs2.resources.model.Pet in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testIssue204_allOf.

@Test
public void testIssue204_allOf() throws Exception {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + "  version: 2.0.0\n" + "  title: Test allOf API\n" + "  description: 'Tests the allOf API for parent, interface and child models.'\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" + "    Furry:\n" + "      type: object\n" + "      required:\n" + "        - coatColour\n" + "      properties:\n" + "        coatColour:\n" + "          type: string\n" + "    Dog:\n" + "      type: object\n" + "      allOf:\n" + "        - $ref: '#/components/schemas/Pet'\n" + "        - $ref: '#/components/schemas/Furry'\n" + "        - required:\n" + "            - name\n" + "          properties:\n" + "            name:\n" + "              type: string";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    assertTrue(result.getMessages().isEmpty());
    OpenAPI openAPI = result.getOpenAPI();
    assertNotNull(openAPI);
    Map<String, Schema> definitions = openAPI.getComponents().getSchemas();
    assertNotNull(definitions);
    assertEquals(3, definitions.size());
    Schema pet = definitions.get("Pet");
    Schema furry = definitions.get("Furry");
    Schema dog = definitions.get("Dog");
    assertNotNull(pet);
    assertNotNull(furry);
    assertNotNull(dog);
    assertTrue(dog instanceof ComposedSchema);
    ComposedSchema dogComposed = (ComposedSchema) dog;
    assertNotNull(dogComposed.getAllOf());
    assertEquals(3, dogComposed.getAllOf().size());
    Schema dogInterfaceRef = dogComposed.getAllOf().get(0);
    Schema dogInterface = definitions.get(dogInterfaceRef.get$ref());
    dogInterfaceRef = dogComposed.getAllOf().get(1);
    dogInterface = definitions.get(dogInterfaceRef.get$ref());
    assertTrue(dogComposed.getAllOf().get(0).get$ref() != null);
}
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)

Aggregations

Test (org.testng.annotations.Test)69 OpenAPI (io.swagger.v3.oas.models.OpenAPI)60 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)37 Schema (io.swagger.v3.oas.models.media.Schema)32 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)28 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)28 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)24 StringSchema (io.swagger.v3.oas.models.media.StringSchema)24 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)20 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)17 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)17 PathItem (io.swagger.v3.oas.models.PathItem)16 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)15 MapSchema (io.swagger.v3.oas.models.media.MapSchema)12 DateSchema (io.swagger.v3.oas.models.media.DateSchema)10 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)10 ArrayList (java.util.ArrayList)10 Operation (io.swagger.v3.oas.models.Operation)9 Paths (io.swagger.v3.oas.models.Paths)9 Parameter (io.swagger.v3.oas.models.parameters.Parameter)8