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-3085 OpenAPI Doesn't Support @...Param Annotated Fields #3163

Merged
merged 6 commits into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import javax.ws.rs.Consumes;
Expand Down Expand Up @@ -108,16 +109,16 @@ interface VisitorFunction<A extends Annotation, E extends AnnotatedElement> {

void visitConsumes(Consumes produces, AnnotatedElement element, ApiContext context);

void visitQueryParam(QueryParam param, java.lang.reflect.Parameter element, ApiContext context);
void visitQueryParam(QueryParam param, AnnotatedElement element, ApiContext context);

void visitPathParam(PathParam param, java.lang.reflect.Parameter element, ApiContext context);
void visitPathParam(PathParam param, AnnotatedElement element, ApiContext context);

void visitFormParam(FormParam param, java.lang.reflect.Parameter element, ApiContext context);
void visitFormParam(FormParam param, AnnotatedElement element, ApiContext context);

void visitHeaderParam(HeaderParam param, java.lang.reflect.Parameter element, ApiContext context);

void visitCookieParam(CookieParam param, java.lang.reflect.Parameter element, ApiContext context);
void visitHeaderParam(HeaderParam param, AnnotatedElement element, ApiContext context);

void visitCookieParam(CookieParam param, AnnotatedElement element, ApiContext context);

// OpenAPI annotations

void visitOpenAPI(OpenAPIDefinition definition, AnnotatedElement element, ApiContext context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,35 +356,55 @@ public void visitConsumes(Consumes consumes, AnnotatedElement element, ApiContex
}

@Override
public void visitQueryParam(QueryParam param, java.lang.reflect.Parameter element, ApiContext context) {
public void visitQueryParam(QueryParam param, AnnotatedElement element, ApiContext context) {
org.eclipse.microprofile.openapi.models.parameters.Parameter newParameter = new ParameterImpl();
newParameter.setName(param.value());
newParameter.setIn(In.QUERY);
newParameter.setStyle(Style.SIMPLE);
newParameter.setSchema(new SchemaImpl().type(ModelUtils.getSchemaType(element.getType())));
if (element instanceof java.lang.reflect.Parameter) {
newParameter.setSchema(new SchemaImpl().type(ModelUtils
.getSchemaType(java.lang.reflect.Parameter.class.cast(element).getType())));
} else {
newParameter.setSchema(new SchemaImpl().type(ModelUtils
.getSchemaType(Field.class.cast(element).getType())));
}
context.getWorkingOperation().addParameter(newParameter);
}

@Override
public void visitPathParam(PathParam param, java.lang.reflect.Parameter element, ApiContext context) {
public void visitPathParam(PathParam param, AnnotatedElement element, ApiContext context) {
org.eclipse.microprofile.openapi.models.parameters.Parameter newParameter = new ParameterImpl();
newParameter.setName(param.value());
newParameter.setRequired(true);
newParameter.setIn(In.PATH);
newParameter.setStyle(Style.SIMPLE);
newParameter.setSchema(new SchemaImpl().type(ModelUtils.getSchemaType(element.getType())));
if (element instanceof java.lang.reflect.Parameter) {
newParameter.setSchema(new SchemaImpl().type(ModelUtils
.getSchemaType(java.lang.reflect.Parameter.class.cast(element).getType())));
} else {
newParameter.setSchema(new SchemaImpl().type(ModelUtils
.getSchemaType(Field.class.cast(element).getType())));
}
context.getWorkingOperation().addParameter(newParameter);
}

@Override
public void visitFormParam(FormParam param, java.lang.reflect.Parameter element, ApiContext context) {
public void visitFormParam(FormParam param, AnnotatedElement element, ApiContext context) {
// Find the aggregate schema type of all the parameters
SchemaType formSchemaType = null;
for (java.lang.reflect.Parameter methodParam : element.getDeclaringExecutable().getParameters()) {
if (methodParam.isAnnotationPresent(FormParam.class)) {
formSchemaType = ModelUtils.getParentSchemaType(formSchemaType,
ModelUtils.getSchemaType(methodParam.getType()));

if (element instanceof java.lang.reflect.Parameter) {
java.lang.reflect.Parameter[] parameters = java.lang.reflect.Parameter.class.cast(element)
.getDeclaringExecutable().getParameters();
for (java.lang.reflect.Parameter methodParam : parameters) {
if (methodParam.isAnnotationPresent(FormParam.class)) {
formSchemaType = ModelUtils.getParentSchemaType(formSchemaType,
ModelUtils.getSchemaType(methodParam.getType()));
}
}
} else {
formSchemaType = ModelUtils.getParentSchemaType(formSchemaType,
ModelUtils.getSchemaType(Field.class.cast(element).getType()));
}

// If there's no request body, fill out a new one right down to the schema
Expand All @@ -399,22 +419,34 @@ public void visitFormParam(FormParam param, java.lang.reflect.Parameter element,
}

@Override
public void visitHeaderParam(HeaderParam param, java.lang.reflect.Parameter element, ApiContext context) {
public void visitHeaderParam(HeaderParam param, AnnotatedElement element, ApiContext context) {
org.eclipse.microprofile.openapi.models.parameters.Parameter newParameter = new ParameterImpl();
newParameter.setName(param.value());
newParameter.setIn(In.HEADER);
newParameter.setStyle(Style.SIMPLE);
newParameter.setSchema(new SchemaImpl().type(ModelUtils.getSchemaType(element.getType())));
if (element instanceof java.lang.reflect.Parameter) {
newParameter.setSchema(new SchemaImpl().type(ModelUtils
.getSchemaType(java.lang.reflect.Parameter.class.cast(element).getType())));
} else {
newParameter.setSchema(new SchemaImpl().type(ModelUtils
.getSchemaType(Field.class.cast(element).getType())));
}
context.getWorkingOperation().addParameter(newParameter);
}

@Override
public void visitCookieParam(CookieParam param, java.lang.reflect.Parameter element, ApiContext context) {
public void visitCookieParam(CookieParam param, AnnotatedElement element, ApiContext context) {
org.eclipse.microprofile.openapi.models.parameters.Parameter newParameter = new ParameterImpl();
newParameter.setName(param.value());
newParameter.setIn(In.COOKIE);
newParameter.setStyle(Style.SIMPLE);
newParameter.setSchema(new SchemaImpl().type(ModelUtils.getSchemaType(element.getType())));
if (element instanceof java.lang.reflect.Parameter) {
newParameter.setSchema(new SchemaImpl().type(ModelUtils
.getSchemaType(java.lang.reflect.Parameter.class.cast(element).getType())));
} else {
newParameter.setSchema(new SchemaImpl().type(ModelUtils
.getSchemaType(Field.class.cast(element).getType())));
}
context.getWorkingOperation().addParameter(newParameter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
import fish.payara.microprofile.openapi.api.visitor.ApiVisitor;
import fish.payara.microprofile.openapi.api.visitor.ApiVisitor.VisitorFunction;
import fish.payara.microprofile.openapi.api.visitor.ApiWalker;
import java.util.ArrayList;
import java.util.List;

/**
* A walker that visits each annotation and passes it to the visitor.
Expand Down Expand Up @@ -182,17 +184,33 @@ private <A extends Annotation, A2 extends Annotation, E extends AnnotatedElement

processAnnotation(clazz, annotationClass, annotationFunction, altClass, altFunction,
new OpenApiContext(api, getResourcePath(clazz)));

List<Field> fields = new ArrayList<>();
for (Field field : clazz.getDeclaredFields()) {
processAnnotation(field, annotationClass, annotationFunction, altClass, altFunction,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we discussed removing this? This class would be best kept generic, moving the implementation specific logic into the handleParam methods.

new OpenApiContext(api, null));
for (Annotation annotation : field.getDeclaredAnnotations()) {
if (annotation instanceof QueryParam
|| annotation instanceof PathParam
|| annotation instanceof HeaderParam
|| annotation instanceof CookieParam
|| annotation instanceof FormParam) {
fields.add(field);
} else {
processAnnotation(field, annotationClass, annotationFunction, altClass, altFunction,
new OpenApiContext(api, null));
}
}
}

for (Method method : clazz.getDeclaredMethods()) {

processAnnotation(method, annotationClass, annotationFunction, altClass, altFunction,
new OpenApiContext(api, getResourcePath(method), getOperation(method)));

if (!fields.isEmpty()) {
for (Field field : fields) {
processAnnotation(field, annotationClass, annotationFunction, altClass, altFunction,
new OpenApiContext(api, getResourcePath(method), getOperation(method)));
}
}
for (java.lang.reflect.Parameter parameter : method.getParameters()) {
processAnnotation(parameter, annotationClass, annotationFunction, altClass, altFunction,
new OpenApiContext(api, getResourcePath(method), getOperation(method)));
Expand Down