use of org.eclipse.microprofile.openapi.models.media.MediaType in project Payara by payara.
the class ContentImpl method createInstance.
public static ContentImpl createInstance(AnnotationModel annotation, ApiContext context) {
ContentImpl from = new ContentImpl();
String typeName = annotation.getValue("mediaType", String.class);
if (typeName == null || typeName.isEmpty()) {
typeName = javax.ws.rs.core.MediaType.WILDCARD;
}
MediaType mediaType = new MediaTypeImpl();
from.addMediaType(typeName, mediaType);
extractAnnotations(annotation, context, "examples", "name", ExampleImpl::createInstance, mediaType::addExample);
mediaType.setExample(annotation.getValue("example", String.class));
AnnotationModel schemaAnnotation = annotation.getValue("schema", AnnotationModel.class);
if (schemaAnnotation != null) {
Boolean hidden = schemaAnnotation.getValue("hidden", Boolean.class);
if (hidden == null || !hidden) {
mediaType.setSchema(SchemaImpl.createInstance(schemaAnnotation, context));
}
}
extractAnnotations(annotation, context, "encoding", "name", EncodingImpl::createInstance, mediaType::addEncoding);
return from;
}
use of org.eclipse.microprofile.openapi.models.media.MediaType in project Payara by payara.
the class ApplicationProcessor method insertDefaultRequestBody.
// PRIVATE METHODS
private RequestBody insertDefaultRequestBody(ApiContext context, Operation operation, MethodModel method) {
RequestBody requestBody = new RequestBodyImpl();
// Get the request body type of the method
org.glassfish.hk2.classmodel.reflect.ParameterizedType bodyType = null;
for (org.glassfish.hk2.classmodel.reflect.Parameter methodParam : method.getParameters()) {
if (ModelUtils.isRequestBody(context, methodParam)) {
bodyType = methodParam;
break;
}
}
if (bodyType == null) {
return null;
}
// Create the default request body with a wildcard mediatype
MediaType mediaType = new MediaTypeImpl().schema(createSchema(context, bodyType));
requestBody.getContent().addMediaType(javax.ws.rs.core.MediaType.WILDCARD, mediaType);
operation.setRequestBody(requestBody);
return requestBody;
}
use of org.eclipse.microprofile.openapi.models.media.MediaType in project Payara by payara.
the class ApplicationProcessor method visitRequestBodySchema.
@Override
public void visitRequestBodySchema(AnnotationModel requestBodySchema, AnnotatedElement element, ApiContext context) {
if (element instanceof MethodModel || element instanceof org.glassfish.hk2.classmodel.reflect.Parameter) {
final RequestBody currentRequestBody = context.getWorkingOperation().getRequestBody();
if (currentRequestBody != null) {
final String implementationClass = requestBodySchema.getValue("value", String.class);
final SchemaImpl schema = SchemaImpl.fromImplementation(implementationClass, context);
for (MediaType mediaType : currentRequestBody.getContent().getMediaTypes().values()) {
mediaType.setSchema(schema);
}
}
}
}
use of org.eclipse.microprofile.openapi.models.media.MediaType in project Payara by payara.
the class ApplicationProcessor method visitSchemaParameter.
private static void visitSchemaParameter(AnnotationModel schemaAnnotation, org.glassfish.hk2.classmodel.reflect.Parameter parameter, ApiContext context) {
// If this is being parsed at the start, ignore it as the path doesn't exist
if (context.getWorkingOperation() == null) {
return;
}
// Check if it's a request body
if (ModelUtils.isRequestBody(context, parameter)) {
if (context.getWorkingOperation().getRequestBody() == null) {
context.getWorkingOperation().setRequestBody(new RequestBodyImpl());
}
// Insert the schema to the request body media type
MediaType mediaType = context.getWorkingOperation().getRequestBody().getContent().getMediaType(javax.ws.rs.core.MediaType.WILDCARD);
Schema schema = SchemaImpl.createInstance(schemaAnnotation, context);
SchemaImpl.merge(schema, mediaType.getSchema(), true, context);
if (schema.getRef() != null && !schema.getRef().isEmpty()) {
mediaType.setSchema(new SchemaImpl().ref(schema.getRef()));
}
} else if (ModelUtils.getParameterType(context, parameter) != null) {
for (Parameter param : context.getWorkingOperation().getParameters()) {
if (param.getName().equals(ModelUtils.getParameterName(context, parameter))) {
Schema schema = SchemaImpl.createInstance(schemaAnnotation, context);
SchemaImpl.merge(schema, param.getSchema(), true, context);
if (schema.getRef() != null && !schema.getRef().isEmpty()) {
param.setSchema(new SchemaImpl().ref(schema.getRef()));
}
}
}
}
}
use of org.eclipse.microprofile.openapi.models.media.MediaType 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);
}
Aggregations