use of fish.payara.microprofile.openapi.impl.model.media.SchemaImpl in project Payara by payara.
the class ModelInvariantsTest method addKeyValueIgnoresNull.
@Test
public void addKeyValueIgnoresNull() {
BiPredicate<Extensible<?>, String> hasExtension = (obj, key) -> obj.getExtensions().containsKey(key);
assertAddIgnoresNull(new CallbackImpl(), Callback::addPathItem, Callback::hasPathItem);
assertAddIgnoresNull(new CallbackImpl(), Callback::addExtension, hasExtension);
assertAddIgnoresNull(new ExampleImpl(), Example::addExtension, hasExtension);
assertAddIgnoresNull(new HeaderImpl(), Header::addExample, (obj, key) -> obj.getExamples().containsKey(key));
assertAddIgnoresNull(new HeaderImpl(), Header::addExtension, hasExtension);
assertAddIgnoresNull(new ContactImpl(), Contact::addExtension, hasExtension);
assertAddIgnoresNull(new InfoImpl(), Info::addExtension, hasExtension);
assertAddIgnoresNull(new LicenseImpl(), License::addExtension, hasExtension);
assertAddIgnoresNull(new LinkImpl(), Link::addParameter, (obj, key) -> obj.getParameters().containsKey(key));
assertAddIgnoresNull(new LinkImpl(), Link::addExtension, hasExtension);
assertAddIgnoresNull(new ContentImpl(), Content::addMediaType, Content::hasMediaType);
assertAddIgnoresNull(new DiscriminatorImpl(), Discriminator::addMapping, (obj, key) -> obj.getMapping().containsKey(key));
assertAddIgnoresNull(new EncodingImpl(), Encoding::addHeader, (obj, key) -> obj.getHeaders().containsKey(key));
assertAddIgnoresNull(new EncodingImpl(), Encoding::addExtension, hasExtension);
assertAddIgnoresNull(new MediaTypeImpl(), MediaType::addEncoding, (obj, key) -> obj.getEncoding().containsKey(key));
assertAddIgnoresNull(new MediaTypeImpl(), MediaType::addExample, (obj, key) -> obj.getExamples().containsKey(key));
assertAddIgnoresNull(new MediaTypeImpl(), MediaType::addExtension, hasExtension);
assertAddIgnoresNull(new SchemaImpl(), Schema::addProperty, (obj, key) -> obj.getProperties().containsKey(key));
assertAddIgnoresNull(new SchemaImpl(), Schema::addExtension, hasExtension);
assertAddIgnoresNull(new XMLImpl(), XML::addExtension, hasExtension);
assertAddIgnoresNull(new ParameterImpl(), Parameter::addExample, (obj, key) -> obj.getExamples().containsKey(key));
assertAddIgnoresNull(new ParameterImpl(), Parameter::addExtension, hasExtension);
assertAddIgnoresNull(new RequestBodyImpl(), RequestBody::addExtension, hasExtension);
assertAddIgnoresNull(new APIResponseImpl(), APIResponse::addHeader, (obj, key) -> obj.getHeaders().containsKey(key));
assertAddIgnoresNull(new APIResponseImpl(), APIResponse::addLink, (obj, key) -> obj.getLinks().containsKey(key));
assertAddIgnoresNull(new APIResponseImpl(), APIResponse::addExtension, hasExtension);
assertAddIgnoresNull(new APIResponsesImpl(), APIResponses::addAPIResponse, APIResponses::hasAPIResponse);
assertAddIgnoresNull(new APIResponsesImpl(), APIResponses::addExtension, hasExtension);
assertAddIgnoresNull(new OAuthFlowImpl(), OAuthFlow::addExtension, hasExtension);
assertAddIgnoresNull(new OAuthFlowsImpl(), OAuthFlows::addExtension, hasExtension);
assertAddIgnoresNull(new SecuritySchemeImpl(), SecurityScheme::addExtension, hasExtension);
assertAddIgnoresNull(new ServerImpl(), Server::addExtension, hasExtension);
assertAddIgnoresNull(new ServerVariableImpl(), ServerVariable::addExtension, hasExtension);
assertAddIgnoresNull(new TagImpl(), Tag::addExtension, hasExtension);
assertAddIgnoresNull(new ComponentsImpl(), Components::addCallback, (obj, key) -> obj.getCallbacks().containsKey(key));
assertAddIgnoresNull(new ComponentsImpl(), Components::addExample, (obj, key) -> obj.getExamples().containsKey(key));
assertAddIgnoresNull(new ComponentsImpl(), Components::addHeader, (obj, key) -> obj.getHeaders().containsKey(key));
assertAddIgnoresNull(new ComponentsImpl(), Components::addLink, (obj, key) -> obj.getLinks().containsKey(key));
assertAddIgnoresNull(new ComponentsImpl(), Components::addParameter, (obj, key) -> obj.getParameters().containsKey(key));
assertAddIgnoresNull(new ComponentsImpl(), Components::addRequestBody, (obj, key) -> obj.getRequestBodies().containsKey(key));
assertAddIgnoresNull(new ComponentsImpl(), Components::addResponse, (obj, key) -> obj.getResponses().containsKey(key));
assertAddIgnoresNull(new ComponentsImpl(), Components::addSchema, (obj, key) -> obj.getSchemas().containsKey(key));
assertAddIgnoresNull(new ComponentsImpl(), Components::addSecurityScheme, (obj, key) -> obj.getSecuritySchemes().containsKey(key));
assertAddIgnoresNull(new ComponentsImpl(), Components::addExtension, hasExtension);
assertAddIgnoresNull(new ExternalDocumentationImpl(), ExternalDocumentation::addExtension, hasExtension);
assertAddIgnoresNull(new OpenAPIImpl(), OpenAPI::addExtension, hasExtension);
assertAddIgnoresNull(new OperationImpl(), Operation::addCallback, (obj, key) -> obj.getCallbacks().containsKey(key));
assertAddIgnoresNull(new OperationImpl(), Operation::addExtension, hasExtension);
assertAddIgnoresNull(new PathItemImpl(), PathItem::addExtension, hasExtension);
assertAddIgnoresNull(new PathsImpl(), Paths::addPathItem, Paths::hasPathItem);
assertAddIgnoresNull(new PathsImpl(), Paths::addExtension, hasExtension);
}
use of fish.payara.microprofile.openapi.impl.model.media.SchemaImpl 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);
}
}
use of fish.payara.microprofile.openapi.impl.model.media.SchemaImpl 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;
}
use of fish.payara.microprofile.openapi.impl.model.media.SchemaImpl 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);
}
}
}
}
use of fish.payara.microprofile.openapi.impl.model.media.SchemaImpl in project Payara by payara.
the class ComponentsImpl method createInstance.
public static Components createInstance(AnnotationModel annotation, ApiContext context) {
Components from = new ComponentsImpl();
extractAnnotations(annotation, context, "schemas", "name", SchemaImpl::createInstance, from::addSchema);
extractAnnotations(annotation, context, "responses", "name", APIResponseImpl::createInstance, from::addResponse);
extractAnnotations(annotation, context, "parameters", "name", ParameterImpl::createInstance, from::addParameter);
extractAnnotations(annotation, context, "examples", "name", ExampleImpl::createInstance, from::addExample);
extractAnnotations(annotation, context, "requestBodies", "name", RequestBodyImpl::createInstance, from::addRequestBody);
extractAnnotations(annotation, context, "securitySchemes", "securitySchemeName", SecuritySchemeImpl::createInstance, from::addSecurityScheme);
extractAnnotations(annotation, context, "links", "name", LinkImpl::createInstance, from::addLink);
extractAnnotations(annotation, context, "callbacks", "name", CallbackImpl::createInstance, from::addCallback);
HeaderImpl.createInstances(annotation, context).forEach(from::addHeader);
return from;
}
Aggregations