Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PAYARA-3155 @...Param annotations doesn't support @DefaultValue annotations on OpenAPI #3252

Merged
merged 1 commit into from Oct 9, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -125,6 +125,7 @@
import fish.payara.microprofile.openapi.impl.model.util.ModelUtils;
import fish.payara.microprofile.openapi.impl.visitor.OpenApiContext;
import fish.payara.microprofile.openapi.impl.visitor.OpenApiWalker;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import org.eclipse.microprofile.openapi.models.parameters.Parameter.Style;

Expand Down Expand Up @@ -429,6 +430,7 @@ public void visitCookieParam(CookieParam param, AnnotatedElement element, ApiCon
private void addParameter(AnnotatedElement element, ApiContext context,
org.eclipse.microprofile.openapi.models.parameters.Parameter newParameter) {
SchemaImpl schema = new SchemaImpl();
String defaultValue = getDefaultValueIfPresent(element);

if (element instanceof java.lang.reflect.Parameter) {
java.lang.reflect.Parameter parameter = java.lang.reflect.Parameter.class.cast(element);
Expand All @@ -440,7 +442,13 @@ private void addParameter(AnnotatedElement element, ApiContext context,

if (schema.getType() == SchemaType.ARRAY) {
schema.setItems(getArraySchema(element));
if (defaultValue != null) {
schema.getItems().setDefaultValue(defaultValue);
}
} else if (defaultValue != null) {
schema.setDefaultValue(defaultValue);
}

newParameter.setSchema(schema);

if (context.getWorkingOperation() != null) {
Expand Down Expand Up @@ -482,7 +490,21 @@ private SchemaImpl getArraySchema(AnnotatedElement element) {
arraySchema.setType(ModelUtils.getSchemaType((Class<?>) parameterizedType.getActualTypeArguments()[0]));
return arraySchema;
}


private String getDefaultValueIfPresent(AnnotatedElement element) {
Annotation[] annotations = element.getDeclaredAnnotations();
for (Annotation annotation : annotations) {
if ("javax.ws.rs.DefaultValue".equals(annotation.annotationType().getName())) {
try {
return (String) annotation.annotationType().getMethod("value").invoke(annotation);
} catch (Exception ex) {
LOGGER.log(WARNING, "Couldn't get the default value", ex);
}
}
}
return null;
}

@Override
public void visitOpenAPI(OpenAPIDefinition definition, AnnotatedElement element, ApiContext context) {
OpenAPIImpl.merge(definition, context.getApi(), true);
Expand Down