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

ref #3074 - include ModelConverter skipped classes in Reader shouldIgnoreClass #3107

Merged
merged 6 commits into from
Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -115,6 +115,10 @@ public ResolvedSchema resolveAsResolvedSchema(AnnotatedType type) {
return resolvedSchema;
}

public boolean isRegisteredAsSkippedClass(String className) {
return skippedClasses.contains(className);
}

private boolean shouldProcess(Type type) {
final Class<?> cls = TypeFactory.defaultInstance().constructType(type).getRawClass();
if (cls.isPrimitive()) {
Expand All @@ -126,12 +130,7 @@ private boolean shouldProcess(Type type) {
return false;
}
}
for (String classToSkip : skippedClasses) {
if (className.equals(classToSkip)) {
return false;
}
}
return true;
return !skippedClasses.contains(className);
}

static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ public OpenAPI read(Class<?> cls,
returnType = annotatedMethod.getType();
}

if (shouldIgnoreClass(returnType.getTypeName()) && !returnType.equals(subResource)) {
if (shouldIgnoreClass(returnType.getTypeName()) && !method.getGenericReturnType().equals(subResource)) {
continue;
}
}
Expand Down Expand Up @@ -1013,7 +1013,7 @@ private Operation parseMethod(
}

final Class<?> subResource = getSubResourceWithJaxRsSubresourceLocatorSpecs(method);
if (!shouldIgnoreClass(returnType.getTypeName()) && !returnType.equals(subResource)) {
if (!shouldIgnoreClass(returnType.getTypeName()) && !method.getGenericReturnType().equals(subResource)) {
ResolvedSchema resolvedSchema = ModelConverters.getInstance().resolveAsResolvedSchema(new AnnotatedType(returnType).resolveAsRef(true).jsonViewAnnotation(jsonViewAnnotation));
if (resolvedSchema.schema != null) {
Schema returnTypeSchema = resolvedSchema.schema;
Expand Down Expand Up @@ -1066,9 +1066,14 @@ private boolean shouldIgnoreClass(String className) {
return true;
}
boolean ignore = false;
ignore = ignore || className.replace("[simple type, class ", "").startsWith("javax.ws.rs.");
ignore = ignore || className.equalsIgnoreCase("void");
ignore = ignore || className.equalsIgnoreCase("[simple type, class void]");
String rawClassName = className;
if (rawClassName.startsWith("[")) { // jackson JavaType
rawClassName = className.replace("[simple type, class ", "");
rawClassName = rawClassName.substring(0, rawClassName.length() -1);
}
ignore = ignore || rawClassName.startsWith("javax.ws.rs.");
ignore = ignore || rawClassName.equalsIgnoreCase("void");
ignore = ignore || ModelConverters.getInstance().isRegisteredAsSkippedClass(rawClassName);
return ignore;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.swagger.v3.core.converter.AnnotatedType;
import io.swagger.v3.core.converter.ModelConverter;
import io.swagger.v3.core.converter.ModelConverterContextImpl;
import io.swagger.v3.core.converter.ModelConverters;
import io.swagger.v3.core.filter.AbstractSpecFilter;
import io.swagger.v3.core.filter.OpenAPISpecFilter;
import io.swagger.v3.core.filter.SpecFilter;
Expand All @@ -29,6 +30,7 @@
import io.swagger.v3.jaxrs2.resources.RefHeaderResource;
import io.swagger.v3.jaxrs2.resources.RefLinksResource;
import io.swagger.v3.jaxrs2.resources.RefParameter3029Resource;
import io.swagger.v3.jaxrs2.resources.RefParameter3074Resource;
import io.swagger.v3.jaxrs2.resources.RefParameterResource;
import io.swagger.v3.jaxrs2.resources.RefRequestBodyResource;
import io.swagger.v3.jaxrs2.resources.RefResponsesResource;
Expand Down Expand Up @@ -2003,4 +2005,17 @@ public void testTicket3082() {
" type: string\n";
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}

@Test(description = "Filter class return type")
public void testTicket3074() {
Reader reader = new Reader(new OpenAPI());
OpenAPI oasResult = reader.read(RefParameter3074Resource.class);
SerializationMatchers.assertEqualsToYaml(oasResult, RefParameter3074Resource.EXPECTED_YAML_WITH_WRAPPER);

ModelConverters.getInstance().addClassToSkip("io.swagger.v3.jaxrs2.resources.RefParameter3074Resource$Wrapper");

reader = new Reader(new OpenAPI());
oasResult = reader.read(RefParameter3074Resource.class);
SerializationMatchers.assertEqualsToYaml(oasResult, RefParameter3074Resource.EXPECTED_YAML_WITHOUT_WRAPPER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;

public class RefParameter3029Resource {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package io.swagger.v3.jaxrs2.resources;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

public class RefParameter3074Resource {

public static final String EXPECTED_YAML_WITHOUT_WRAPPER =
"openapi: 3.0.1\n" +
"paths:\n" +
" /employee:\n" +
" get:\n" +
" summary: Get an employee\n" +
" operationId: getEmployee\n" +
" responses:\n" +
" 200:\n" +
" content:\n" +
" application/json:\n" +
" schema:\n" +
" $ref: '#/components/schemas/Employee'\n" +
" 500:\n" +
" content:\n" +
" application/json:\n" +
" schema:\n" +
" $ref: '#/components/schemas/Error'\n" +
"components:\n" +
" schemas:\n" +
" Employee:\n" +
" type: object\n" +
" Error:\n" +
" type: object";

public static final String EXPECTED_YAML_WITH_WRAPPER = EXPECTED_YAML_WITHOUT_WRAPPER +
"\n Wrapper:\n type: object";

class Wrapper<A> {
final A a;

Wrapper(A a) {
this.a = a;
}
}

class Employee {
int number;
String name;

Employee(int number, String name) {
this.number = number;
this.name = name;
}
}

class Error {
String message;
}

@GET
@Path("/employee")
@Operation(summary = "Get an employee")
@ApiResponses({
@ApiResponse(
responseCode = "200",
content = @Content(schema = @Schema(implementation = Employee.class), mediaType = "application/json")
),
@ApiResponse(
responseCode = "500",
content = @Content(schema = @Schema(implementation = Error.class), mediaType = "application/json")
),
})
public Wrapper getEmployee() {
return new Wrapper<Employee>(new Employee(1, "Michael Suyama"));
}
}