use of org.eclipse.microprofile.openapi.models.responses.APIResponse in project wildfly-swarm by wildfly-swarm.
the class OpenApiParser method readResponses.
/**
* Reads the {@link APIResponse} OpenAPI nodes.
* @param node
*/
private Map<String, APIResponse> readResponses(JsonNode node) {
if (node == null || !node.isObject()) {
return null;
}
Map<String, APIResponse> models = new LinkedHashMap<>();
for (Iterator<String> fieldNames = node.fieldNames(); fieldNames.hasNext(); ) {
String fieldName = fieldNames.next();
JsonNode childNode = node.get(fieldName);
models.put(fieldName, readAPIResponse(childNode));
}
return models;
}
use of org.eclipse.microprofile.openapi.models.responses.APIResponse in project wildfly-swarm by wildfly-swarm.
the class FilterUtil method filterAPIResponses.
/**
* Filters the given models.
* @param filter
* @param models
*/
private static void filterAPIResponses(OASFilter filter, Map<String, APIResponse> models) {
if (models == null) {
return;
}
Collection<String> keys = new ArrayList<>(models.keySet());
for (String key : keys) {
APIResponse model = models.get(key);
filterAPIResponse(filter, model);
if (filter.filterAPIResponse(model) == null) {
models.remove(key);
}
}
}
use of org.eclipse.microprofile.openapi.models.responses.APIResponse in project Payara by payara.
the class ApplicationProcessor method visitAPIResponseSchema.
@Override
public void visitAPIResponseSchema(AnnotationModel apiResponseSchema, AnnotatedElement element, ApiContext context) {
final APIResponseImpl response = APIResponseImpl.createInstance(apiResponseSchema, context);
final OperationImpl operation = (OperationImpl) context.getWorkingOperation();
// Handle exception mappers
if (operation == null) {
if (element instanceof MethodModel && "toResponse".equals(element.getName())) {
final MethodModel methodModel = (MethodModel) element;
final String exceptionType = methodModel.getParameter(0).getTypeName();
mapException(context, exceptionType, response);
} else {
LOGGER.warning("Unrecognised annotation position at: " + element.shortDesc());
}
return;
}
// If response code hasn't been specified
String responseCode = response.getResponseCode();
if (responseCode == null || responseCode.isEmpty()) {
assert element instanceof MethodModel;
final MethodModel method = (MethodModel) element;
if (isVoid(method.getReturnType())) {
if (HttpMethod.POST.equals(operation.getMethod())) {
responseCode = "201";
} else if (Arrays.asList(method.getArgumentTypes()).contains("javax.ws.rs.container.AsyncResponse")) {
responseCode = "200";
} else {
responseCode = "204";
}
} else {
responseCode = "200";
}
}
response.setResponseCode(responseCode);
// If the response description hasn't been specified
final String responseDescription = response.getDescription();
if (responseDescription == null || responseDescription.isEmpty()) {
try {
final int statusInt = Integer.parseInt(responseCode);
final Status status = Status.fromStatusCode(statusInt);
if (status != null) {
response.setDescription(status.getReasonPhrase());
}
} catch (NumberFormatException ex) {
LOGGER.log(Level.FINE, "Unrecognised status code, description will be empty", ex);
}
}
final APIResponses responses = operation.getResponses();
// Remove the default response
final APIResponse defaultResponse = responses.getAPIResponse(APIResponses.DEFAULT);
if (defaultResponse != null) {
responses.removeAPIResponse(APIResponses.DEFAULT);
responses.addAPIResponse(responseCode, defaultResponse);
}
// Add the generated response
APIResponsesImpl.merge(response, responses, true, context);
}
use of org.eclipse.microprofile.openapi.models.responses.APIResponse in project Payara by payara.
the class ApplicationProcessor method insertDefaultResponse.
/**
* Creates a new {@link APIResponse} to model the default response of a
* {@link Method}, and inserts it into the {@link Operation} responses.
*
* @param context the API context.
* @param operation the {@link Operation} to add the default response to.
* @param method the {@link Method} to model the default response on.
* @return the newly created {@link APIResponse}.
*/
private void insertDefaultResponse(ApiContext context, OperationImpl operation, MethodModel method) {
final APIResponsesImpl responses = new APIResponsesImpl();
operation.setResponses(responses);
// Add the default response
APIResponse defaultResponse = new APIResponseImpl();
responses.addAPIResponse(APIResponses.DEFAULT, defaultResponse);
defaultResponse.setDescription("Default Response.");
// Configure the default response with a wildcard mediatype
MediaType mediaType = new MediaTypeImpl().schema(createSchema(context, method.getReturnType()));
defaultResponse.getContent().addMediaType(javax.ws.rs.core.MediaType.WILDCARD, mediaType);
// Add responses for the applicable declared exceptions
for (String exceptionType : method.getExceptionTypes()) {
final APIResponseImpl mappedResponse = (APIResponseImpl) context.getMappedExceptionResponses().get(exceptionType);
if (mappedResponse != null) {
final String responseCode = mappedResponse.getResponseCode();
if (responseCode != null) {
responses.addAPIResponse(responseCode, mappedResponse);
}
}
operation.addExceptionType(exceptionType);
}
}
use of org.eclipse.microprofile.openapi.models.responses.APIResponse in project Payara by payara.
the class ApplicationProcessor method visitProduces.
@Override
public void visitProduces(AnnotationModel produces, AnnotatedElement element, ApiContext context) {
if (element instanceof MethodModel && context.getWorkingOperation() != null) {
for (APIResponse response : context.getWorkingOperation().getResponses().getAPIResponses().values()) {
if (response != null) {
// Find the wildcard return type
if (response.getContent() != null && response.getContent().getMediaType(javax.ws.rs.core.MediaType.WILDCARD) != null) {
MediaType wildcardMedia = response.getContent().getMediaType(javax.ws.rs.core.MediaType.WILDCARD);
// Merge the wildcard return type with the valid response types
// This keeps the specific details of a reponse type that has a schema
List<String> mediaTypes = produces.getValue("value", List.class);
for (String mediaType : mediaTypes) {
MediaType held = response.getContent().getMediaType(getContentType(mediaType));
if (held == null) {
response.getContent().addMediaType(getContentType(mediaType), wildcardMedia);
} else {
MediaTypeImpl.merge(held, wildcardMedia, true);
}
}
// If there is an @Produces, remove the wildcard
response.getContent().removeMediaType(javax.ws.rs.core.MediaType.WILDCARD);
}
}
}
}
}
Aggregations