Skip to content

Commit

Permalink
Added support for customizing OpenAPI type converters and externalize…
Browse files Browse the repository at this point in the history
…d Jackson module.
  • Loading branch information
noboomu committed Apr 23, 2019
1 parent 80ab753 commit 6375bfa
Show file tree
Hide file tree
Showing 22 changed files with 126 additions and 1,173 deletions.
5 changes: 4 additions & 1 deletion core/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ assets {

openapi {


basePath= ${application.path}"/openapi"

redocPath= "redoc"
Expand Down Expand Up @@ -95,6 +94,10 @@ openapi {
description="Default Server"
}
]

converters = [
"io.sinistral.proteus.openapi.jaxrs2.ServerModelResolver"
]
}


Expand Down
6 changes: 1 addition & 5 deletions core/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@

<logger name="io.netty" level="ERROR" />
<logger name="io.netty.handler" level="ERROR" />
<logger name="io.sinistral.proteus.server.tools.openapi" level="ERROR" />
<logger name="io.sinistral.proteus.server.tools.swagger" level="ERROR" />
<logger name="io.sinistral.proteus.server.handlers" level="DEBUG" />
<logger name="io.sinistral.proteus.server.handlers" level="ERROR" />
<logger name="io.sinistral.proteus.services" level="ERROR" />
<logger name="io.sinistral.proteus.openapi" level="ERROR" />
<logger name="io.sinistral.proteus.swagger" level="ERROR" />

<logger name="com.sun.jersey" level="ERROR" />

Expand Down
1 change: 1 addition & 0 deletions openapi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
<groupId>org.zalando</groupId>
<artifactId>jackson-datatype-money</artifactId>
<version>1.1.1</version>
<scope>test</scope>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;
import com.fasterxml.jackson.databind.type.TypeFactory;
import io.sinistral.proteus.openapi.models.MoneyModelConverter;
import io.sinistral.proteus.server.ServerRequest;
import io.sinistral.proteus.server.ServerResponse;
import io.swagger.v3.core.converter.AnnotatedType;
Expand Down Expand Up @@ -1374,19 +1373,11 @@ else if (classType.getRawClass().isAssignableFrom(CompletableFuture.class))
}
}

ModelConverters converters = ModelConverters.getInstance();

converters.addConverter(new ServerModelResolver());
converters.addConverter(new MoneyModelConverter());


ResolvedSchema resolvedSchema = converters
ResolvedSchema resolvedSchema = ModelConverters.getInstance()
.resolveAsResolvedSchema(new AnnotatedType(returnType).resolveAsRef(true).jsonViewAnnotation(jsonViewAnnotation));

