Search in sources :

Example 1 with ObjectSchema

use of io.swagger.v3.oas.models.media.ObjectSchema in project swagger-core by swagger-api.

the class ObjectPropertyTest method readModelWithObjectProperty.

@Test(description = "convert a model with object properties")
public void readModelWithObjectProperty() throws IOException {
    String json = "{" + "   \"properties\":{" + "      \"id\":{" + "         \"type\":\"string\"" + "      }," + "      \"someObject\":{" + "         \"type\":\"object\"," + "        \"x-foo\": \"vendor x\"," + "         \"properties\":{" + "            \"innerId\":{" + "               \"type\":\"string\"" + "            }" + "         }" + "      }" + "   }" + "}";
    Schema model = Json.mapper().readValue(json, Schema.class);
    Schema p = (Schema) model.getProperties().get("someObject");
    assertTrue(p instanceof ObjectSchema);
    ObjectSchema op = (ObjectSchema) p;
    Schema sp = op.getProperties().get("innerId");
    assertTrue(sp instanceof StringSchema);
    assertTrue(op.getExtensions() != null);
    assertNotNull(op.getExtensions().get("x-foo"));
    assertEquals(op.getExtensions().get("x-foo"), "vendor x");
}
Also used : ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Schema(io.swagger.v3.oas.models.media.Schema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Test(org.testng.annotations.Test)

Example 2 with ObjectSchema

use of io.swagger.v3.oas.models.media.ObjectSchema in project swagger-core by swagger-api.

the class MapPropertyDeserializerTest method testBooleanAdditionalPropertiesDeserialization.

@Test(description = "it should deserialize a boolean additionalProperties")
public void testBooleanAdditionalPropertiesDeserialization() throws Exception {
    Operation operation = Json.mapper().readValue(jsonAdditionalPropertiesBoolean, Operation.class);
    ApiResponse response = operation.getResponses().get("200");
    assertNotNull(response);
    Schema responseSchema = response.getContent().get("*/*").getSchema();
    assertNotNull(responseSchema);
    assertTrue(responseSchema instanceof ObjectSchema);
    assertTrue(responseSchema.getAdditionalProperties() instanceof Boolean);
    Assert.assertFalse((Boolean) responseSchema.getAdditionalProperties());
    operation = Json.mapper().readValue(jsonAdditionalPropertiesBooleanTrue, Operation.class);
    response = operation.getResponses().get("200");
    assertNotNull(response);
    responseSchema = response.getContent().get("*/*").getSchema();
    assertNotNull(responseSchema);
    assertTrue(responseSchema instanceof MapSchema);
    assertTrue(responseSchema.getAdditionalProperties() instanceof Boolean);
    Assert.assertTrue((Boolean) responseSchema.getAdditionalProperties());
}
Also used : ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 3 with ObjectSchema

use of io.swagger.v3.oas.models.media.ObjectSchema in project swagger-core by swagger-api.

the class MapPropertyDeserializerTest method testBooleanAdditionalPropertiesSerialization.

@Test(description = "it should serialize a boolean additionalProperties")
public void testBooleanAdditionalPropertiesSerialization() throws Exception {
    Operation operation = Json.mapper().readValue(json, Operation.class);
    ApiResponse response = operation.getResponses().get("200");
    assertNotNull(response);
    Schema responseSchema = response.getContent().get("*/*").getSchema();
    Schema schema = new ObjectSchema().additionalProperties(true);
    assertEquals(normalizeLineEnds(Json.pretty(schema)), "{\n" + "  \"type\" : \"object\",\n" + "  \"additionalProperties\" : true\n" + "}");
    schema = new ObjectSchema().additionalProperties(responseSchema);
    assertEquals(normalizeLineEnds(Json.pretty(schema)), "{\n" + "  \"type\" : \"object\",\n" + "  \"additionalProperties\" : {\n" + "    \"type\" : \"object\",\n" + "    \"additionalProperties\" : {\n" + "      \"type\" : \"integer\",\n" + "      \"format\" : \"int32\"\n" + "    },\n" + "    \"x-foo\" : \"vendor x\"\n" + "  }\n" + "}");
}
Also used : ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Schema(io.swagger.v3.oas.models.media.Schema) Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 4 with ObjectSchema

use of io.swagger.v3.oas.models.media.ObjectSchema in project swagger-core by swagger-api.

the class ModelExampleTest method createModel.

@Test(description = "it should create a model")
public void createModel() {
    ObjectSchema model = new ObjectSchema();
    model.addProperties("name", new StringSchema().example("Tony"));
    model.addProperties("id", new IntegerSchema().example(123));
    assertNotNull(model);
}
Also used : ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Test(org.testng.annotations.Test)

Example 5 with ObjectSchema

use of io.swagger.v3.oas.models.media.ObjectSchema in project swagger-core by swagger-api.

the class OpenAPI3_1SerializationTest method testRequestBodyRefSerialization.

@Test
public void testRequestBodyRefSerialization() {
    OpenAPI openAPI = new OpenAPI().openapi("3.1.0").path("/test", new PathItem().description("test path item").post(new Operation().operationId("testPathItem").requestBody(new RequestBody().$ref("#/components/requestBodies/body").description("ref request body")))).components(new Components().addRequestBodies("body", new RequestBody().content(new Content().addMediaType("application/json", new MediaType().schema(new ObjectSchema())))));
    SerializationMatchers.assertEqualsToYaml31(openAPI, "openapi: 3.1.0\n" + "paths:\n" + "  /test:\n" + "    description: test path item\n" + "    post:\n" + "      operationId: testPathItem\n" + "      requestBody:\n" + "        description: ref request body\n" + "        $ref: '#/components/requestBodies/body'\n" + "components:\n" + "  requestBodies:\n" + "    body:\n" + "      content:\n" + "        application/json:\n" + "          schema: {}");
    SerializationMatchers.assertEqualsToJson31(openAPI, "{\n" + "  \"openapi\" : \"3.1.0\",\n" + "  \"paths\" : {\n" + "    \"/test\" : {\n" + "      \"description\" : \"test path item\",\n" + "      \"post\" : {\n" + "        \"operationId\" : \"testPathItem\",\n" + "        \"requestBody\" : {\n" + "          \"description\" : \"ref request body\",\n" + "          \"$ref\" : \"#/components/requestBodies/body\"\n" + "        }\n" + "      }\n" + "    }\n" + "  },\n" + "  \"components\" : {\n" + "    \"requestBodies\" : {\n" + "      \"body\" : {\n" + "        \"content\" : {\n" + "          \"application/json\" : {\n" + "            \"schema\" : { }\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}");
}
Also used : Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Content(io.swagger.v3.oas.models.media.Content) MediaType(io.swagger.v3.oas.models.media.MediaType) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Aggregations

ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)14 Schema (io.swagger.v3.oas.models.media.Schema)11 StringSchema (io.swagger.v3.oas.models.media.StringSchema)10 Test (org.testng.annotations.Test)10 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)9 MapSchema (io.swagger.v3.oas.models.media.MapSchema)7 Components (io.swagger.v3.oas.models.Components)5 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)5 NumberSchema (io.swagger.v3.oas.models.media.NumberSchema)5 Operation (io.swagger.v3.oas.models.Operation)4 AnnotatedType (io.swagger.v3.core.converter.AnnotatedType)3 OpenAPI (io.swagger.v3.oas.models.OpenAPI)3 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)3 UUIDSchema (io.swagger.v3.oas.models.media.UUIDSchema)3 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)3 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)2 JsonView (com.fasterxml.jackson.annotation.JsonView)2 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)2 JavaType (com.fasterxml.jackson.databind.JavaType)2 AnnotatedMethod (com.fasterxml.jackson.databind.introspect.AnnotatedMethod)2