Search in sources :

Example 1 with StringSchema

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

the class EnumPropertyTest method testExtractEnumFields.

@Test(description = "it should extract enum values from fields")
public void testExtractEnumFields() {
    final Map<String, Schema> models = ModelConverters.getInstance().read(ModelWithEnumField.class);
    final Schema model = models.get("ModelWithEnumField");
    final Schema enumProperty = (Schema) model.getProperties().get("enumValue");
    assertTrue(enumProperty instanceof StringSchema);
    final StringSchema stringProperty = (StringSchema) enumProperty;
    assertEquals(stringProperty.getEnum(), Arrays.asList("PRIVATE", "PUBLIC", "SYSTEM", "INVITE_ONLY"));
}
Also used : StringSchema(io.swagger.v3.oas.models.media.StringSchema) Schema(io.swagger.v3.oas.models.media.Schema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest)

Example 2 with StringSchema

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

the class ModelConverterTest method serializeParameterizedType.

@Test(description = "it should serialize a parameterized type per 606")
public void serializeParameterizedType() {
    final Map<String, Schema> schemas = readAll(Employee.class);
    final Schema employee = (Schema) schemas.get("employee").getProperties().get("employee");
    final Map<String, Schema> props = employee.getProperties();
    final Iterator<String> et = props.keySet().iterator();
    final Schema id = props.get(et.next());
    assertTrue(id instanceof IntegerSchema);
    final Schema firstName = props.get(et.next());
    assertTrue(firstName instanceof StringSchema);
    final Schema lastName = props.get(et.next());
    assertTrue(lastName instanceof StringSchema);
    final Schema department = props.get(et.next());
    assertNotNull(department.get$ref());
    final Schema manager = props.get(et.next());
    assertNotNull(manager.get$ref());
    final Schema team = props.get(et.next());
    assertTrue(team instanceof ArraySchema);
    final ArraySchema ap = (ArraySchema) team;
    assertTrue(ap.getUniqueItems());
    assertNotNull(employee.getXml());
    assertEquals(employee.getXml().getName(), "employee");
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Test(org.testng.annotations.Test)

Example 3 with StringSchema

use of io.swagger.v3.oas.models.media.StringSchema 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 4 with StringSchema

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

the class SwaggerSerializerTest method convertSpec.

@Test(description = "it should convert a spec")
public void convertSpec() throws IOException {
    final Schema personModel = ModelConverters.getInstance().read(Person.class).get("Person");
    final Schema errorModel = ModelConverters.getInstance().read(Error.class).get("Error");
    final Info info = new Info().version("1.0.0").title("Swagger Petstore");
    final Contact contact = new Contact().name("Swagger API Team").email("foo@bar.baz").url("http://swagger.io");
    info.setContact(contact);
    final Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", "value");
    info.addExtension("x-test2", map);
    info.addExtension("x-test", "value");
    final OpenAPI swagger = new OpenAPI().info(info).addServersItem(new Server().url("http://petstore.swagger.io")).schema("Person", personModel).schema("Error", errorModel);
    final Operation get = new Operation().summary("finds pets in the system").description("a longer description").addTagsItem("Pet Operations").operationId("get pet by id").deprecated(true);
    get.addParametersItem(new Parameter().in("query").name("tags").description("tags to filter by").required(false).schema(new StringSchema()));
    get.addParametersItem(new Parameter().in("path").name("petId").description("pet to fetch").schema(new IntegerSchema().format("int64")));
    final ApiResponse response = new ApiResponse().description("pets returned").content(new Content().addMediaType("application/json", new MediaType().schema(new Schema().$ref("Person")).example("fun")));
    final ApiResponse errorResponse = new ApiResponse().description("error response").addLink("myLink", new Link().description("a link").operationId("theLinkedOperationId").addParameter("userId", "gah")).content(new Content().addMediaType("application/json", new MediaType().schema(new Schema().$ref("Error"))));
    get.responses(new ApiResponses().addApiResponse("200", response).addApiResponse("default", errorResponse));
    final Operation post = new Operation().summary("adds a new pet").description("you can add a new pet this way").addTagsItem("Pet Operations").operationId("add pet").responses(new ApiResponses().addApiResponse("default", errorResponse)).requestBody(new RequestBody().description("the pet to add").content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref("Person")))));
    swagger.paths(new Paths().addPathItem("/pets", new PathItem().get(get).post(post)));
    final String swaggerJson = Json.mapper().writeValueAsString(swagger);
    Json.prettyPrint(swagger);
    final OpenAPI rebuilt = Json.mapper().readValue(swaggerJson, OpenAPI.class);
    SerializationMatchers.assertEqualsToJson(rebuilt, swaggerJson);
}
Also used : Server(io.swagger.v3.oas.models.servers.Server) HashMap(java.util.HashMap) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) Operation(io.swagger.v3.oas.models.Operation) Info(io.swagger.v3.oas.models.info.Info) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Contact(io.swagger.v3.oas.models.info.Contact) PathItem(io.swagger.v3.oas.models.PathItem) Content(io.swagger.v3.oas.models.media.Content) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Paths(io.swagger.v3.oas.models.Paths) Person(io.swagger.v3.core.oas.models.Person) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Link(io.swagger.v3.oas.models.links.Link) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Example 5 with StringSchema

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

the class JsonPropertiesDeserializationTest method testDeserializeConstrainedStringProperty.

@Test(description = "should deserialize a string property with constraints")
public void testDeserializeConstrainedStringProperty() throws Exception {
    OpenAPI oas = TestUtils.deserializeJsonFileFromClasspath("specFiles/propertiesWithConstraints.json", OpenAPI.class);
    StringSchema property = (StringSchema) oas.getComponents().getSchemas().get("Health").getProperties().get("string_with_constraints");
    assertEquals(property.getMinLength(), Integer.valueOf(10));
    assertEquals(property.getMaxLength(), Integer.valueOf(100));
    assertEquals(property.getPattern(), "apattern");
}
Also used : StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Aggregations

StringSchema (io.swagger.v3.oas.models.media.StringSchema)88 Test (org.testng.annotations.Test)79 Schema (io.swagger.v3.oas.models.media.Schema)69 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)53 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)52 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)47 OpenAPI (io.swagger.v3.oas.models.OpenAPI)40 PathItem (io.swagger.v3.oas.models.PathItem)22 Operation (io.swagger.v3.oas.models.Operation)21 MediaType (io.swagger.v3.oas.models.media.MediaType)21 Content (io.swagger.v3.oas.models.media.Content)20 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)19 MapSchema (io.swagger.v3.oas.models.media.MapSchema)19 NumberSchema (io.swagger.v3.oas.models.media.NumberSchema)19 DateSchema (io.swagger.v3.oas.models.media.DateSchema)17 Components (io.swagger.v3.oas.models.Components)15 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)15 Parameter (io.swagger.v3.oas.models.parameters.Parameter)15 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)15 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)14