use of org.eclipse.microprofile.openapi.models.responses.APIResponses 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.APIResponses 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");
}
use of org.eclipse.microprofile.openapi.models.responses.APIResponses in project Payara by payara.
the class GenericSchemaMappingTest method genericSchemaTest.
@Test
public void genericSchemaTest() {
APIResponses responses = getDocument().getPaths().getPathItem("/test/response").getGET().getResponses();
assertNotNull("The default response should have been created.", responses.getDefaultValue());
assertNotNull("The default response should return */*.", responses.getDefaultValue().getContent().getMediaType(WILDCARD));
assertEquals("The default response */* should match the specified schema.", "#/components/schemas/JsonAnimalList", responses.getDefaultValue().getContent().getMediaType(WILDCARD).getSchema().getRef());
Map<String, Schema> schemas = getDocument().getComponents().getSchemas();
assertEquals(2, schemas.size());
Schema jsonAnimalList = schemas.get("JsonAnimalList");
assertNotNull(jsonAnimalList);
assertEquals(1, jsonAnimalList.getProperties().size());
assertEquals("JSON wrapper for a list of animals", jsonAnimalList.getDescription());
assertEquals(SchemaType.OBJECT, jsonAnimalList.getType());
Schema data = jsonAnimalList.getProperties().get("data");
assertNotNull(data);
assertEquals(5, data.getProperties().size());
assertEquals(SchemaType.OBJECT, data.getType());
Schema totalItems = data.getProperties().get("totalItems");
assertNotNull(totalItems);
assertEquals(SchemaType.INTEGER, totalItems.getType());
Schema items = data.getProperties().get("items");
assertNotNull(items);
assertEquals(SchemaType.ARRAY, items.getType());
assertEquals("#/components/schemas/Animal", items.getItems().getRef());
Schema itemMap = data.getProperties().get("itemMap");
assertNotNull(itemMap);
assertEquals(SchemaType.OBJECT, itemMap.getType());
assertNotNull(itemMap.getAdditionalPropertiesSchema());
assertEquals("#/components/schemas/Animal", itemMap.getAdditionalPropertiesSchema().getRef());
Schema textMap = data.getProperties().get("textMap");
assertNotNull(textMap);
assertEquals(SchemaType.OBJECT, textMap.getType());
assertNotNull(textMap.getAdditionalPropertiesSchema());
assertEquals(SchemaType.STRING, textMap.getAdditionalPropertiesSchema().getType());
Schema itemsMap = data.getProperties().get("itemsMap");
assertNotNull(itemsMap);
assertEquals(SchemaType.OBJECT, itemsMap.getType());
assertNotNull(itemsMap.getAdditionalPropertiesSchema());
assertEquals(SchemaType.ARRAY, itemsMap.getAdditionalPropertiesSchema().getType());
assertEquals("#/components/schemas/Animal", itemsMap.getAdditionalPropertiesSchema().getItems().getRef());
Schema animal = schemas.get("Animal");
assertNotNull(animal);
assertEquals(2, animal.getProperties().size());
assertEquals(SchemaType.OBJECT, animal.getType());
Schema name = animal.getProperties().get("name");
assertNotNull(name);
assertEquals(SchemaType.STRING, name.getType());
Schema age = animal.getProperties().get("age");
assertNotNull(age);
assertEquals(SchemaType.INTEGER, age.getType());
}
use of org.eclipse.microprofile.openapi.models.responses.APIResponses 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.responses.APIResponses in project Payara by payara.
the class ResponseTest method defaultDeleteStringSchemaTest.
@Test
public void defaultDeleteStringSchemaTest() {
APIResponses responses = getDocument().getPaths().getPathItem("/test/response/schema").getDELETE().getResponses();
checkResponseCode(responses, 200);
final APIResponseImpl response = (APIResponseImpl) responses.getAPIResponse("200");
checkMediaTypesExist(response, APPLICATION_JSON, APPLICATION_XML);
checkSchemaDescriptions(response, "Custom Response");
}
Aggregations