Search in sources :

Example 36 with StringSchema

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

the class SchemaResolverTest method should_ReturnArraySchema_When_GivenTypeIsAnArray.

@Test
public void should_ReturnArraySchema_When_GivenTypeIsAnArray() {
    ResolvedType arrayType = mock(ResolvedType.class);
    ResolvedArrayType arrayResolvedType = mock(ResolvedArrayType.class);
    ResolvedType stringType = mockReferencedTypeOf(String.class);
    when(arrayType.isArray()).thenReturn(true);
    when(arrayType.asArrayType()).thenReturn(arrayResolvedType);
    when(arrayResolvedType.getComponentType()).thenReturn(stringType);
    Map<String, GeneratorType> usedTypes = new HashMap<>();
    SchemaResolver schemaResolver = new SchemaResolver(new GeneratorType(arrayType), usedTypes);
    Schema schema = schemaResolver.resolve();
    Assert.assertTrue(schema instanceof ArraySchema);
    Assert.assertTrue(schema.getNullable());
    Assert.assertTrue(((ArraySchema) schema).getItems() instanceof StringSchema);
    Assert.assertTrue(usedTypes.isEmpty());
}
Also used : ResolvedArrayType(com.github.javaparser.resolution.types.ResolvedArrayType) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) HashMap(java.util.HashMap) BooleanSchema(io.swagger.v3.oas.models.media.BooleanSchema) DateSchema(io.swagger.v3.oas.models.media.DateSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Example 37 with StringSchema

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

the class SchemaResolverTest method should_ReturnNullableStringSchema_When_GivenTypeIsAString.

@Test
public void should_ReturnNullableStringSchema_When_GivenTypeIsAString() {
    ResolvedType resolvedType = mockReferencedTypeOf(String.class);
    Map<String, GeneratorType> usedTypes = new HashMap<>();
    SchemaResolver schemaResolver = new SchemaResolver(new GeneratorType(resolvedType), usedTypes);
    Schema schema = schemaResolver.resolve();
    Assert.assertTrue(schema instanceof StringSchema);
    Assert.assertTrue(schema.getNullable());
    Assert.assertTrue(usedTypes.isEmpty());
}
Also used : HashMap(java.util.HashMap) BooleanSchema(io.swagger.v3.oas.models.media.BooleanSchema) DateSchema(io.swagger.v3.oas.models.media.DateSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Example 38 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 39 with StringSchema

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

the class SchemaResolver method createEnumTypeSchema.

private Schema createEnumTypeSchema() {
    ResolvedReferenceType resolvedReferenceType = type.asResolvedType().asReferenceType();
    List<String> entries = resolvedReferenceType.getTypeDeclaration().orElseThrow(IllegalArgumentException::new).asEnum().getEnumConstants().stream().map(ResolvedEnumConstantDeclaration::getName).collect(Collectors.toList());
    String qualifiedName = resolvedReferenceType.getQualifiedName();
    usedTypes.put(qualifiedName, type);
    StringSchema schema = new StringSchema();
    schema.name(qualifiedName);
    schema.setEnum(entries);
    schema.$ref(getFullQualifiedNameRef(qualifiedName));
    return schema;
}
Also used : ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) StringSchema(io.swagger.v3.oas.models.media.StringSchema)

Example 40 with StringSchema

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

the class SchemaResolverTest method should_ReturnNullableArray_When_GivenTypeIsAListString.

@Test
public void should_ReturnNullableArray_When_GivenTypeIsAListString() {
    ResolvedType resolvedType = mockReferencedTypeOf(Collection.class);
    ResolvedReferenceType resolvedReferenceType = resolvedType.asReferenceType();
    List<Pair<ResolvedTypeParameterDeclaration, ResolvedType>> pairs = Collections.singletonList(new Pair<>(null, mockReferencedTypeOf(String.class)));
    when(resolvedReferenceType.getTypeParametersMap()).thenReturn(pairs);
    Map<String, GeneratorType> usedTypes = new HashMap<>();
    SchemaResolver schemaResolver = new SchemaResolver(new GeneratorType(resolvedType), usedTypes);
    Schema schema = schemaResolver.resolve();
    Assert.assertTrue(schema instanceof ArraySchema);
    Assert.assertTrue(schema.getNullable());
    Assert.assertTrue(((ArraySchema) schema).getItems() instanceof StringSchema);
    Assert.assertTrue(usedTypes.isEmpty());
}
Also used : ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) HashMap(java.util.HashMap) BooleanSchema(io.swagger.v3.oas.models.media.BooleanSchema) DateSchema(io.swagger.v3.oas.models.media.DateSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Pair(com.github.javaparser.utils.Pair) Test(org.junit.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