Search in sources :

Example 61 with StringSchema

use of io.swagger.v3.oas.models.media.StringSchema in project flow by vaadin.

the class SchemaGenerator method createSingleSchemaFromResolvedType.

Schema createSingleSchemaFromResolvedType(GeneratorType type) {
    ResolvedReferenceType resolvedReferenceType = type.asResolvedType().asReferenceType();
    if (type.isEnum()) {
        List<String> entries = resolvedReferenceType.getTypeDeclaration().orElseThrow(IllegalArgumentException::new).asEnum().getEnumConstants().stream().map(ResolvedEnumConstantDeclaration::getName).collect(Collectors.toList());
        StringSchema schema = new StringSchema();
        schema.name(resolvedReferenceType.getQualifiedName());
        schema.setEnum(entries);
        return schema;
    }
    Schema schema = new ObjectSchema().name(resolvedReferenceType.getQualifiedName());
    Map<String, Boolean> fieldsOptionalMap = getFieldsAndOptionalMap(type);
    List<ResolvedFieldDeclaration> serializableFields = resolvedReferenceType.getTypeDeclaration().orElseThrow(IllegalArgumentException::new).getDeclaredFields().stream().filter(resolvedFieldDeclaration -> fieldsOptionalMap.containsKey(resolvedFieldDeclaration.getName())).collect(Collectors.toList());
    // Make sure the order is consistent in properties map
    schema.setProperties(new LinkedHashMap<>());
    for (ResolvedFieldDeclaration resolvedFieldDeclaration : serializableFields) {
        String name = resolvedFieldDeclaration.getName();
        ResolvedType fieldType = resolvedFieldDeclaration.getType();
        ResolvedType mappedType = openApiObjectGenerator.toMappedType(fieldType);
        if (mappedType != null) {
            fieldType = mappedType;
        }
        Schema subtype = openApiObjectGenerator.parseResolvedTypeToSchema(new GeneratorType(fieldType)).nullable(null);
        if (!fieldsOptionalMap.get(name)) {
            schema.addRequiredItem(name);
        }
        schema.addProperties(name, subtype);
    }
    return schema;
}
Also used : Arrays(java.util.Arrays) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) LinkedHashMap(java.util.LinkedHashMap) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Type(com.github.javaparser.ast.type.Type) ResolvedFieldDeclaration(com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration) Map(java.util.Map) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) Schema(io.swagger.v3.oas.models.media.Schema) LinkedHashSet(java.util.LinkedHashSet) Logger(org.slf4j.Logger) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) Set(java.util.Set) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) Collectors(java.util.stream.Collectors) List(java.util.List) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ResolvedEnumConstantDeclaration(com.github.javaparser.resolution.declarations.ResolvedEnumConstantDeclaration) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) ExplicitNullableTypeChecker(dev.hilla.ExplicitNullableTypeChecker) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ResolvedFieldDeclaration(com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Example 62 with StringSchema

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

the class ParameterDeSerializationTest method deserializeEnumPathParameter.

@Test(description = "it should deserialize a path parameter with enum")
public void deserializeEnumPathParameter() throws IOException {
    final String json = "{" + "   \"in\":\"path\"," + "   \"required\":true," + "   \"schema\":{" + "     \"type\":\"array\"," + "     \"items\":{" + "        \"type\":\"string\"," + "        \"enum\":[\"a\",\"b\",\"c\"]" + "     }" + "}}";
    final Parameter p = m.readValue(json, Parameter.class);
    SerializationMatchers.assertEqualsToJson(p, json);
    ArraySchema as = (ArraySchema) p.getSchema();
    assertEquals(((StringSchema) as.getItems()).getEnum(), Arrays.asList("a", "b", "c"));
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Parameter(io.swagger.v3.oas.models.parameters.Parameter) Test(org.testng.annotations.Test)

Example 63 with StringSchema

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

the class ModelExampleTest method createModelWithExample.

@Test(description = "it should create a model with example")
public void createModelWithExample() {
    ObjectSchema model = new ObjectSchema();
    model.addProperties("name", new StringSchema().example("Tony"));
    model.addProperties("id", new IntegerSchema().example(123));
    model.example("{\"name\":\"Fred\",\"id\":123456\"}");
    assertEquals(model.getExample(), "{\"name\":\"Fred\",\"id\":123456\"}");
}
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 64 with StringSchema

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

the class OpenAPI3_1SerializationTest method testComponentPathItemsSerialization.

@Test
public void testComponentPathItemsSerialization() {
    Schema schema = new StringSchema();
    schema.addType(schema.getType());
    OpenAPI openAPI = new OpenAPI().openapi("3.1.0").components(new Components().addSchemas("stringTest", schema).addPathItem("/pathTest", new PathItem().description("test path item").get(new Operation().operationId("testPathItem").responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("response description"))))).addResponses("201", new ApiResponse().description("api response description")).addParameters("param", new Parameter().in("query").description("parameter description").schema(schema)).addExamples("example", new Example().summary("example summary").value("This is an example/").description("example description")).addRequestBodies("body", new RequestBody().content(new Content().addMediaType("application/json", new MediaType().schema(new ObjectSchema())))).addHeaders("test-head", new Header().description("test header description")).addSecuritySchemes("basic", new SecurityScheme().in(SecurityScheme.In.HEADER).scheme("http").description("ref security description")).addLinks("Link", new Link().operationRef("#/paths/~12.0~1repositories~1{username}/get")).addCallbacks("TestCallback", new Callback().addPathItem("{$request.query.queryUrl}", new PathItem().description("test path item").post(new Operation().operationId("testPathItem")))));
    SerializationMatchers.assertEqualsToYaml31(openAPI, "openapi: 3.1.0\n" + "components:\n" + "  schemas:\n" + "    stringTest:\n" + "      type: string\n" + "  responses:\n" + "    \"201\":\n" + "      description: api response description\n" + "  parameters:\n" + "    param:\n" + "      in: query\n" + "      description: parameter description\n" + "      schema:\n" + "        type: string\n" + "  examples:\n" + "    example:\n" + "      summary: example summary\n" + "      description: example description\n" + "      value: This is an example/\n" + "  requestBodies:\n" + "    body:\n" + "      content:\n" + "        application/json:\n" + "          schema: {}\n" + "  headers:\n" + "    test-head:\n" + "      description: test header description\n" + "  securitySchemes:\n" + "    basic:\n" + "      description: ref security description\n" + "      in: header\n" + "      scheme: http\n" + "  links:\n" + "    Link:\n" + "      operationRef: \"#/paths/~12.0~1repositories~1{username}/get\"\n" + "  callbacks:\n" + "    TestCallback:\n" + "      '{$request.query.queryUrl}':\n" + "        description: test path item\n" + "        post:\n" + "          operationId: testPathItem\n" + "  pathItems:\n" + "    /pathTest:\n" + "      description: test path item\n" + "      get:\n" + "        operationId: testPathItem\n" + "        responses:\n" + "          \"200\":\n" + "            description: response description");
    SerializationMatchers.assertEqualsToJson31(openAPI, "{\n" + "  \"openapi\" : \"3.1.0\",\n" + "  \"components\" : {\n" + "    \"schemas\" : {\n" + "      \"stringTest\" : {\n" + "        \"type\" : \"string\"\n" + "      }\n" + "    },\n" + "    \"responses\" : {\n" + "      \"201\" : {\n" + "        \"description\" : \"api response description\"\n" + "      }\n" + "    },\n" + "    \"parameters\" : {\n" + "      \"param\" : {\n" + "        \"in\" : \"query\",\n" + "        \"description\" : \"parameter description\",\n" + "        \"schema\" : {\n" + "          \"type\" : \"string\"\n" + "        }\n" + "      }\n" + "    },\n" + "    \"examples\" : {\n" + "      \"example\" : {\n" + "        \"summary\" : \"example summary\",\n" + "        \"description\" : \"example description\",\n" + "        \"value\" : \"This is an example/\"\n" + "      }\n" + "    },\n" + "    \"requestBodies\" : {\n" + "      \"body\" : {\n" + "        \"content\" : {\n" + "          \"application/json\" : {\n" + "            \"schema\" : { }\n" + "          }\n" + "        }\n" + "      }\n" + "    },\n" + "    \"headers\" : {\n" + "      \"test-head\" : {\n" + "        \"description\" : \"test header description\"\n" + "      }\n" + "    },\n" + "    \"securitySchemes\" : {\n" + "      \"basic\" : {\n" + "        \"description\" : \"ref security description\",\n" + "        \"in\" : \"header\",\n" + "        \"scheme\" : \"http\"\n" + "      }\n" + "    },\n" + "    \"links\" : {\n" + "      \"Link\" : {\n" + "        \"operationRef\" : \"#/paths/~12.0~1repositories~1{username}/get\"\n" + "      }\n" + "    },\n" + "    \"callbacks\" : {\n" + "      \"TestCallback\" : {\n" + "        \"{$request.query.queryUrl}\" : {\n" + "          \"description\" : \"test path item\",\n" + "          \"post\" : {\n" + "            \"operationId\" : \"testPathItem\"\n" + "          }\n" + "        }\n" + "      }\n" + "    },\n" + "    \"pathItems\" : {\n" + "      \"/pathTest\" : {\n" + "        \"description\" : \"test path item\",\n" + "        \"get\" : {\n" + "          \"operationId\" : \"testPathItem\",\n" + "          \"responses\" : {\n" + "            \"200\" : {\n" + "              \"description\" : \"response description\"\n" + "            }\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}");
    openAPI.openapi("3.0.3");
    SerializationMatchers.assertEqualsToYaml(openAPI, "openapi: 3.0.3\n" + "components:\n" + "  schemas:\n" + "    stringTest:\n" + "      type: string\n" + "  responses:\n" + "    \"201\":\n" + "      description: api response description\n" + "  parameters:\n" + "    param:\n" + "      in: query\n" + "      description: parameter description\n" + "      schema:\n" + "        type: string\n" + "  examples:\n" + "    example:\n" + "      summary: example summary\n" + "      description: example description\n" + "      value: This is an example/\n" + "  requestBodies:\n" + "    body:\n" + "      content:\n" + "        application/json:\n" + "          schema:\n" + "            type: object\n" + "  headers:\n" + "    test-head:\n" + "      description: test header description\n" + "  securitySchemes:\n" + "    basic:\n" + "      description: ref security description\n" + "      in: header\n" + "      scheme: http\n" + "  links:\n" + "    Link:\n" + "      operationRef: \"#/paths/~12.0~1repositories~1{username}/get\"\n" + "  callbacks:\n" + "    TestCallback:\n" + "      '{$request.query.queryUrl}':\n" + "        description: test path item\n" + "        post:\n" + "          operationId: testPathItem");
    SerializationMatchers.assertEqualsToJson(openAPI, "{\n" + "  \"openapi\" : \"3.0.3\",\n" + "  \"components\" : {\n" + "    \"schemas\" : {\n" + "      \"stringTest\" : {\n" + "        \"type\" : \"string\"\n" + "      }\n" + "    },\n" + "    \"responses\" : {\n" + "      \"201\" : {\n" + "        \"description\" : \"api response description\"\n" + "      }\n" + "    },\n" + "    \"parameters\" : {\n" + "      \"param\" : {\n" + "        \"in\" : \"query\",\n" + "        \"description\" : \"parameter description\",\n" + "        \"schema\" : {\n" + "          \"type\" : \"string\"\n" + "        }\n" + "      }\n" + "    },\n" + "    \"examples\" : {\n" + "      \"example\" : {\n" + "        \"summary\" : \"example summary\",\n" + "        \"description\" : \"example description\",\n" + "        \"value\" : \"This is an example/\"\n" + "      }\n" + "    },\n" + "    \"requestBodies\" : {\n" + "      \"body\" : {\n" + "        \"content\" : {\n" + "          \"application/json\" : {\n" + "            \"schema\" : {\n" + "              \"type\" : \"object\"\n" + "            }\n" + "          }\n" + "        }\n" + "      }\n" + "    },\n" + "    \"headers\" : {\n" + "      \"test-head\" : {\n" + "        \"description\" : \"test header description\"\n" + "      }\n" + "    },\n" + "    \"securitySchemes\" : {\n" + "      \"basic\" : {\n" + "        \"description\" : \"ref security description\",\n" + "        \"in\" : \"header\",\n" + "        \"scheme\" : \"http\"\n" + "      }\n" + "    },\n" + "    \"links\" : {\n" + "      \"Link\" : {\n" + "        \"operationRef\" : \"#/paths/~12.0~1repositories~1{username}/get\"\n" + "      }\n" + "    },\n" + "    \"callbacks\" : {\n" + "      \"TestCallback\" : {\n" + "        \"{$request.query.queryUrl}\" : {\n" + "          \"description\" : \"test path item\",\n" + "          \"post\" : {\n" + "            \"operationId\" : \"testPathItem\"\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}");
}
Also used : Schema(io.swagger.v3.oas.models.media.Schema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) Callback(io.swagger.v3.oas.models.callbacks.Callback) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Header(io.swagger.v3.oas.models.headers.Header) Content(io.swagger.v3.oas.models.media.Content) Example(io.swagger.v3.oas.models.examples.Example) Parameter(io.swagger.v3.oas.models.parameters.Parameter) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Link(io.swagger.v3.oas.models.links.Link) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Example 65 with StringSchema

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

