Skip to content

Commit

Permalink
Added support for more flexible schemas.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Apr 23, 2019
1 parent 04ce8d3 commit 80ab753
Show file tree
Hide file tree
Showing 28 changed files with 1,447 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public void bindMappers()

}

this.requestStaticInjection(Extractors.class);
this.requestStaticInjection(ServerResponse.class);

}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
import com.google.inject.AbstractModule;
import io.sinistral.proteus.server.Extractors;
import io.sinistral.proteus.server.ServerResponse;

public class JacksonModule extends AbstractModule
{
Expand All @@ -25,8 +23,5 @@ protected void configure()
objectMapper.registerModule(new Jdk8Module());

this.bind(ObjectMapper.class).toInstance(objectMapper);

this.requestStaticInjection(Extractors.class);
this.requestStaticInjection(ServerResponse.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
*/
package io.sinistral.proteus.swagger.test.controllers;
package io.sinistral.proteus.test.controllers;

import static io.sinistral.proteus.server.ServerResponse.response;

Expand Down Expand Up @@ -37,10 +37,9 @@
import com.google.inject.Singleton;

import io.sinistral.proteus.annotations.Blocking;
import io.sinistral.proteus.annotations.Debug;
import io.sinistral.proteus.server.ServerRequest;
import io.sinistral.proteus.server.ServerResponse;
import io.sinistral.proteus.swagger.test.models.User;
import io.sinistral.proteus.test.models.User;

import io.undertow.server.HttpServerExchange;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
*/
package io.sinistral.proteus.swagger.test.models;
package io.sinistral.proteus.test.models;


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
*
*/
package io.sinistral.proteus.swagger.test.server;
package io.sinistral.proteus.test.server;

import java.util.List;

import io.sinistral.proteus.swagger.test.controllers.Tests;
import io.sinistral.proteus.test.controllers.Tests;
import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.RunListener;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
*/
package io.sinistral.proteus.swagger.test.server;
package io.sinistral.proteus.test.server;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
Expand Down Expand Up @@ -36,8 +36,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;

import io.restassured.http.ContentType;
import io.sinistral.proteus.swagger.test.models.User;
import io.sinistral.proteus.swagger.test.models.User.UserType;
import io.sinistral.proteus.test.models.User;
import io.sinistral.proteus.test.models.User.UserType;

/*
* import static io.restassured.RestAssured.*; import static io.restassured.matcher.RestAssuredMatchers.*; import static org.hamcrest.Matchers.*;
Expand Down
9 changes: 8 additions & 1 deletion openapi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<directory>src/test/resources</directory>
</testResource>
<testResource>
<directory>src/test/java</directory>
<directory>src/test/java/sinistral/proteus/openapi</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
Expand Down Expand Up @@ -109,6 +109,13 @@
<artifactId>swagger-integration</artifactId>
<version>${openapi.version}</version>
</dependency>

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

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>proteus-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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 @@ -1373,11 +1374,19 @@ else if (classType.getRawClass().isAssignableFrom(CompletableFuture.class))
}
}

ResolvedSchema resolvedSchema = ModelConverters.getInstance()
ModelConverters converters = ModelConverters.getInstance();

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


ResolvedSchema resolvedSchema = converters
.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,12 +8,15 @@
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 @@ -30,17 +33,28 @@ 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(Json.mapper());
super(mapper);


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

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

}

/*
Expand Down Expand Up @@ -83,6 +97,11 @@ else if (rawClass.isAssignableFrom(CompletableFuture.class))

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 @@ -120,8 +139,24 @@ else if (resolvedType.getTypeName().contains("Optional"))

try {


// log.info("Processing " + annotatedType + " " + classType + " " + annotatedType.getName());
return super.resolve(annotatedType, context, next);

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;
}


} catch (Exception e) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
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 Down Expand Up @@ -38,7 +40,13 @@ public class ServerParameterExtension extends AbstractOpenAPIExtension
private static String COOKIE_PARAM = "cookie";
private static String PATH_PARAM = "path";
private static String FORM_PARAM = "form";
final ObjectMapper mapper = Json.mapper();

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,
Expand Down
Loading

0 comments on commit 80ab753

Please sign in to comment.