use of org.wildfly.swarm.microprofile.openapi.api.models.media.ContentImpl in project wildfly-swarm by wildfly-swarm.
the class OpenApiParser method readContent.
/**
* Reads a {@link Content} OpenAPI node.
* @param node
*/
private Content readContent(JsonNode node) {
if (node == null || !node.isObject()) {
return null;
}
ContentImpl model = new ContentImpl();
for (Iterator<String> fieldNames = node.fieldNames(); fieldNames.hasNext(); ) {
String fieldName = fieldNames.next();
model.addMediaType(fieldName, readMediaType(node.get(fieldName)));
}
return model;
}
use of org.wildfly.swarm.microprofile.openapi.api.models.media.ContentImpl 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.wildfly.swarm.microprofile.openapi.api.models.media.ContentImpl in project wildfly-swarm by wildfly-swarm.
the class OpenApiAnnotationScanner method createResponseFromJaxRsMethod.
/**
* Called when a jax-rs method's APIResponse annotations have all been processed but
* no response was actually created for the operation. This method will create a response
* from the method information and add it to the given operation. It will try to do this
* by examining the method's return value and the type of operation (GET, PUT, POST, DELETE).
*
* If there is a return value of some kind (a non-void return type) then the response code
* is assumed to be 200.
*
* If there not a return value (void return type) then either a 201 or 204 is returned,
* depending on the type of request.
*
* TODO generate responses for each checked exception?
* @param method
* @param operation
*/
private void createResponseFromJaxRsMethod(MethodInfo method, Operation operation) {
Type returnType = method.returnType();
Schema schema;
APIResponses responses;
APIResponse response;
ContentImpl content;
if (returnType.kind() == Type.Kind.VOID) {
String code = "204";
if (method.hasAnnotation(OpenApiConstants.DOTNAME_POST)) {
code = "201";
}
responses = ModelUtil.responses(operation);
response = new APIResponseImpl();
responses.addApiResponse(code, response);
} else {
schema = typeToSchema(returnType);
responses = ModelUtil.responses(operation);
response = new APIResponseImpl();
content = new ContentImpl();
String[] produces = this.currentProduces;
if (produces == null || produces.length == 0) {
produces = OpenApiConstants.DEFAULT_PRODUCES;
}
for (String producesType : produces) {
MediaType mt = new MediaTypeImpl();
mt.setSchema(schema);
content.addMediaType(producesType, mt);
}
response.setContent(content);
responses.addApiResponse("200", response);
}
}
use of org.wildfly.swarm.microprofile.openapi.api.models.media.ContentImpl 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