Search in sources :

Example 26 with Pet

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

the class OpenAPIV3ParserTest method testExampleFlag.

@Test
public void testExampleFlag() {
    OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveCombinators(true);
    options.setResolveFully(true);
    options.setFlatten(true);
    SwaggerParseResult parseResult = openApiParser.readLocation("media-type-null-example.yaml", null, options);
    OpenAPI openAPI = parseResult.getOpenAPI();
    assertNull(openAPI.getPaths().get("/pets/{petId}").getGet().getResponses().get("200").getContent().get("application/json").getExample());
    assertTrue(openAPI.getPaths().get("/pets/{petId}").getGet().getResponses().get("200").getContent().get("application/json").getExampleSetFlag());
    assertNull(openAPI.getPaths().get("/pet").getPost().getResponses().get("200").getContent().get("application/json").getExample());
    assertFalse(openAPI.getPaths().get("/pet").getPost().getResponses().get("200").getContent().get("application/json").getExampleSetFlag());
    assertNotNull(openAPI.getPaths().get("/pet").getPost().getRequestBody().getContent().get("application/json").getExample());
    assertNotNull(openAPI.getPaths().get("/pet").getPost().getRequestBody().getContent().get("application/json").getExample());
    assertTrue(openAPI.getPaths().get("/pet").getPost().getRequestBody().getContent().get("application/json").getExampleSetFlag());
    assertNotNull(openAPI.getPaths().get("/object-with-null-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("foo").getValue());
    assertTrue(openAPI.getPaths().get("/object-with-null-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("foo").getValueSetFlag());
    assertNull(openAPI.getPaths().get("/object-with-null-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("bar").getValue());
    assertTrue(openAPI.getPaths().get("/object-with-null-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("bar").getValueSetFlag());
    assertNotNull(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("a").getValue());
    assertTrue(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("a").getValueSetFlag());
    assertNotNull(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("b").getValue());
    assertTrue(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("b").getValueSetFlag());
    assertNotNull(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("c").getValue());
    assertTrue(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("c").getValueSetFlag());
    assertNull(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("d").getValue());
    assertTrue(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("d").getValueSetFlag());
    assertNull(openAPI.getComponents().getSchemas().get("ObjectWithNullExample").getExample());
    assertTrue(openAPI.getComponents().getSchemas().get("ObjectWithNullExample").getExampleSetFlag());
    assertNotNull(openAPI.getComponents().getSchemas().get("ObjectWithNullInSchemaExample").getExample());
    assertTrue(openAPI.getComponents().getSchemas().get("ObjectWithNullInSchemaExample").getExampleSetFlag());
    assertNotNull(((Schema) openAPI.getComponents().getSchemas().get("ObjectWithNullPropertyExample").getProperties().get("a")).getExample());
    assertTrue(((Schema) openAPI.getComponents().getSchemas().get("ObjectWithNullPropertyExample").getProperties().get("a")).getExampleSetFlag());
    assertNull(((Schema) openAPI.getComponents().getSchemas().get("ObjectWithNullPropertyExample").getProperties().get("b")).getExample());
    assertTrue(((Schema) openAPI.getComponents().getSchemas().get("ObjectWithNullPropertyExample").getProperties().get("b")).getExampleSetFlag());
    assertNull(openAPI.getComponents().getSchemas().get("StringWithNullExample").getExample());
    assertTrue(openAPI.getComponents().getSchemas().get("StringWithNullExample").getExampleSetFlag());
    assertNull(openAPI.getComponents().getSchemas().get("ArrayWithNullArrayExample").getExample());
    assertTrue(openAPI.getComponents().getSchemas().get("ArrayWithNullArrayExample").getExampleSetFlag());
    assertNull(((ArraySchema) openAPI.getComponents().getSchemas().get("ArrayWithNullItemExample")).getItems().getExample());
    assertTrue(((ArraySchema) openAPI.getComponents().getSchemas().get("ArrayWithNullItemExample")).getItems().getExampleSetFlag());
}
Also used : ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) 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 27 with Pet

use of io.swagger.v3.jaxrs2.resources.model.Pet in project vertx-web by vert-x3.

the class OpenAPI3ValidationTest method testAdditionalPropertiesJson.

@Test
public void testAdditionalPropertiesJson() throws Exception {
    Operation op = testSpec.getPaths().get("/additionalProperties").getPost();
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
    loadHandlers("/additionalProperties", HttpMethod.POST, true, validationHandler, (routingContext) -> {
        RequestParameters params = routingContext.get("parsedParameters");
        routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("Content-Type", "application/json").end(params.body().getJsonObject().encode());
    });
    JsonObject pet = new JsonObject();
    pet.put("id", 14612);
    pet.put("name", "Willy");
    pet.put("lazyness", "Highest");
    testRequestWithJSON(HttpMethod.POST, "/additionalProperties", pet, 400, errorMessage(ValidationException.ErrorType.JSON_INVALID));
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) JsonObject(io.vertx.core.json.JsonObject) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 28 with Pet

use of io.swagger.v3.jaxrs2.resources.model.Pet in project vertx-web by vert-x3.

the class OpenAPI3ValidationTest method testComplexMultipart.

@Test
public void testComplexMultipart() throws Exception {
    Operation op = testSpec.getPaths().get("/multipart/complex").getPost();
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
    loadHandlers("/multipart/complex", HttpMethod.POST, false, validationHandler, (routingContext) -> {
        RequestParameters params = routingContext.get("parsedParameters");
        assertEquals(params.formParameter("param1").getString(), "sampleString");
        assertNotNull(params.formParameter("param2").getJsonObject());
        assertEquals(params.formParameter("param2").getJsonObject().getString("name"), "Willy");
        assertEquals(params.formParameter("param4").getArray().size(), 4);
        routingContext.response().setStatusMessage("ok").end();
    });
    MultiMap form = MultiMap.caseInsensitiveMultiMap();
    form.add("param1", "sampleString");
    JsonObject pet = new JsonObject();
    pet.put("id", 14612);
    pet.put("name", "Willy");
    form.add("param2", pet.encode());
    form.add("param3", "SELECT * FROM table;");
    List<String> valuesArray = new ArrayList<>();
    for (int i = 0; i < 4; i++) valuesArray.add(getSuccessSample(ParameterType.FLOAT).getFloat().toString());
    form.add("param4", serializeInCSVStringArray(valuesArray));
    testRequestWithForm(HttpMethod.POST, "/multipart/complex", FormType.MULTIPART, form, 200, "ok");
}
Also used : MultiMap(io.vertx.core.MultiMap) OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 29 with Pet

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

the class JsonDeserializationTest method testCompositionTest.

@Test(description = "it should deserialize the composition test")
public void testCompositionTest() throws IOException {
    final String json = ResourceUtils.loadClassResource(getClass(), "specFiles/compositionTest-3.0.json");
    final Object deserialized = m.readValue(json, OpenAPI.class);
    assertTrue(deserialized instanceof OpenAPI);
    OpenAPI openAPI = (OpenAPI) deserialized;
    Schema lizardSchema = openAPI.getComponents().getSchemas().get("Lizard");
    assertTrue(lizardSchema instanceof ComposedSchema);
    assertEquals(((ComposedSchema) lizardSchema).getAllOf().size(), 2);
    Schema petSchema = openAPI.getComponents().getSchemas().get("Pet");
    assertEquals(petSchema.getDiscriminator().getPropertyName(), "pet_type");
    assertEquals(petSchema.getDiscriminator().getMapping().get("cachorro"), "#/components/schemas/Dog");
}
Also used : ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 30 with Pet

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

the class JsonDeserializationTest method testDeserializeAPathRef.

@Test
public void testDeserializeAPathRef() throws Exception {
    final OpenAPI oas = TestUtils.deserializeJsonFileFromClasspath("specFiles/pathRef.json", OpenAPI.class);
    final PathItem petPath = oas.getPaths().get("/pet");
    assertNotNull(petPath.get$ref());
    assertEquals(petPath.get$ref(), "http://my.company.com/paths/health.json");
    assertTrue(oas.getPaths().get("/user") instanceof PathItem);
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) 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