use of fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl 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 fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl 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 fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl in project Payara by payara.
the class ComponentsImpl method merge.
public static void merge(Components from, Components to, boolean override, ApiContext context) {
if (from == null) {
return;
}
// Handle @Schema
if (from.getSchemas() != null) {
for (Entry<String, Schema> fromEntry : from.getSchemas().entrySet()) {
final String schemaName = fromEntry.getKey();
if (schemaName != null) {
final Schema fromSchema = fromEntry.getValue();
final Schema toSchema = to.getSchemas().getOrDefault(schemaName, new SchemaImpl());
SchemaImpl.merge(fromSchema, toSchema, override, context);
to.addSchema(schemaName, toSchema);
}
}
}
// Handle @Callback
if (from.getCallbacks() != null) {
for (String callbackName : from.getCallbacks().keySet()) {
if (callbackName != null) {
Callback newCallback = new CallbackImpl();
CallbackImpl.merge(from.getCallbacks().get(callbackName), newCallback, override, context);
to.addCallback(callbackName, newCallback);
}
}
}
// Handle @ExampleObject
if (from.getExamples() != null) {
for (String exampleName : from.getExamples().keySet()) {
if (exampleName != null) {
Example newExample = new ExampleImpl();
ExampleImpl.merge(from.getExamples().get(exampleName), newExample, override);
to.addExample(exampleName, newExample);
}
}
}
// Handle @Header
if (from.getHeaders() != null) {
for (String headerName : from.getHeaders().keySet()) {
if (headerName != null) {
Header newHeader = new HeaderImpl();
HeaderImpl.merge(from.getHeaders().get(headerName), newHeader, override, context);
to.addHeader(headerName, newHeader);
}
}
}
// Handle @Link
if (from.getLinks() != null) {
for (String linkName : from.getLinks().keySet()) {
if (linkName != null) {
Link newLink = new LinkImpl();
LinkImpl.merge(from.getLinks().get(linkName), newLink, override);
to.addLink(linkName, newLink);
}
}
}
// Handle @Parameter
if (from.getParameters() != null) {
for (String parameterName : from.getParameters().keySet()) {
if (parameterName != null) {
Parameter newParameter = new ParameterImpl();
ParameterImpl.merge(from.getParameters().get(parameterName), newParameter, override, context);
to.addParameter(parameterName, newParameter);
}
}
}
// Handle @RequestBody
if (from.getRequestBodies() != null) {
for (String requestBodyName : from.getRequestBodies().keySet()) {
if (requestBodyName != null) {
RequestBody newRequestBody = new RequestBodyImpl();
RequestBodyImpl.merge(from.getRequestBodies().get(requestBodyName), newRequestBody, override, context);
to.addRequestBody(requestBodyName, newRequestBody);
}
}
}
// Handle @APIResponse
if (from.getResponses() != null) {
for (String responseName : from.getResponses().keySet()) {
if (responseName != null) {
APIResponse newResponse = new APIResponseImpl();
APIResponseImpl.merge(from.getResponses().get(responseName), newResponse, override, context);
to.addResponse(responseName, newResponse);
}
}
}
// Handle @SecurityScheme
if (from.getSecuritySchemes() != null) {
for (String securitySchemeName : from.getSecuritySchemes().keySet()) {
if (securitySchemeName != null) {
SecurityScheme newSecurity = new SecuritySchemeImpl();
SecuritySchemeImpl.merge(from.getSecuritySchemes().get(securitySchemeName), newSecurity, override);
to.addSecurityScheme(securitySchemeName, newSecurity);
}
}
}
}
use of fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl in project Payara by payara.
the class OperationImpl method createInstance.
public static Operation createInstance(AnnotationModel annotation, ApiContext context) {
OperationImpl from = new OperationImpl();
from.setSummary(annotation.getValue("summary", String.class));
from.setDescription(annotation.getValue("description", String.class));
AnnotationModel externalDocs = annotation.getValue("externalDocs", AnnotationModel.class);
if (externalDocs != null) {
from.setExternalDocs(ExternalDocumentationImpl.createInstance(externalDocs));
}
from.setOperationId(annotation.getValue("operationId", String.class));
extractAnnotations(annotation, context, "parameters", ParameterImpl::createInstance, from::addParameter);
AnnotationModel requestBody = annotation.getValue("requestBody", AnnotationModel.class);
if (requestBody != null) {
from.setRequestBody(RequestBodyImpl.createInstance(requestBody, context));
}
extractAnnotations(annotation, context, "responses", "responseCode", APIResponseImpl::createInstance, from.responses::addAPIResponse);
extractAnnotations(annotation, context, "callbacks", "name", CallbackImpl::createInstance, from::addCallback);
from.setDeprecated(annotation.getValue("deprecated", Boolean.class));
extractAnnotations(annotation, context, "security", SecurityRequirementImpl::createInstance, from::addSecurityRequirement);
extractAnnotations(annotation, context, "servers", ServerImpl::createInstance, from::addServer);
from.setMethod(annotation.getValue("method", String.class));
return from;
}
use of fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl in project Payara by payara.
the class ResponseTest method defaultPostVoidSchemaTest.
@Test
public void defaultPostVoidSchemaTest() {
APIResponses responses = getDocument().getPaths().getPathItem("/test/response/schema").getPOST().getResponses();
checkResponseCode(responses, 201);
final APIResponseImpl response = (APIResponseImpl) responses.getAPIResponse("201");
checkMediaTypesExist(response, APPLICATION_JSON, APPLICATION_XML);
checkSchemaDescriptions(response, "Custom Response");
}
Aggregations