Skip to content

Commit

Permalink
Cleanup OpenAPI module and add json spec support.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Oct 29, 2021
1 parent 09f630e commit 4d8d408
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 768 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Proteus Changelog.
## Unreleased
### No issue

**Improved error logging and shutdown process.**


[09f630e8f400db9](https://github.com/noboomu/proteus/commit/09f630e8f400db9) Joshua Bauer *2021-10-25 19:29:23*

**Improve Optional bean parsing.**


Expand Down
2 changes: 1 addition & 1 deletion proteus-core/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ openapi {

port = ${application.ports.http}

specFilename="openapi.yaml"
specFilename="openapi"

openapi="3.0.1"

Expand Down
6 changes: 0 additions & 6 deletions proteus-openapi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,6 @@
<version>${openapi.version}</version>
</dependency>

<dependency>
<groupId>org.zalando</groupId>
<artifactId>jackson-datatype-money</artifactId>
<version>1.1.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.sinistral</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import java.util.jar.JarFile;
Expand All @@ -68,16 +73,20 @@ public class OpenAPIService extends DefaultService implements Supplier<RoutingHa

private static Logger log = LoggerFactory.getLogger(OpenAPIService.class.getCanonicalName());

protected final String resourcePathPrefix = "openapi";
protected ObjectMapper jsonMapper;

protected ObjectMapper mapper;
protected ObjectWriter writer;
protected ObjectMapper yamlMapper;
protected Path resourcePath = null;

protected ClassLoader serviceClassLoader = null;

protected OpenAPI openApi = null;
protected String spec = null;

protected String yamlSpec = null;

protected String jsonSpec = null;

protected String indexHTML = null;

protected String redocHTML = null;

protected final String resourcePrefix = "io/sinistral/proteus/openapi";
Expand Down Expand Up @@ -131,13 +140,9 @@ public class OpenAPIService extends DefaultService implements Supplier<RoutingHa

public OpenAPIService()
{
jsonMapper = Json.mapper();

mapper = Json.mapper();

mapper.registerModule(new Jdk8Module());

yamlMapper = Yaml.mapper();
writer = Yaml.pretty();
jsonMapper.registerModule(new Jdk8Module());
}

protected void generateHTML()
Expand Down Expand Up @@ -225,7 +230,7 @@ protected void generateHTML()
FileUtils.deleteDirectory(tmpDirParent.toFile());
} catch (IOException ex)
{
log.error("Failed to delete temp openapi directory",ex);
log.error("Failed to delete temp openapi directory", ex);
}
}));
}
Expand All @@ -252,11 +257,12 @@ protected void generateSpec() throws Exception
OpenAPIExtensions.setExtensions(Collections.singletonList(new ServerParameterExtension()));

OpenAPI openApi = new OpenAPI();
Info info = mapper.convertValue(openAPIConfig.getValue("info").unwrapped(), Info.class);

Info info = jsonMapper.convertValue(openAPIConfig.getValue("info").unwrapped(), Info.class);

openApi.setInfo(info);

Map<String, SecurityScheme> securitySchemes = mapper.convertValue(openAPIConfig.getValue("securitySchemes").unwrapped(), new TypeReference<Map<String, SecurityScheme>>() {});
Map<String, SecurityScheme> securitySchemes = jsonMapper.convertValue(openAPIConfig.getValue("securitySchemes").unwrapped(), new TypeReference<Map<String, SecurityScheme>>() {});

if (openApi.getComponents() == null)
{
Expand All @@ -265,7 +271,7 @@ protected void generateSpec() throws Exception

openApi.getComponents().setSecuritySchemes(securitySchemes);

List<Server> servers = mapper.convertValue(openAPIConfig.getValue("servers").unwrapped(), new TypeReference<List<Server>>() {});
List<Server> servers = jsonMapper.convertValue(openAPIConfig.getValue("servers").unwrapped(), new TypeReference<List<Server>>() {});

openApi.setServers(servers);

Expand Down Expand Up @@ -298,8 +304,12 @@ protected void generateSpec() throws Exception
.init();

openApi = ctx.read();

this.openApi = openApi;
this.spec = writer.writeValueAsString(openApi);

this.yamlSpec = Yaml.pretty().writeValueAsString(openApi);

this.jsonSpec = Json.pretty().writeValueAsString(openApi);

}

