Search in sources :

Example 6 with FieldModel

use of org.glassfish.hk2.classmodel.reflect.FieldModel in project Payara by payara.

the class OpenApiWalker method processAnnotation.

private void processAnnotation(E element, AnnotationInfo annotations, ApiVisitor visitor, OpenApiContext context) {
    for (Class<? extends Annotation> annotationClass : getAnnotationVisitor(visitor).keySet()) {
        VisitorFunction<AnnotationModel, E> annotationFunction = getAnnotationVisitor(visitor).get(annotationClass);
        Class<? extends Annotation> alternative = getAnnotationAlternatives().get(annotationClass);
        // Check the element
        if (annotations.isAnnotationPresent(annotationClass, element)) {
            if (element instanceof FieldModel && (annotationClass == HeaderParam.class || annotationClass == CookieParam.class || annotationClass == PathParam.class || annotationClass == QueryParam.class)) {
                FieldModel field = (FieldModel) element;
                // NB. if fields are annotated as Param all methods have it
                for (MethodModel method : field.getDeclaringType().getMethods()) {
                    OpenApiContext methodContext = new OpenApiContext(context, method);
                    if (methodContext.getWorkingOperation() != null) {
                        annotationFunction.apply(annotations.getAnnotation(annotationClass, element), element, methodContext);
                    }
                }
            } else {
                annotationFunction.apply(annotations.getAnnotation(annotationClass, element), element, context);
            }
        } else if (element instanceof MethodModel && annotations.isAnnotationPresent(annotationClass) && (alternative == null || !annotations.isAnnotationPresent(alternative, element))) {
            // If the method isn't annotated, inherit the class annotation
            if (context.getPath() != null) {
                annotationFunction.apply(annotations.getAnnotation(annotationClass), element, context);
            }
        }
    }
}
Also used : CookieParam(javax.ws.rs.CookieParam) MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) HeaderParam(javax.ws.rs.HeaderParam) DELETE(javax.ws.rs.DELETE) QueryParam(javax.ws.rs.QueryParam) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel)

Example 7 with FieldModel

use of org.glassfish.hk2.classmodel.reflect.FieldModel in project Payara by payara.

the class ApplicationProcessor method visitSchemaFieldOrMethod.

private void visitSchemaFieldOrMethod(AnnotationModel schemaAnnotation, AnnotatedElement fieldOrMethod, ExtensibleType<?> declaringType, String typeName, ApiContext context) {
    assert (fieldOrMethod instanceof FieldModel) || (fieldOrMethod instanceof MethodModel);
    Boolean hidden = schemaAnnotation.getValue("hidden", Boolean.class);
    if (hidden == null || !hidden) {
        // Get the schema object name
        String schemaName = ModelUtils.getSchemaName(context, fieldOrMethod);
        SchemaImpl schema = SchemaImpl.createInstance(schemaAnnotation, context);
        // Get the parent schema object name
        String parentName = null;
        AnnotationModel classSchemaAnnotation = context.getAnnotationInfo(declaringType).getAnnotation(org.eclipse.microprofile.openapi.annotations.media.Schema.class);
        if (classSchemaAnnotation != null) {
            parentName = classSchemaAnnotation.getValue("name", String.class);
        }
        if (parentName == null || parentName.isEmpty()) {
            parentName = declaringType.getSimpleName();
        }
        // Get or create the parent schema object
        final Components components = context.getApi().getComponents();
        Schema parentSchema = components.getSchemas().getOrDefault(parentName, new SchemaImpl());
        components.addSchema(parentName, parentSchema);
        Schema property = parentSchema.getProperties().getOrDefault(schemaName, new SchemaImpl());
        parentSchema.addProperty(schemaName, property);
        if (schema.isRequired()) {
            parentSchema.addRequired(schemaName);
        }
        if (property.getRef() == null) {
            property.setType(ModelUtils.getSchemaType(typeName, context));
        }
        SchemaImpl.merge(schema, property, true, context);
    }
}
Also used : Components(org.eclipse.microprofile.openapi.models.Components) MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) Schema(org.eclipse.microprofile.openapi.models.media.Schema) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel)

Example 8 with FieldModel

use of org.glassfish.hk2.classmodel.reflect.FieldModel in project Payara by payara.

the class ApplicationProcessor method visitSchemaClass.

