Skip to content

Commit

Permalink
resolve #2489 - resolve @ApplicationPath
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Oct 24, 2017
1 parent e8a6c36 commit c35a123
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import static org.testng.Assert.assertEquals;

public class PojoTests {
public class PojoTest {

private Map<String, io.swagger.oas.models.media.Schema> read(Type type) {
return ModelConverters.getInstance().read(type);
Expand Down Expand Up @@ -485,14 +485,14 @@ public void testModelPropertyStringExampleJson () {
" id:\n" +
" minimum: 2\n" +
" type: string\n" +
" description: dsfsdsdfdsf\n" +
" description: testdesc\n" +
" example: '{\"id\":19877734}'";
SerializationMatchers.assertEqualsToYaml(readAll(modelWithPropertyStringExampleOverrideJson.class), yaml);
}

static class modelWithPropertyStringExampleOverrideJson {

@Schema(type = "string", name = "id", example = "{\"id\": 19877734}", minimum = "2", description = "dsfsdsdfdsf")
@Schema(type = "string", name = "id", example = "{\"id\": 19877734}", minimum = "2", description = "testdesc")
private String exampleJson;

public String getExampleJson() {
Expand Down
38 changes: 36 additions & 2 deletions modules/swagger-jaxrs2/src/main/java/io/swagger/jaxrs2/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.Consumes;
import javax.ws.rs.core.Application;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
Expand All @@ -68,6 +70,7 @@ public class Reader implements OpenApiReader {

protected OpenAPIConfiguration config;

private Application application;
private OpenAPI openAPI;
private Components components;
private Paths paths;
Expand Down Expand Up @@ -113,7 +116,7 @@ public OpenAPI getOpenAPI() {
* Scans a single class for Swagger annotations - does not invoke ReaderListeners
*/
public OpenAPI read(Class<?> cls) {
return read(cls, "");
return read(cls, resolveApplicationPath());
}

/**
Expand Down Expand Up @@ -161,7 +164,7 @@ public int compare(Class<?> class1, Class<?> class2) {
}

for (Class<?> cls : sortedClasses) {
read(cls, "");
read(cls, resolveApplicationPath());
}

for (ReaderListener listener : listeners.values()) {
Expand All @@ -188,6 +191,33 @@ public OpenAPI read(Set<Class<?>> classes, Map<String, Object> resources) {
return read(classes);
}

protected String resolveApplicationPath() {
if (application != null) {
ApplicationPath applicationPath = application.getClass().getAnnotation(ApplicationPath.class);
if (applicationPath != null) {
if (StringUtils.isNotBlank(applicationPath.value())) {
return applicationPath.value();
}
}
// look for inner application, e.g. ResourceConfig
try {
Method m = application.getClass().getMethod("getApplication", null);
Application innerApplication = (Application) m.invoke(application, null);
applicationPath = innerApplication.getClass().getAnnotation(ApplicationPath.class);
if (applicationPath != null) {
if (StringUtils.isNotBlank(applicationPath.value())) {
return applicationPath.value();
}
}
} catch (NoSuchMethodException e) {
// no inner application found
} catch (Exception e) {
// no inner application found
}
}
return "";
}

public OpenAPI read(Class<?> cls, String parentPath) {

List<io.swagger.oas.annotations.security.SecurityScheme> apiSecurityScheme = ReflectionUtils.getRepeatableAnnotations(cls, io.swagger.oas.annotations.security.SecurityScheme.class);
Expand Down Expand Up @@ -785,4 +815,8 @@ protected boolean isOperationHidden(Method method) {
}
return false;
}

public void setApplication (Application application) {
this.application = application;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ protected OpenApiReader buildReader(OpenAPIConfiguration openApiConfiguration) t
} else {
reader = new Reader();
}
if (reader instanceof Reader) {
((Reader)reader).setApplication(app);
}
reader.setConfiguration(openApiConfiguration);
return reader;
}
Expand Down

0 comments on commit c35a123

Please sign in to comment.