use of fish.payara.microprofile.openapi.api.visitor.ApiContext in project Payara by payara.
the class ApplicationProcessor method visitAPIResponse.
@Override
public void visitAPIResponse(AnnotationModel annotation, AnnotatedElement element, ApiContext context) {
APIResponseImpl apiResponse = APIResponseImpl.createInstance(annotation, context);
Operation workingOperation = context.getWorkingOperation();
// Handle exception mappers
if (workingOperation == null) {
if (element instanceof MethodModel && "toResponse".equals(element.getName())) {
final MethodModel methodModel = (MethodModel) element;
final String exceptionType = methodModel.getParameter(0).getTypeName();
mapException(context, exceptionType, apiResponse);
} else {
LOGGER.warning("Unrecognised annotation position at: " + element.shortDesc());
}
return;
}
APIResponsesImpl.merge(apiResponse, workingOperation.getResponses(), true, context);
// If an APIResponse has been processed that isn't the default
String responseCode = apiResponse.getResponseCode();
if (responseCode != null && !responseCode.isEmpty() && !responseCode.equals(APIResponses.DEFAULT)) {
// If the element doesn't also contain a response mapping to the default
AnnotationModel apiResponsesParent = element.getAnnotation(org.eclipse.microprofile.openapi.annotations.responses.APIResponses.class.getName());
if (apiResponsesParent != null) {
List<AnnotationModel> apiResponses = apiResponsesParent.getValue("value", List.class);
if (apiResponses.stream().map(a -> a.getValue("responseCode", String.class)).noneMatch(code -> code == null || code.isEmpty() || code.equals(APIResponses.DEFAULT))) {
// Then remove the default response
workingOperation.getResponses().removeAPIResponse(APIResponses.DEFAULT);
}
} else {
workingOperation.getResponses().removeAPIResponse(APIResponses.DEFAULT);
}
}
}
use of fish.payara.microprofile.openapi.api.visitor.ApiContext in project Payara by payara.
the class ApplicationProcessor method findOperationParameterFor.
private static Parameter findOperationParameterFor(Parameter parameter, MethodModel annotated, ApiContext context) {
String name = parameter.getName();
// If the parameter reference is valid
if (name != null && !name.isEmpty()) {
// Get all parameters with the same name
List<org.glassfish.hk2.classmodel.reflect.Parameter> matchingMethodParameters = annotated.getParameters().stream().filter(x -> name.equals(ModelUtils.getParameterName(context, x))).collect(Collectors.toList());
// If there is more than one match, filter it further
In in = parameter.getIn();
if (matchingMethodParameters.size() > 1 && in != null) {
// Remove all parameters of the wrong input type
matchingMethodParameters.removeIf(x -> ModelUtils.getParameterType(context, x) != In.valueOf(in.name()));
}
if (matchingMethodParameters.isEmpty()) {
return null;
}
// If there's only one matching parameter, handle it immediately
String matchingMethodParamName = ModelUtils.getParameterName(context, matchingMethodParameters.get(0));
// Find the matching operation parameter
for (Parameter operationParam : context.getWorkingOperation().getParameters()) {
if (operationParam.getName().equals(matchingMethodParamName)) {
return operationParam;
}
}
}
return null;
}
Aggregations