private Schema visitSchemaClass(Schema schema, AnnotationModel schemaAnnotation, ClassModel clazz, Collection<ParameterizedInterfaceModel> parameterizedInterfaces, ApiContext context) {
    // Get the schema object name
    String schemaName = ModelUtils.getSchemaName(context, clazz);
    // Add a new schema
    if (schema == null) {
        final Components components = context.getApi().getComponents();
        schema = components.getSchemas().getOrDefault(schemaName, new SchemaImpl());
        components.addSchema(schemaName, schema);
    }
    // If there is an annotation, parse its configuration
    if (schemaAnnotation != null) {
        SchemaImpl.merge(SchemaImpl.createInstance(schemaAnnotation, context), schema, false, context);
    }
    for (FieldModel field : clazz.getFields()) {
        final String fieldName = field.getName();
        Boolean hidden = false;
        AnnotationModel fieldSchemaAnnotation = field.getAnnotation(org.eclipse.microprofile.openapi.annotations.media.Schema.class.getName());
        if (fieldSchemaAnnotation != null) {
            hidden = fieldSchemaAnnotation.getValue("hidden", Boolean.class);
        }
        if (!Boolean.TRUE.equals(hidden) && !field.isTransient() && !fieldName.startsWith("this$")) {
            final Schema existingProperty = schema.getProperties().get(fieldName);
            final Schema newProperty = createSchema(null, context, field, clazz, parameterizedInterfaces);
            if (existingProperty != null) {
                SchemaImpl.merge(existingProperty, newProperty, true, context);
            }
            schema.addProperty(fieldName, newProperty);
        }
    }
    if (schema.getType() == null) {
        schema.setType(ModelUtils.getSchemaType(clazz.getName(), context));
    }
    // If there is an extending class, add the data
    final ClassModel superClass = clazz.getParent();
    if (superClass != null && !superClass.getName().startsWith("java.")) {
        // Get the parent annotation
        AnnotationModel parentSchemAnnotation = context.getAnnotationInfo(superClass).getAnnotation(org.eclipse.microprofile.openapi.annotations.media.Schema.class);
        ParameterizedInterfaceModel parameterizedInterface = clazz.getParameterizedInterface(superClass);
        if (parameterizedInterface == null) {
            // Create a schema for the parent
            final Schema parentSchema = visitSchemaClass(null, parentSchemAnnotation, superClass, Collections.emptyList(), context);
            // Get the superclass schema name
            String parentSchemaName = ModelUtils.getSchemaName(context, superClass);
            // Link the schemas
            schema.addAllOf(new SchemaImpl().ref(parentSchemaName));
            // Add all the parent schema properties
            for (Entry<String, Schema> property : parentSchema.getProperties().entrySet()) {
                schema.addProperty(property.getKey(), property.getValue());
            }
        } else {
            visitSchemaClass(schema, parentSchemAnnotation, superClass, parameterizedInterface.getParametizedTypes(), context);
        }
    }
    return schema;
}
Also used : Schema(org.eclipse.microprofile.openapi.models.media.Schema) Components(org.eclipse.microprofile.openapi.models.Components) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) ClassModel(org.glassfish.hk2.classmodel.reflect.ClassModel) ParameterizedInterfaceModel(org.glassfish.hk2.classmodel.reflect.ParameterizedInterfaceModel) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel)

Example 9 with FieldModel

use of org.glassfish.hk2.classmodel.reflect.FieldModel in project Payara by payara.

the class ApplicationProcessor method vistEnumClass.

private void vistEnumClass(AnnotationModel schemaAnnotation, EnumType enumType, ApiContext context) {
    // Get the schema object name
    String schemaName = ModelUtils.getSchemaName(context, enumType);
    Schema schema = SchemaImpl.createInstance(schemaAnnotation, context);
    Schema newSchema = new SchemaImpl();
    context.getApi().getComponents().addSchema(schemaName, newSchema);
    if (schema != null) {
        SchemaImpl.merge(schema, newSchema, true, context);
    }
    if (schema == null || schema.getEnumeration() == null || schema.getEnumeration().isEmpty()) {
        // if the schema annotation does not specify enums, then all enum fields will be added
        for (FieldModel enumField : enumType.getStaticFields()) {
            final String enumValue = enumField.getName();
            if (!enumValue.contains("$VALUES")) {
                newSchema.addEnumeration(enumValue);
            }
        }
    }
}
Also used : SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) Schema(org.eclipse.microprofile.openapi.models.media.Schema) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel)

Aggregations

FieldModel (org.glassfish.hk2.classmodel.reflect.FieldModel)9 AnnotationModel (org.glassfish.hk2.classmodel.reflect.AnnotationModel)6 SchemaImpl (fish.payara.microprofile.openapi.impl.model.media.SchemaImpl)5 MethodModel (org.glassfish.hk2.classmodel.reflect.MethodModel)5 Schema (org.eclipse.microprofile.openapi.models.media.Schema)3 Annotation (java.lang.annotation.Annotation)2 Components (org.eclipse.microprofile.openapi.models.Components)2 Parameter (org.eclipse.microprofile.openapi.models.parameters.Parameter)2 ClassModel (org.glassfish.hk2.classmodel.reflect.ClassModel)2 ExtensibleType (org.glassfish.hk2.classmodel.reflect.ExtensibleType)2 ParameterImpl (fish.payara.microprofile.openapi.impl.model.parameters.ParameterImpl)1 AnnotationInfo (fish.payara.microprofile.openapi.impl.visitor.AnnotationInfo)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CookieParam (javax.ws.rs.CookieParam)1 DELETE (javax.ws.rs.DELETE)1 HeaderParam (javax.ws.rs.HeaderParam)1