Skip to content

Commit

Permalink
Documentation updates.
Browse files Browse the repository at this point in the history
Added ReDoc to SwaggerService.
Added default response code to Swagger spec. 
Removed excess logging in Reader.
  • Loading branch information
noboomu committed May 12, 2017
1 parent 5ba7480 commit 4fdb70a
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,7 @@ public static void addStatement(MethodSpec.Builder builder, Parameter parameter)
*/
public static TypeHandler forType(Type type)
{
log.debug("forType " + type);


boolean hasValueOf = false;
boolean hasFromString = false;
boolean isOptional = type.getTypeName().contains("java.util.Optional");
Expand Down Expand Up @@ -471,8 +470,7 @@ else if (type.getTypeName().contains("java.nio.ByteBuffer"))

Class<?> erasedType = (Class<?>) extractErasedType(type);

log.debug("erasedType: " + erasedType + " for " + type);


if (hasValueOfMethod(erasedType))
{
return OptionalValueOfType;
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/io/sinistral/proteus/server/swagger/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ private Operation parseMethod(Class<?> cls, Method method, AnnotatedMethod annot
operationId = this.getOperationId(method.getName());
}

LOGGER.debug("initial operationId: " + operationId + " nickname: " + apiOperation.nickname());
// LOGGER.debug("initial operationId: " + operationId + " nickname: " + apiOperation.nickname());

String responseContainer = null;

Expand All @@ -849,7 +849,7 @@ private Operation parseMethod(Class<?> cls, Method method, AnnotatedMethod annot
operationId = apiOperation.nickname();
}

LOGGER.debug("operationId after nickname: " + operationId);
// LOGGER.debug("operationId after nickname: " + operationId);

defaultResponseHeaders = parseResponseHeaders(apiOperation.responseHeaders());

Expand Down Expand Up @@ -1061,7 +1061,8 @@ else if( responseCls.isAssignableFrom(CompletableFuture.class) )

if (operation.getResponses() == null) {
Response response = new Response().description(SUCCESSFUL_OPERATION);
operation.defaultResponse(response);

operation.response(200, response);
}

processOperationDecorator(operation, method);
Expand All @@ -1073,16 +1074,15 @@ private void processOperationDecorator(Operation operation, Method method) {
final Iterator<SwaggerExtension> chain = SwaggerExtensions.chain();
if (chain.hasNext()) {
SwaggerExtension extension = chain.next();
LOGGER.debug("trying to decorate operation: {}", extension);
// LOGGER.debug("trying to decorate operation: {}", extension);
extension.decorateOperation(operation, method, chain);
}
}

private void addResponse(Operation operation, ApiResponse apiResponse) {
Map<String, Property> responseHeaders = parseResponseHeaders(apiResponse.responseHeaders());

Response response = new Response()
.description(apiResponse.message()).headers(responseHeaders);
Response response = new Response().description(apiResponse.message()).headers(responseHeaders);

if (apiResponse.code() == 0) {
operation.defaultResponse(response);
Expand Down Expand Up @@ -1111,7 +1111,7 @@ private List<Parameter> getParameters(Type type, List<Annotation> annotations, j
}


LOGGER.debug("getParameters for {}", type);
// LOGGER.debug("getParameters for {}", type);
Set<Type> typesToSkip = new HashSet<>();
typesToSkip.add(TypeFactory.defaultInstance().constructType(ServerRequest.class));
typesToSkip.add(TypeFactory.defaultInstance().constructType(HttpServerExchange.class));
Expand Down Expand Up @@ -1150,7 +1150,7 @@ private List<Parameter> getParameters(Type type, List<Annotation> annotations, j
final List<Parameter> processed = new ArrayList<Parameter>(parameters.size());
for (Parameter parameter : parameters) {

LOGGER.debug("getParameters for {}", type);
// LOGGER.debug("getParameters for {}", type);

if (ParameterProcessor.applyAnnotations(swagger, parameter, type, annotations) != null) {

Expand All @@ -1159,11 +1159,11 @@ private List<Parameter> getParameters(Type type, List<Annotation> annotations, j
}
return processed;
} else {
LOGGER.debug("no parameter found, looking at body params");
// LOGGER.debug("no parameter found, looking at body params");
final List<Parameter> body = new ArrayList<Parameter>();
if (!typesToSkip.contains(type)) {

LOGGER.debug("getBody Parameters for {}", type);
// LOGGER.debug("getBody Parameters for {}", type);

Parameter param = ParameterProcessor.applyAnnotations(swagger, null, type, annotations);
if (param != null) {
Expand Down
89 changes: 60 additions & 29 deletions src/main/java/io/sinistral/proteus/services/SwaggerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,7 @@ public class SwaggerService extends BaseService implements Supplier<RoutingHan
protected io.sinistral.proteus.server.swagger.Reader reader = null;

protected final String swaggerResourcePathPrefix = "swagger";


protected Swagger swagger = null;

protected String swaggerSpec = null;

protected String swaggerIndexHTML = null;



@Inject
@Named("swagger.resourcePrefix")
protected String swaggerResourcePrefix;
Expand All @@ -89,6 +81,9 @@ public class SwaggerService extends BaseService implements Supplier<RoutingHan
@Named("swagger.info")
protected Config swaggerInfo;

@Inject
@Named("swagger.redocPath")
protected String redocPath;

@Inject
@Named("application.host")
Expand Down Expand Up @@ -125,6 +120,13 @@ public class SwaggerService extends BaseService implements Supplier<RoutingHan

protected ClassLoader serviceClassLoader = null;

protected Swagger swagger = null;

protected String swaggerSpec = null;

protected String swaggerIndexHTML = null;

protected String redocHTML = null;

public SwaggerService( )
{
Expand Down Expand Up @@ -220,25 +222,38 @@ public void generateSwaggerHTML()
{


final InputStream templateInputStream = this.getClass().getClassLoader().getResourceAsStream(swaggerResourcePrefix + "/index.html");

byte[] templateBytes = IOUtils.toByteArray(templateInputStream);
try(InputStream templateInputStream = this.getClass().getClassLoader().getResourceAsStream(swaggerResourcePrefix + "/index.html"))
{
byte[] templateBytes = IOUtils.toByteArray(templateInputStream);

String templateString = new String(templateBytes,Charset.defaultCharset());

String themePath = "swagger-ui.css";

if(!swaggerTheme.equals("default"))
{
themePath= "themes/theme-" + swaggerTheme + ".css";
}

templateString = templateString.replaceAll("\\{\\{ themePath \\}\\}", themePath);
templateString = templateString.replaceAll("\\{\\{ swaggerBasePath \\}\\}", swaggerBasePath);
templateString = templateString.replaceAll("\\{\\{ title \\}\\}",applicationName + " Swagger UI");
templateString = templateString.replaceAll("\\{\\{ swaggerFullPath \\}\\}","//" + host + ((port != 80 && port != 443) ? ":" + port : "") + swaggerBasePath + ".json");

this.swaggerIndexHTML = templateString;
}

String templateString = new String(templateBytes,Charset.defaultCharset());

String themePath = "swagger-ui.css";

if(!this.swaggerTheme.equals("default"))
try(InputStream templateInputStream = this.getClass().getClassLoader().getResourceAsStream(swaggerResourcePrefix + "/redoc.html"))
{
themePath= "themes/theme-" + this.swaggerTheme + ".css";
}
templateString = templateString.replaceAll("\\{\\{ themePath \\}\\}", themePath);
templateString = templateString.replaceAll("\\{\\{ swaggerBasePath \\}\\}", this.swaggerBasePath);
templateString = templateString.replaceAll("\\{\\{ title \\}\\}",applicationName + " Swagger UI");
templateString = templateString.replaceAll("\\{\\{ swaggerFullPath \\}\\}","//" + host + ((port != 80 && port != 443) ? ":" + port : "") + this.swaggerBasePath + ".json");

this.swaggerIndexHTML = templateString;
byte[] templateBytes = IOUtils.toByteArray(templateInputStream);
String templateString = new String(templateBytes,Charset.defaultCharset());
templateString = templateString.replaceAll("\\{\\{ swaggerSpecPath \\}\\}", this.swaggerBasePath + ".json");
templateString = templateString.replaceAll("\\{\\{ applicationName \\}\\}", applicationName);

this.redocHTML = templateString;
}

URL url = this.getClass().getClassLoader().getResource(swaggerResourcePrefix);

Expand Down Expand Up @@ -332,12 +347,28 @@ public void handleRequest(HttpServerExchange exchange) throws Exception

});


this.registeredEndpoints.add(EndpointInfo.builder().withConsumes("*/*").withPathTemplate(pathTemplate).withControllerName("Swagger").withMethod(Methods.GET).withProduces(MediaType.APPLICATION_JSON).build());

pathTemplate = this.swaggerBasePath + "/" + this.redocPath;

router.add(HttpMethod.GET,pathTemplate, new HttpHandler(){

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception
{

exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, MediaType.TEXT_HTML);
exchange.getResponseSender().send(redocHTML);

}

});


this.registeredEndpoints.add(EndpointInfo.builder().withConsumes("*/*").withPathTemplate(pathTemplate).withControllerName("Swagger").withMethod(Methods.GET).withProduces(MediaType.TEXT_HTML).build());

pathTemplate = this.swaggerBasePath;



router.add(HttpMethod.GET, pathTemplate , new HttpHandler(){

@Override
Expand Down
23 changes: 23 additions & 0 deletions src/main/resources/io/sinistral/proteus/swagger/redoc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<title>{{ applicationName }} ReDoc</title>
<!-- needed for adaptive design -->
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">

<!--
ReDoc doesn't change outer page styles
-->
<style>
body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<redoc spec-url='{{ swaggerSpecPath }}'></redoc>
<script src="https://rebilly.github.io/ReDoc/releases/latest/redoc.min.js"> </script>
</body>
</html>
26 changes: 9 additions & 17 deletions src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,24 @@ application {
version = "1.0"

name="proteus"
# tmpdir
tmpdir = ${java.io.tmpdir}/${application.name}

# path (a.k.a. as contextPath)
path = "/v1"

# localhost
host = "localhost"

# HTTP ports
port = 8090

#securePort = 8443

# uncomment to enabled HTTPS
# securePort = 8443

# we do UTF-8
charset = UTF-8

# date format
dateFormat = dd-MMM-yyyy


fallbackHandler = "io.sinistral.proteus.server.handlers.ServerFallbackHandler"

defaultResponseListener = "io.sinistral.proteus.server.handlers.ServerDefaultResponseListener"

redirect_https = ""

tmpdir = ${java.io.tmpdir}/${application.name}
}

api.version="v1"
Expand Down Expand Up @@ -63,7 +54,7 @@ swagger {
# the path that has an index.html template and theme css files
resourcePrefix="io/sinistral/proteus/swagger"
# swagger version
swagger: "2.0"
swagger="2.0"
info {
# swagger info title
title = ${application.name}
Expand All @@ -75,12 +66,13 @@ swagger {
theme="default"
# where the swagger endpoints will be mounted
basePath= ${application.path}"/swagger"
# where redoc will be mounted relative to swagger base path
redocPath= "redoc"
#the name of the spec file
specFilename="swagger.json"
consumes = ["application/json"]
produces = ["application/json"]
# supported schemes
schemes = ["http"]

}

undertow
Expand Down

0 comments on commit 4fdb70a

Please sign in to comment.