the class BeanValidatorTest method readBeanValidatorTest.

@Test(description = "read bean validations")
public void readBeanValidatorTest() {
    final Map<String, Schema> schemas = ModelConverters.getInstance().readAll(BeanValidationsModel.class);
    final Schema model = schemas.get("BeanValidationsModel");
    final Map<String, Schema> properties = model.getProperties();
    Assert.assertTrue(model.getRequired().contains("id"));
    final IntegerSchema age = (IntegerSchema) properties.get("age");
    Assert.assertEquals(age.getMinimum(), new BigDecimal(13.0));
    Assert.assertEquals(age.getMaximum(), new BigDecimal(99.0));
    final StringSchema password = (StringSchema) properties.get("password");
    Assert.assertEquals((int) password.getMinLength(), 6);
    Assert.assertEquals((int) password.getMaxLength(), 20);
    final StringSchema email = (StringSchema) properties.get("email");
    Assert.assertEquals((String) email.getPattern(), "(.+?)@(.+?)");
    final NumberSchema minBalance = (NumberSchema) properties.get("minBalance");
    Assert.assertTrue(minBalance.getExclusiveMinimum());
    final NumberSchema maxBalance = (NumberSchema) properties.get("maxBalance");
    Assert.assertTrue(maxBalance.getExclusiveMaximum());
    final ArraySchema items = (ArraySchema) properties.get("items");
    Assert.assertEquals((int) items.getMinItems(), 2);
    Assert.assertEquals((int) items.getMaxItems(), 10);
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

StringSchema (io.swagger.v3.oas.models.media.StringSchema)89 Test (org.testng.annotations.Test)79 Schema (io.swagger.v3.oas.models.media.Schema)70 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)54 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)53 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 MapSchema (io.swagger.v3.oas.models.media.MapSchema)20 NumberSchema (io.swagger.v3.oas.models.media.NumberSchema)20 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)19 DateSchema (io.swagger.v3.oas.models.media.DateSchema)17 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)16 Components (io.swagger.v3.oas.models.Components)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