use of org.eclipse.microprofile.openapi.models.media.Content in project wildfly-swarm by wildfly-swarm.
the class ModelUtil method setParameterSchema.
/**
* Sets the given {@link Schema} on the given {@link Parameter}. This is tricky
* because the paramater may EITHER have a schema property or it may have a
* {@link Content} child which itself has zero or more {@link MediaType} children
* which will contain the {@link Schema}.
*
* The OpenAPI specification requires that a parameter have *either* a schema
* or a content, but not both.
* @param parameter
* @param schema
*/
public static void setParameterSchema(Parameter parameter, Schema schema) {
if (schema == null) {
return;
}
if (parameter.getContent() == null) {
parameter.schema(schema);
return;
}
Content content = parameter.getContent();
if (content.isEmpty()) {
String[] defMediaTypes = OpenApiConstants.DEFAULT_PARAMETER_MEDIA_TYPES;
for (String mediaTypeName : defMediaTypes) {
MediaType mediaType = new MediaTypeImpl();
mediaType.setSchema(schema);
content.addMediaType(mediaTypeName, mediaType);
}
return;
}
for (String mediaTypeName : content.keySet()) {
MediaType mediaType = content.get(mediaTypeName);
mediaType.setSchema(schema);
}
}
use of org.eclipse.microprofile.openapi.models.media.Content 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 org.eclipse.microprofile.openapi.models.media.Content in project wildfly-swarm by wildfly-swarm.
the class OpenApiAnnotationScanner method readContent.
/**
* Reads a single Content annotation into a model. The value in this case is an array of
* Content annotations.
* @param value
*/
private Content readContent(AnnotationValue value, ContentDirection direction) {
if (value == null) {
return null;
}
LOG.debug("Processing a single @Content annotation.");
Content content = new ContentImpl();
AnnotationInstance[] nestedArray = value.asNestedArray();
for (AnnotationInstance nested : nestedArray) {
String contentType = JandexUtil.stringValue(nested, OpenApiConstants.PROP_MEDIA_TYPE);
MediaType mediaTypeModel = readMediaType(nested);
if (contentType == null) {
// If the content type is not provided in the @Content annotation, then
// we assume it applies to all the jax-rs method's @Consumes or @Produces
String[] mimeTypes = {};
if (direction == ContentDirection.Input && currentConsumes != null) {
mimeTypes = currentConsumes;
}
if (direction == ContentDirection.Output && currentProduces != null) {
mimeTypes = currentProduces;
}
if (direction == ContentDirection.Parameter) {
mimeTypes = OpenApiConstants.DEFAULT_PARAMETER_MEDIA_TYPES;
}
for (String mimeType : mimeTypes) {
content.addMediaType(mimeType, mediaTypeModel);
}
} else {
content.addMediaType(contentType, mediaTypeModel);
}
}
return content;
}
use of org.eclipse.microprofile.openapi.models.media.Content in project wildfly-swarm by wildfly-swarm.
the class ModelUtil method setRequestBodySchema.
/**
* Sets the given {@link Schema} on the given {@link RequestBody}.
* @param requestBody
* @param schema
* @param mediaTypes
*/
public static void setRequestBodySchema(RequestBody requestBody, Schema schema, String[] mediaTypes) {
Content content = requestBody.getContent();
if (content == null) {
content = new ContentImpl();
requestBody.setContent(content);
}
if (content.isEmpty()) {
String[] requestBodyTypes = OpenApiConstants.DEFAULT_REQUEST_BODY_TYPES;
if (mediaTypes != null && mediaTypes.length > 0) {
requestBodyTypes = mediaTypes;
}
for (String mediaTypeName : requestBodyTypes) {
MediaType mediaType = new MediaTypeImpl();
mediaType.setSchema(schema);
content.addMediaType(mediaTypeName, mediaType);
}
return;
}
for (String mediaTypeName : content.keySet()) {
MediaType mediaType = content.get(mediaTypeName);
mediaType.setSchema(schema);
}
}
Aggregations