Search in sources :

Example 1 with ResolvedEnumConstantDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedEnumConstantDeclaration 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)

Aggregations

JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1 TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)1 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)1 Type (com.github.javaparser.ast.type.Type)1 ResolvedEnumConstantDeclaration (com.github.javaparser.resolution.declarations.ResolvedEnumConstantDeclaration)1 ResolvedFieldDeclaration (com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration)1 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)1 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)1 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)1 ExplicitNullableTypeChecker (dev.hilla.ExplicitNullableTypeChecker)1 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)1 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)1 Schema (io.swagger.v3.oas.models.media.Schema)1 StringSchema (io.swagger.v3.oas.models.media.StringSchema)1 Modifier (java.lang.reflect.Modifier)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1