use of org.eclipse.microprofile.openapi.models.media.Schema.SchemaType in project Payara by payara.
the class ApplicationProcessor method createSchema.
private Schema createSchema(Schema schema, ApiContext context, ParameterizedType type, ExtensibleType clazz, Collection<ParameterizedInterfaceModel> classParameterizedTypes) {
if (schema == null) {
schema = new SchemaImpl();
}
SchemaType schemaType = ModelUtils.getSchemaType(type, context);
// If the annotated element is the same type as the reference class, return a null schema
if (schemaType == SchemaType.OBJECT && type.getType() != null && type.getType().equals(clazz)) {
schema.setType(null);
schema.setItems(null);
return schema;
}
if (type.getType() == null) {
ParameterizedInterfaceModel classParameterizedType = findParameterizedModelFromGenerics(clazz, classParameterizedTypes, type);
String typeName = null;
if (type.getTypeName() != null) {
typeName = type.getTypeName();
}
if ((typeName == null || Object.class.getName().equals(typeName)) && classParameterizedType != null) {
typeName = classParameterizedType.getRawInterfaceName();
}
schemaType = ModelUtils.getSchemaType(typeName, context);
if (schema.getType() == null) {
schema.setType(schemaType);
}
Schema containerSchema = schema;
if (schemaType == SchemaType.ARRAY) {
containerSchema = new SchemaImpl();
schema.setItems(containerSchema);
}
if (classParameterizedType != null) {
Collection<ParameterizedInterfaceModel> genericTypes = classParameterizedType.getParametizedTypes();
if (genericTypes.isEmpty()) {
if (insertObjectReference(context, containerSchema, classParameterizedType.getRawInterface(), classParameterizedType.getRawInterfaceName())) {
containerSchema.setType(null);
containerSchema.setItems(null);
}
} else if (classParameterizedType.getRawInterface() instanceof ClassModel) {
visitSchemaClass(containerSchema, null, (ClassModel) classParameterizedType.getRawInterface(), genericTypes, context);
} else {
LOGGER.log(FINE, "Unrecognised schema {0} class found.", new Object[] { classParameterizedType.getRawInterface() });
}
} else if (!type.getParameterizedTypes().isEmpty()) {
List<ParameterizedType> genericTypes = type.getParameterizedTypes();
if (ModelUtils.isMap(typeName, context) && genericTypes.size() == 2) {
createSchema(containerSchema, context, genericTypes.get(0), clazz, classParameterizedTypes);
containerSchema = new SchemaImpl();
schema.setAdditionalPropertiesSchema(containerSchema);
createSchema(containerSchema, context, genericTypes.get(1), clazz, classParameterizedTypes);
} else {
createSchema(containerSchema, context, genericTypes.get(0), clazz, classParameterizedTypes);
}
} else {
return createSchema(containerSchema, context, type);
}
return schema;
}
return createSchema(schema, context, type);
}
use of org.eclipse.microprofile.openapi.models.media.Schema.SchemaType in project Payara by payara.
the class ApplicationProcessor method createSchema.
private Schema createSchema(Schema schema, ApiContext context, ParameterizedType type) {
String typeName = type.getTypeName();
List<ParameterizedType> genericTypes = type.getParameterizedTypes();
SchemaType schemaType = ModelUtils.getSchemaType(type, context);
if (schema == null) {
schema = new SchemaImpl();
schema.setType(schemaType);
}
// Set the subtype if it's an array (for example an array of ints)
if (schemaType == SchemaType.ARRAY) {
if (type.isArray()) {
schemaType = ModelUtils.getSchemaType(type.getTypeName(), context);
schema.setType(schemaType);
} else if (!genericTypes.isEmpty()) {
// should be something Iterable
schema.setItems(createSchema(context, genericTypes.get(0)));
}
}
// If the schema is an object, insert the reference
if (schemaType == SchemaType.OBJECT) {
if (insertObjectReference(context, schema, type.getType(), typeName)) {
schema.setType(null);
schema.setItems(null);
}
}
if (type instanceof AnnotatedElement) {
AnnotatedElement element = (AnnotatedElement) type;
final AnnotationModel schemaAnnotation = element.getAnnotation(org.eclipse.microprofile.openapi.annotations.media.Schema.class.getName());
if (schemaAnnotation != null) {
SchemaImpl.merge(SchemaImpl.createInstance(schemaAnnotation, context), schema, false, context);
}
}
return schema;
}
use of org.eclipse.microprofile.openapi.models.media.Schema.SchemaType in project Payara by payara.
the class ApplicationProcessor method visitFormParam.
@Override
public void visitFormParam(AnnotationModel param, AnnotatedElement element, ApiContext context) {
// Find the aggregate schema type of all the parameters
SchemaType formSchemaType = null;
if (element instanceof org.glassfish.hk2.classmodel.reflect.Parameter) {
List<org.glassfish.hk2.classmodel.reflect.Parameter> parameters = ((org.glassfish.hk2.classmodel.reflect.Parameter) element).getMethod().getParameters();
for (org.glassfish.hk2.classmodel.reflect.Parameter methodParam : parameters) {
if (methodParam.getAnnotation(FormParam.class.getName()) != null) {
formSchemaType = ModelUtils.getParentSchemaType(formSchemaType, ModelUtils.getSchemaType(methodParam, context));
}
}
}
final Operation workingOperation = context.getWorkingOperation();
if (workingOperation != null) {
// If there's no request body, fill out a new one right down to the schema
if (workingOperation.getRequestBody() == null) {
workingOperation.setRequestBody(new RequestBodyImpl().content(new ContentImpl().addMediaType(javax.ws.rs.core.MediaType.WILDCARD, new MediaTypeImpl().schema(new SchemaImpl()))));
}
for (MediaType mediaType : workingOperation.getRequestBody().getContent().getMediaTypes().values()) {
final Schema schema = mediaType.getSchema();
if (schema != null) {
schema.setType(formSchemaType);
}
}
}
}
use of org.eclipse.microprofile.openapi.models.media.Schema.SchemaType in project Payara by payara.
the class ApplicationProcessor method visitParameter.
@Override
public void visitParameter(AnnotationModel annotation, AnnotatedElement element, ApiContext context) {
Parameter matchedParam = null;
Boolean hidden = annotation.getValue("hidden", Boolean.class);
if (hidden != null && hidden) {
return;
}
Parameter parameter = ParameterImpl.createInstance(annotation, context);
if (element instanceof org.glassfish.hk2.classmodel.reflect.Parameter) {
matchedParam = findOperationParameterFor((org.glassfish.hk2.classmodel.reflect.Parameter) element, context);
}
if (element instanceof MethodModel) {
matchedParam = findOperationParameterFor(parameter, (MethodModel) element, context);
}
if (matchedParam != null) {
ParameterImpl.merge(parameter, matchedParam, true, context);
// If a content was added, and a schema type exists, reconfigure the schema type
if (matchedParam.getContent() != null && !matchedParam.getContent().getMediaTypes().isEmpty() && matchedParam.getSchema() != null && matchedParam.getSchema().getType() != null) {
SchemaType type = matchedParam.getSchema().getType();
matchedParam.setSchema(null);
for (MediaType mediaType : matchedParam.getContent().getMediaTypes().values()) {
if (mediaType.getSchema() == null) {
mediaType.setSchema(new SchemaImpl());
}
mediaType.getSchema().setType(ModelUtils.mergeProperty(mediaType.getSchema().getType(), type, false));
}
}
}
}
Aggregations