if (resolvedSchema.schema != null)
{
System.out.println("resolvedSchema: " + resolvedSchema.schema);

Schema returnTypeSchema = resolvedSchema.schema;
Content content = new Content();
MediaType mediaType = new MediaType().schema(returnTypeSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.type.TypeFactory;
import io.sinistral.proteus.openapi.models.MoneyModelConverter;
import io.sinistral.proteus.server.ServerResponse;
import io.swagger.v3.core.converter.AnnotatedType;
import io.swagger.v3.core.converter.ModelConverter;
import io.swagger.v3.core.converter.ModelConverterContext;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.media.Schema;
import org.javamoney.moneta.Money;
import org.zalando.jackson.datatype.money.MoneyModule;

import java.io.File;
import java.lang.annotation.Annotation;
Expand All @@ -33,28 +30,17 @@ public class ServerModelResolver extends io.swagger.v3.core.jackson.ModelResolve
{
private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ServerModelResolver.class.getCanonicalName());

static final ObjectMapper mapper = Json.mapper();

static
{
mapper.registerModule(new MoneyModule());
}

public ServerModelResolver()
{
super(mapper);


System.out.println("not from super");
super(Json.mapper());
}

/**
* @param mapper
*/
public ServerModelResolver(ObjectMapper mapper)
{
super(ServerModelResolver.mapper);

super(mapper);
}

/*
Expand All @@ -72,7 +58,6 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context

if ((rawClass != null) &&!resolvedType.isPrimitive())
{
// log.debug("resolvedType in " + resolvedType);
if (rawClass.isAssignableFrom(ServerResponse.class))
{
resolvedType = classType.containedType(0);
Expand All @@ -83,25 +68,18 @@ else if (rawClass.isAssignableFrom(CompletableFuture.class))

if (futureCls.isAssignableFrom(ServerResponse.class))
{
// log.debug("class is assignable from ServerResponse");
final JavaType futureType = TypeFactory.defaultInstance().constructType(classType.containedType(0));

resolvedType = futureType.containedType(0);
}
else
{
// log.debug("class is NOT assignable from ServerResponse");
resolvedType = classType.containedType(0);
}
}

if (resolvedType != null)
{
// if (resolvedType.getTypeName().contains("org.javamoney.moneta.Money"))
// {
// resolvedType = TypeFactory.defaultInstance().constructFromCanonical(Money.class.getName());
// }

if (resolvedType.getTypeName().contains("java.lang.Void"))
{
resolvedType = TypeFactory.defaultInstance().constructFromCanonical(Void.class.getName());
Expand Down Expand Up @@ -133,30 +111,12 @@ else if (resolvedType.getTypeName().contains("Optional"))

annotatedType.setType(resolvedType);

// log.debug("resolvedType out " + resolvedType);
}
}

try {


// log.info("Processing " + annotatedType + " " + classType + " " + annotatedType.getName());

if(annotatedType.getType().getTypeName().contains("org.javamoney.moneta.Money"))
{
MoneyModelConverter.MoneySchema schema = new MoneyModelConverter.MoneySchema();

context.defineModel("money",schema);

return schema;
}
else {

Schema schema = super.resolve(annotatedType, context, next);

return schema;
}

return super.resolve(annotatedType, context, next);

} catch (Exception e) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
*/
package io.sinistral.proteus.openapi.jaxrs2;

import org.zalando.jackson.datatype.money.MoneyModule;

import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JavaType;
Expand All @@ -24,10 +22,19 @@
import io.swagger.v3.oas.models.parameters.Parameter;
import org.apache.commons.lang3.StringUtils;

import javax.ws.rs.*;
import javax.ws.rs.BeanParam;
import javax.ws.rs.CookieParam;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
* @author jbauer
Expand All @@ -43,11 +50,6 @@ public class ServerParameterExtension extends AbstractOpenAPIExtension

final static ObjectMapper mapper = Json.mapper();

static
{
mapper.registerModule(new MoneyModule());
}

@Override
public ResolvedParameter extractParameters(List<Annotation> annotations, Type type, Set<Type> typesToSkip, Components components, javax.ws.rs.Consumes classConsumes,
javax.ws.rs.Consumes methodConsumes, boolean includeRequestBody, JsonView jsonViewAnnotation, Iterator<OpenAPIExtension> chain)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 +1,17 @@
package io.sinistral.proteus.openapi.services;

import java.io.File;
import java.io.InputStream;

import java.net.URL;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import java.util.Arrays;
import java.util.Collections;
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;
import java.util.stream.Collectors;

import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.MediaType;

import io.sinistral.proteus.openapi.jaxrs2.Reader;
import io.sinistral.proteus.openapi.jaxrs2.ServerModelResolver;
import io.sinistral.proteus.openapi.jaxrs2.ServerParameterExtension;
import io.sinistral.proteus.openapi.models.MoneyModelConverter;
import io.sinistral.proteus.services.DefaultService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;

import com.typesafe.config.Config;

import io.sinistral.proteus.openapi.jaxrs2.Reader;
import io.sinistral.proteus.openapi.jaxrs2.ServerParameterExtension;
import io.sinistral.proteus.server.endpoints.EndpointInfo;

import io.sinistral.proteus.services.DefaultService;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.jaxrs2.ext.OpenAPIExtensions;
Expand All @@ -62,7 +24,6 @@
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;

import io.undertow.server.HandlerWrapper;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.RoutingHandler;
Expand All @@ -72,6 +33,31 @@
import io.undertow.util.CanonicalPathUtils;
import io.undertow.util.Headers;
import io.undertow.util.Methods;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.MediaType;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
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;
import java.util.stream.Collectors;

/**
* A service for generating and serving an OpenAPI v3 spec and ui.
Expand Down Expand Up @@ -268,7 +254,7 @@ protected void generateSpec() throws Exception

SwaggerConfiguration config = new SwaggerConfiguration().resourceClasses(classes.stream().map(Class::getName).collect(Collectors.toSet())).openAPI(openApi);

config.setModelConverterClassess(new HashSet<>(Arrays.asList(ServerModelResolver.class.getName())));
config.setModelConverterClassess(new HashSet<>( openAPIConfig.getStringList("converters") ));

OpenApiContext ctx = new GenericOpenApiContext().openApiConfiguration(config)
.openApiReader(new Reader(config))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,25 +148,17 @@ public ServerResponse<Set<Long>> genericSet( ServerRequest request, @QueryParam
@Operation(description = "Money type endpoint" )
public ServerResponse<Money> getMoney(ServerRequest request ) throws Exception
{
return response( Money.of(100.0,"USD") ).applicationJson();
return response( Money.of(123.23,"USD") ).applicationJson();
}

@GET
@Path("types/pojo2")
@Produces((MediaType.APPLICATION_JSON))
@Operation(description = "Money type endpoint" )
public ServerResponse<Pojo> getPojo2(ServerRequest request ) throws Exception
{
return response( new Pojo(100L,"USD") ).applicationJson();
}

@GET
@Path("types/pojo")
@Produces((MediaType.APPLICATION_JSON))
@Operation(description = "Money type endpoint" )
@Operation(description = "Pojo type endpoint" )
public ServerResponse<Pojo> getPojo(ServerRequest request ) throws Exception
{
return response( new Pojo(100L,"USD") ).applicationJson();
return response( new Pojo(100L,"John Doe") ).applicationJson();
}

@POST
Expand Down
Loading

0 comments on commit 6375bfa

Please sign in to comment.