Expand All @@ -317,60 +327,79 @@ protected void startUp() throws Exception
{
generateSpec();

log.debug("\nOpenAPI Spec:\n" + writer.writeValueAsString(this.openApi));
log.debug("\nOpenAPI Spec:\n{}", Yaml.pretty().writeValueAsString(this.openApi));

} catch (Exception e)
{
log.error("Error generating OpenAPI spec", e);
}

}, this.executor());
});

router.addAll(this.get());
}

public RoutingHandler get()
{

FileResourceManager resourceManager = new FileResourceManager(this.resourcePath.toFile(), 1024);

RoutingHandler router = new RoutingHandler();

router.add(HttpMethod.GET, basePath, (HttpServerExchange exchange) ->
{
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, MediaType.TEXT_HTML);
exchange.getResponseSender().send(indexHTML);
});

/*
* YAML path
*/
String pathTemplate = this.applicationPath + "/" + this.specFilename;

FileResourceManager resourceManager = new FileResourceManager(this.resourcePath.toFile(), 1024);
String yamlTemplatePath = String.format("%s/%s.yaml", this.applicationPath, this.specFilename);

router.add(HttpMethod.GET, pathTemplate, (HttpServerExchange exchange) ->
router.add(HttpMethod.GET, yamlTemplatePath, (HttpServerExchange exchange) ->
{
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, io.sinistral.proteus.protocol.MediaType.TEXT_YAML.contentType());

exchange.getResponseSender().send(spec);
exchange.getResponseSender().send(yamlSpec);
});

this.registeredEndpoints.add(EndpointInfo.builder()
.withConsumes("*/*")
.withPathTemplate(pathTemplate)
.withPathTemplate(yamlTemplatePath)
.withControllerName(this.getClass().getSimpleName())
.withMethod(Methods.GET)
.withProduces(io.sinistral.proteus.protocol.MediaType.TEXT_YAML.contentType())
.build());

router.add(HttpMethod.GET, basePath, (HttpServerExchange exchange) ->
this.registeredEndpoints.add(EndpointInfo.builder()
.withConsumes(MediaType.WILDCARD)
.withProduces(MediaType.TEXT_HTML)
.withPathTemplate(yamlTemplatePath)
.withControllerName(this.getClass().getSimpleName())
.withMethod(Methods.GET)
.build());

String jsonTemplatePath = String.format("%s/%s.json", this.applicationPath, this.specFilename);

router.add(HttpMethod.GET, jsonTemplatePath, (HttpServerExchange exchange) ->
{
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, MediaType.TEXT_HTML);
exchange.getResponseSender().send(indexHTML);
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, io.sinistral.proteus.protocol.MediaType.JSON.contentType());

exchange.getResponseSender().send(jsonSpec);
});

this.registeredEndpoints.add(EndpointInfo.builder()
.withConsumes(MediaType.WILDCARD)
.withProduces(MediaType.TEXT_HTML)
.withPathTemplate(pathTemplate)
.withConsumes("*/*")
.withPathTemplate(jsonTemplatePath)
.withControllerName(this.getClass().getSimpleName())
.withMethod(Methods.GET)
.withProduces(io.sinistral.proteus.protocol.MediaType.JSON.contentType())
.build());

final String specPath = pathTemplate;

final String specPath = yamlTemplatePath;

router.add(HttpMethod.GET, this.basePath + "/" + this.redocPath, (HttpServerExchange exchange) ->
{
Expand All @@ -394,10 +423,10 @@ public RoutingHandler get()
try
{

pathTemplate = this.basePath + "/*";
String wildcardPathTemplate = this.basePath + "/*";

router.add(HttpMethod.GET,
pathTemplate,
wildcardPathTemplate,
new ResourceHandler(resourceManager) {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception
Expand Down Expand Up @@ -441,7 +470,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception
this.registeredEndpoints.add(EndpointInfo.builder()
.withConsumes(MediaType.WILDCARD)
.withProduces(MediaType.WILDCARD)
.withPathTemplate(pathTemplate)
.withPathTemplate(wildcardPathTemplate)
.withControllerName(this.getClass().getSimpleName())
.withMethod(Methods.GET)
.build());
Expand Down
Loading

0 comments on commit 4d8d408

Please sign in to comment.