use of org.eclipse.microprofile.openapi.models.media.Schema in project Payara by payara.
the class SchemaImpl method createInstance.
public static SchemaImpl createInstance(AnnotationModel annotation, ApiContext context) {
SchemaImpl from = new SchemaImpl();
if (annotation == null) {
return from;
}
// Solve the required attribute before "ref" as it is the only one which doesn't conflict with it.
final Boolean isRequired = annotation.getValue("required", Boolean.class);
if (isRequired != null) {
from.isRequired = isRequired;
}
String ref = annotation.getValue("ref", String.class);
if (ref != null && !ref.isEmpty()) {
from.setRef(ref);
return from;
}
EnumModel typeEnum = annotation.getValue("type", EnumModel.class);
if (typeEnum != null) {
from.setType(SchemaType.valueOf(typeEnum.getValue()));
}
final String implementationClass = annotation.getValue("implementation", String.class);
if (implementationClass != null) {
setImplementation(from, implementationClass, true, context);
}
from.setDefaultValue(annotation.getValue("defaultValue", Object.class));
from.setName(annotation.getValue("name", String.class));
from.setTitle(annotation.getValue("title", String.class));
Double multipleOf = annotation.getValue("multipleOf", Double.class);
if (multipleOf != null) {
from.setMultipleOf(BigDecimal.valueOf(multipleOf));
}
String maximum = annotation.getValue("maximum", String.class);
if (maximum != null && !maximum.isEmpty()) {
from.setMaximum(new BigDecimal(maximum));
}
from.setExclusiveMaximum(annotation.getValue("exclusiveMaximum", Boolean.class));
String minimum = annotation.getValue("minimum", String.class);
if (minimum != null && !minimum.isEmpty()) {
from.setMinimum(new BigDecimal(minimum));
}
from.setExclusiveMinimum(annotation.getValue("exclusiveMinimum", Boolean.class));
from.setMaxLength(annotation.getValue("maxLength", Integer.class));
from.setMinLength(annotation.getValue("minLength", Integer.class));
from.setPattern(annotation.getValue("pattern", String.class));
from.setMaxItems(annotation.getValue("maxItems", Integer.class));
from.setMinItems(annotation.getValue("minItems", Integer.class));
from.setUniqueItems(annotation.getValue("uniqueItems", Boolean.class));
from.setMaxProperties(annotation.getValue("maxProperties", Integer.class));
from.setMinProperties(annotation.getValue("minProperties", Integer.class));
from.setRequired(annotation.getValue("requiredProperties", List.class));
extractAnnotations(annotation, context, "properties", "name", SchemaImpl::createInstance, from::addProperty);
for (Entry<String, Schema> property : from.getProperties().entrySet()) {
final SchemaImpl propertySchema = (SchemaImpl) property.getValue();
if (propertySchema.isRequired) {
from.addRequired(property.getKey());
}
}
from.setDescription(annotation.getValue("description", String.class));
from.setFormat(annotation.getValue("format", String.class));
from.setNullable(annotation.getValue("nullable", Boolean.class));
from.setReadOnly(annotation.getValue("readOnly", Boolean.class));
from.setWriteOnly(annotation.getValue("writeOnly", Boolean.class));
from.setExample(annotation.getValue("example", Object.class));
AnnotationModel externalDocs = annotation.getValue("externalDocs", AnnotationModel.class);
if (externalDocs != null) {
from.setExternalDocs(ExternalDocumentationImpl.createInstance(externalDocs));
}
from.setDeprecated(annotation.getValue("deprecated", Boolean.class));
from.setEnumeration(annotation.getValue("enumeration", List.class));
String discriminatorProperty = annotation.getValue("discriminatorProperty", String.class);
List<AnnotationModel> discriminatorMapping = annotation.getValue("discriminatorMapping", List.class);
if (discriminatorMapping != null && !discriminatorProperty.isEmpty()) {
DiscriminatorImpl discriminator = new DiscriminatorImpl();
discriminator.setPropertyName(discriminatorProperty);
for (AnnotationModel mapping : discriminatorMapping) {
String value = mapping.getValue("value", String.class);
String schema = mapping.getValue("schema", String.class);
discriminator.addMapping(value, ModelUtils.getSimpleName(schema));
}
from.setDiscriminator(discriminator);
}
String not = annotation.getValue("not", String.class);
if (not != null) {
Schema schema = from.getSchemaInstance(not, context);
if (schema != null) {
from.setNot(schema);
}
}
List<String> anyOf = annotation.getValue("anyOf", List.class);
if (anyOf != null) {
mergeImmutableList(from.getAnyOf(), from.getSchemaInstances(anyOf, context), from::setAnyOf);
}
List<String> allOf = annotation.getValue("allOf", List.class);
if (allOf != null) {
mergeImmutableList(from.getAllOf(), from.getSchemaInstances(allOf, context), from::setAllOf);
}
List<String> oneOf = annotation.getValue("oneOf", List.class);
if (oneOf != null) {
mergeImmutableList(from.getOneOf(), from.getSchemaInstances(oneOf, context), from::setOneOf);
}
return from;
}
use of org.eclipse.microprofile.openapi.models.media.Schema 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 in project Payara by payara.
the class ApplicationProcessor method insertObjectReference.
/**
* Replace the object in the referee with a reference, and create the
* reference in the API.
*
* @param context the API context.
* @param referee the object containing the reference.
* @param referenceClass the class of the object being referenced.
* @return if the reference has been created.
*/
private boolean insertObjectReference(ApiContext context, Reference<?> referee, AnnotatedElement referenceClass, String referenceClassName) {
// Firstly check if it's been already defined (i.e. config property definition)
for (Entry<String, Schema> schemaEntry : context.getApi().getComponents().getSchemas().entrySet()) {
final Schema entryValue = schemaEntry.getValue();
if (entryValue instanceof SchemaImpl) {
final SchemaImpl entryValueImpl = (SchemaImpl) entryValue;
final String implementationClass = entryValueImpl.getImplementation();
if (implementationClass != null && implementationClass.equals(referenceClassName)) {
referee.setRef(schemaEntry.getKey());
return true;
}
}
}
// If the object is a java core class
if (referenceClassName == null || referenceClassName.startsWith("java.")) {
return false;
}
// If the object is a Java EE object type
if (referenceClassName.startsWith("javax.")) {
return false;
}
// Check the class exists in the application
if (!context.isApplicationType(referenceClassName)) {
return false;
}
if (referenceClass != null && referenceClass instanceof ExtensibleType) {
ExtensibleType referenceClassType = (ExtensibleType) referenceClass;
final AnnotationModel schemaAnnotation = context.getAnnotationInfo(referenceClassType).getAnnotation(org.eclipse.microprofile.openapi.annotations.media.Schema.class);
String schemaName = ModelUtils.getSchemaName(context, referenceClass);
// Set the reference name
referee.setRef(schemaName);
Schema schema = context.getApi().getComponents().getSchemas().get(schemaName);
if (schema == null) {
// Create the schema
if (context.isAllowedType(referenceClassType)) {
visitSchema(schemaAnnotation, referenceClassType, context);
} else if (referenceClassType instanceof ClassModel) {
apiWalker.processAnnotation((ClassModel) referenceClassType, this);
} else {
LOGGER.log(FINE, "Unrecognised schema {0} class found.", new Object[] { referenceClassName });
}
}
return true;
}
return false;
}
use of org.eclipse.microprofile.openapi.models.media.Schema 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 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);
}
}
}
}
Aggregations