Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java][native] Remove JAX-RS dependency from generated java-native client. #9585

Merged
merged 1 commit into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {{invokerPackage}}.ApiException;
import java.util.Objects;
import java.lang.reflect.Type;
import java.util.Map;
import javax.ws.rs.core.GenericType;

import com.fasterxml.jackson.annotation.JsonValue;

Expand Down Expand Up @@ -35,7 +34,7 @@ public abstract class AbstractOpenApiSchema {
*
* @return an instance of the actual schema/object
*/
public abstract Map<String, GenericType> getSchemas();
public abstract Map<String, Class<?>> getSchemas();

/**
* Get the actual instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.ext.ContextResolver;

{{>generatedAnnotation}}
public class JSON implements ContextResolver<ObjectMapper> {
public class JSON {
private ObjectMapper mapper;

public JSON() {
Expand Down Expand Up @@ -70,11 +68,6 @@ public class JSON implements ContextResolver<ObjectMapper> {
mapper.setDateFormat(dateFormat);
}

@Override
public ObjectMapper getContext(Class<?> type) {
return mapper;
}

/**
* Get the object mapper
*
Expand Down Expand Up @@ -207,10 +200,10 @@ public class JSON implements ContextResolver<ObjectMapper> {
visitedClasses.add(modelClass);

// Traverse the oneOf/anyOf composed schemas.
Map<String, GenericType> descendants = modelDescendants.get(modelClass);
Map<String, Class<?>> descendants = modelDescendants.get(modelClass);
if (descendants != null) {
for (GenericType childType : descendants.values()) {
if (isInstanceOf(childType.getRawType(), inst, visitedClasses)) {
for (Class<?> childType : descendants.values()) {
if (isInstanceOf(childType, inst, visitedClasses)) {
return true;
}
}
Expand All @@ -221,12 +214,12 @@ public class JSON implements ContextResolver<ObjectMapper> {
/**
* A map of discriminators for all model classes.
*/
private static Map<Class<?>, ClassDiscriminatorMapping> modelDiscriminators = new HashMap<Class<?>, ClassDiscriminatorMapping>();
private static Map<Class<?>, ClassDiscriminatorMapping> modelDiscriminators = new HashMap<>();

/**
* A map of oneOf/anyOf descendants for each model class.
*/
private static Map<Class<?>, Map<String, GenericType>> modelDescendants = new HashMap<Class<?>, Map<String, GenericType>>();
private static Map<Class<?>, Map<String, Class<?>>> modelDescendants = new HashMap<>();

/**
* Register a model class discriminator.
Expand All @@ -246,7 +239,7 @@ public class JSON implements ContextResolver<ObjectMapper> {
* @param modelClass the model class
* @param descendants a map of oneOf/anyOf descendants.
*/
public static void registerDescendants(Class<?> modelClass, Map<String, GenericType> descendants) {
public static void registerDescendants(Class<?> modelClass, Map<String, Class<?>> descendants) {
modelDescendants.put(modelClass, descendants);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -10,7 +8,6 @@ import java.util.HashSet;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
Expand Down Expand Up @@ -99,7 +96,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
}

// store a list of schema names defined in anyOf
public static final Map<String, GenericType> schemas = new HashMap<String, GenericType>();
public static final Map<String, Class<?>> schemas = new HashMap<String, Class<?>>();

public {{classname}}() {
super("anyOf", {{#isNullable}}Boolean.TRUE{{/isNullable}}{{^isNullable}}Boolean.FALSE{{/isNullable}});
Expand Down Expand Up @@ -128,8 +125,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
{{/anyOf}}
static {
{{#anyOf}}
schemas.put("{{{.}}}", new GenericType<{{{.}}}>() {
});
schemas.put("{{{.}}}", {{{.}}}.class);
{{/anyOf}}
JSON.registerDescendants({{classname}}.class, Collections.unmodifiableMap(schemas));
{{#discriminator}}
Expand All @@ -144,7 +140,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
}

@Override
public Map<String, GenericType> getSchemas() {
public Map<String, Class<?>> getSchemas() {
return {{classname}}.schemas;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ apply plugin: 'maven'
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

// Some text from the schema is copy pasted into the source files as UTF-8
// but the default still seems to be to use platform encoding
tasks.withType(JavaCompile) {
configure(options) {
options.encoding = 'UTF-8'
}
}
javadoc {
options.encoding = 'UTF-8'
}

install {
repositories.mavenInstaller {
pom.artifactId = '{{artifactId}}'
Expand Down Expand Up @@ -53,7 +64,6 @@ ext {
swagger_annotations_version = "1.5.22"
jackson_version = "2.10.4"
junit_version = "4.13.1"
ws_rs_version = "2.1.1"
{{#threetenbp}}
threetenbp_version = "2.9.10"
{{/threetenbp}}
Expand All @@ -68,7 +78,6 @@ dependencies {
implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson_version"
implementation "org.openapitools:jackson-databind-nullable:0.2.1"
implementation 'javax.annotation:javax.annotation-api:1.3.2'
implementation "javax.ws.rs:javax.ws.rs-api:$ws_rs_version"
{{#threetenbp}}
implementation "com.github.joschi.jackson:jackson-datatype-threetenbp:$threetenbp_version"
{{/threetenbp}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -11,7 +9,6 @@ import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
Expand Down Expand Up @@ -132,7 +129,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
}

// store a list of schema names defined in oneOf
public static final Map<String, GenericType> schemas = new HashMap<String, GenericType>();
public static final Map<String, Class<?>> schemas = new HashMap<>();

public {{classname}}() {
super("oneOf", {{#isNullable}}Boolean.TRUE{{/isNullable}}{{^isNullable}}Boolean.FALSE{{/isNullable}});
Expand Down Expand Up @@ -161,8 +158,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
{{/oneOf}}
static {
{{#oneOf}}
schemas.put("{{{.}}}", new GenericType<{{{.}}}>() {
});
schemas.put("{{{.}}}", {{{.}}}.class);
{{/oneOf}}
JSON.registerDescendants({{classname}}.class, Collections.unmodifiableMap(schemas));
{{#discriminator}}
Expand All @@ -177,7 +173,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
}

@Override
public Map<String, GenericType> getSchemas() {
public Map<String, Class<?>> getSchemas() {
return {{classname}}.schemas;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,6 @@
<scope>provided</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>${javax-ws-rs-api-version}</version>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>junit</groupId>
Expand All @@ -238,7 +231,6 @@
<jackson-version>2.10.4</jackson-version>
<jackson-databind-nullable-version>0.2.1</jackson-databind-nullable-version>
<javax-annotation-version>1.3.2</javax-annotation-version>
<javax-ws-rs-api-version>2.1.1</javax-ws-rs-api-version>
{{#threetenbp}}
<threetenbp-version>2.9.10</threetenbp-version>
{{/threetenbp}}
Expand Down
13 changes: 11 additions & 2 deletions samples/client/petstore/java/native-async/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ apply plugin: 'maven'
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

// Some text from the schema is copy pasted into the source files as UTF-8
// but the default still seems to be to use platform encoding
tasks.withType(JavaCompile) {
configure(options) {
options.encoding = 'UTF-8'
}
}
javadoc {
options.encoding = 'UTF-8'
}

install {
repositories.mavenInstaller {
pom.artifactId = 'petstore-native'
Expand Down Expand Up @@ -53,7 +64,6 @@ ext {
swagger_annotations_version = "1.5.22"
jackson_version = "2.10.4"
junit_version = "4.13.1"
ws_rs_version = "2.1.1"
}

dependencies {
Expand All @@ -65,6 +75,5 @@ dependencies {
implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson_version"
implementation "org.openapitools:jackson-databind-nullable:0.2.1"
implementation 'javax.annotation:javax.annotation-api:1.3.2'
implementation "javax.ws.rs:javax.ws.rs-api:$ws_rs_version"
testImplementation "junit:junit:$junit_version"
}
8 changes: 0 additions & 8 deletions samples/client/petstore/java/native-async/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,6 @@
<scope>provided</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>${javax-ws-rs-api-version}</version>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>junit</groupId>
Expand All @@ -224,7 +217,6 @@
<jackson-version>2.10.4</jackson-version>
<jackson-databind-nullable-version>0.2.1</jackson-databind-nullable-version>
<javax-annotation-version>1.3.2</javax-annotation-version>
<javax-ws-rs-api-version>2.1.1</javax-ws-rs-api-version>
<junit-version>4.13.1</junit-version>
</properties>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.ext.ContextResolver;

@javax.annotation.processing.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class JSON implements ContextResolver<ObjectMapper> {
public class JSON {
private ObjectMapper mapper;

public JSON() {
Expand All @@ -41,11 +39,6 @@ public void setDateFormat(DateFormat dateFormat) {
mapper.setDateFormat(dateFormat);
}

@Override
public ObjectMapper getContext(Class<?> type) {
return mapper;
}

/**
* Get the object mapper
*
Expand Down Expand Up @@ -178,10 +171,10 @@ public static boolean isInstanceOf(Class<?> modelClass, Object inst, Set<Class<?
visitedClasses.add(modelClass);

// Traverse the oneOf/anyOf composed schemas.
Map<String, GenericType> descendants = modelDescendants.get(modelClass);
Map<String, Class<?>> descendants = modelDescendants.get(modelClass);
if (descendants != null) {
for (GenericType childType : descendants.values()) {
if (isInstanceOf(childType.getRawType(), inst, visitedClasses)) {
for (Class<?> childType : descendants.values()) {
if (isInstanceOf(childType, inst, visitedClasses)) {
return true;
}
}
Expand All @@ -192,12 +185,12 @@ public static boolean isInstanceOf(Class<?> modelClass, Object inst, Set<Class<?
/**
* A map of discriminators for all model classes.
*/
private static Map<Class<?>, ClassDiscriminatorMapping> modelDiscriminators = new HashMap<Class<?>, ClassDiscriminatorMapping>();
private static Map<Class<?>, ClassDiscriminatorMapping> modelDiscriminators = new HashMap<>();

/**
* A map of oneOf/anyOf descendants for each model class.
*/
private static Map<Class<?>, Map<String, GenericType>> modelDescendants = new HashMap<Class<?>, Map<String, GenericType>>();
private static Map<Class<?>, Map<String, Class<?>>> modelDescendants = new HashMap<>();

/**
* Register a model class discriminator.
Expand All @@ -217,7 +210,7 @@ public static void registerDiscriminator(Class<?> modelClass, String discriminat
* @param modelClass the model class
* @param descendants a map of oneOf/anyOf descendants.
*/
public static void registerDescendants(Class<?> modelClass, Map<String, GenericType> descendants) {
public static void registerDescendants(Class<?> modelClass, Map<String, Class<?>> descendants) {
modelDescendants.put(modelClass, descendants);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Objects;
import java.lang.reflect.Type;
import java.util.Map;
import javax.ws.rs.core.GenericType;

import com.fasterxml.jackson.annotation.JsonValue;

Expand Down Expand Up @@ -46,7 +45,7 @@ public AbstractOpenApiSchema(String schemaType, Boolean isNullable) {
*
* @return an instance of the actual schema/object
*/
public abstract Map<String, GenericType> getSchemas();
public abstract Map<String, Class<?>> getSchemas();

/**
* Get the actual instance
Expand Down
13 changes: 11 additions & 2 deletions samples/client/petstore/java/native/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ apply plugin: 'maven'
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

// Some text from the schema is copy pasted into the source files as UTF-8
// but the default still seems to be to use platform encoding
tasks.withType(JavaCompile) {
configure(options) {
options.encoding = 'UTF-8'
}
}
javadoc {
options.encoding = 'UTF-8'
}

install {
repositories.mavenInstaller {
pom.artifactId = 'petstore-native'
Expand Down Expand Up @@ -53,7 +64,6 @@ ext {
swagger_annotations_version = "1.5.22"
jackson_version = "2.10.4"
junit_version = "4.13.1"
ws_rs_version = "2.1.1"
}

dependencies {
Expand All @@ -65,6 +75,5 @@ dependencies {
implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson_version"
implementation "org.openapitools:jackson-databind-nullable:0.2.1"
implementation 'javax.annotation:javax.annotation-api:1.3.2'
implementation "javax.ws.rs:javax.ws.rs-api:$ws_rs_version"
testImplementation "junit:junit:$junit_version"
}
Loading