Permalink
Browse files

Add an option to use reflection in equals, hashCode (Java client) (#1767

)

* add option to use reflection in equals, hashcode (java)

* add model test template to java client only

* update pestore samples

* update java samples

* update doc

* update usage, remove unused file
  • Loading branch information...
wing328 committed Dec 31, 2018
1 parent e8ac630 commit 2c051f265c522d73c60b5310f3858d7e1fb60b82
Showing 1,338 changed files with 46,506 additions and 680 deletions.
@@ -0,0 +1,70 @@
/*
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* OpenAPI spec version: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/


package org.openapitools.client.model;

import org.apache.commons.lang3.builder.EqualsBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

/**
* Model tests for ArrayOfArrayOfNumberOnly
*/
public class ArrayOfArrayOfNumberOnlyTest {
private final ArrayOfArrayOfNumberOnly model = new ArrayOfArrayOfNumberOnly();

/**
* Model tests for ArrayOfArrayOfNumberOnly
*/
@Test
public void test() {
// TODO: test ArrayOfArrayOfNumberOnly
}

/**
* Test the property 'arrayArrayNumber'
*/
@Test
public void arrayArrayNumberTest() {
BigDecimal b1 = new BigDecimal("12.3");
BigDecimal b2 = new BigDecimal("5.6");
List<BigDecimal> arrayArrayNumber = new ArrayList<BigDecimal>();
arrayArrayNumber.add(b1);
arrayArrayNumber.add(b2);
model.getArrayArrayNumber().add(arrayArrayNumber);

// create another instance for comparison
BigDecimal b3 = new BigDecimal("12.3");
BigDecimal b4 = new BigDecimal("5.6");
ArrayOfArrayOfNumberOnly model2 = new ArrayOfArrayOfNumberOnly();
List<BigDecimal> arrayArrayNumber2 = new ArrayList<BigDecimal>();
arrayArrayNumber2.add(b1);
arrayArrayNumber2.add(b2);
model2.getArrayArrayNumber().add(arrayArrayNumber2);

Assert.assertTrue(model2.equals(model));
}

}
@@ -0,0 +1,103 @@
/*
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* OpenAPI spec version: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/


package org.openapitools.client.model;

import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.openapitools.client.model.Category;
import org.openapitools.client.model.Tag;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;


/**
* Model tests for Pet
*/
public class PetTest {
private final Pet model = new Pet();

/**
* Model tests for Pet
*/
@Test
public void testPet() {
// test Pet
model.setId(1029L);
model.setName("Dog");

Pet model2 = new Pet();
model2.setId(1029L);
model2.setName("Dog");

Assert.assertTrue(model.equals(model2));
}

/**
* Test the property 'id'
*/
@Test
public void idTest() {
// TODO: test id
}

/**
* Test the property 'category'
*/
@Test
public void categoryTest() {
// TODO: test category
}

/**
* Test the property 'name'
*/
@Test
public void nameTest() {
// TODO: test name
}

/**
* Test the property 'photoUrls'
*/
@Test
public void photoUrlsTest() {
// TODO: test photoUrls
}

/**
* Test the property 'tags'
*/
@Test
public void tagsTest() {
// TODO: test tags
}

/**
* Test the property 'status'
*/
@Test
public void statusTest() {
// TODO: test status
}

}
@@ -44,4 +44,6 @@ cp CI/samples.ci/client/petstore/java/test-manual/common/ConfigurationTest.java
cp CI/samples.ci/client/petstore/java/test-manual/okhttp-gson/auth/ApiKeyAuthTest.java samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/auth/ApiKeyAuthTest.java
cp CI/samples.ci/client/petstore/java/test-manual/okhttp-gson/auth/HttpBasicAuthTest.java samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/auth/HttpBasicAuthTest.java
cp CI/samples.ci/client/petstore/java/test-manual/okhttp-gson/model/EnumValueTest.java samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/model/EnumValueTest.java
cp CI/samples.ci/client/petstore/java/test-manual/okhttp-gson/model/PetTest.java samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/model/PetTest.java
cp CI/samples.ci/client/petstore/java/test-manual/okhttp-gson/model/ArrayOfArrayOfNumberOnly.java samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/model/ArrayOfArrayOfNumberOnlyTest.java
cp CI/samples.ci/client/petstore/java/test-manual/okhttp-gson/JSONTest.java samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/JSONTest.java
@@ -146,6 +146,9 @@ CONFIG OPTIONS for java
feignVersion
Version of OpenFeign: '10.x', '9.x' (default) (Default: false)

useReflectionEqualsHashCode
Use org.apache.commons.lang3.builder for equals and hashCode in the models. WARNING: This will fail under a security manager, unless the appropriate permissions are set up correctly and also there's potential performance impact. (Default: false)

library
library template (sub-template) to use (Default: okhttp-gson)
jersey1 - HTTP client: Jersey client 1.19.4. JSON processing: Jackson 2.8.9. Enable Java6 support using '-DsupportJava6=true'. Enable gzip request encoding using '-DuseGzipFeature=true'.
@@ -534,17 +534,22 @@ public String escapeReservedWord(String name) {

@Override
public String apiFileFolder() {
return outputFolder + "/" + sourceFolder + "/" + apiPackage().replace('.', '/');
return (outputFolder + "/" + sourceFolder + "/" + apiPackage()).replace('.', File.separatorChar);
}

@Override
public String apiTestFileFolder() {
return outputFolder + "/" + testFolder + "/" + apiPackage().replace('.', '/');
return (outputFolder + "/" + testFolder + "/" + apiPackage()).replace('.', File.separatorChar);
}

@Override
public String modelTestFileFolder() {
return (outputFolder + "/" + testFolder + "/" + modelPackage()).replace('.', File.separatorChar);
}

@Override
public String modelFileFolder() {
return outputFolder + "/" + sourceFolder + "/" + modelPackage().replace('.', '/');
return (outputFolder + "/" + sourceFolder + "/" + modelPackage()).replace('.', File.separatorChar);
}

@Override
@@ -572,6 +577,11 @@ public String toApiTestFilename(String name) {
return toApiName(name) + "Test";
}

@Override
public String toModelTestFilename(String name) {
return toModelName(name) + "Test";
}

@Override
public String toApiName(String name) {
if (name.length() == 0) {
@@ -64,6 +64,7 @@
public static final String FEIGN_VERSION = "feignVersion";
public static final String PARCELABLE_MODEL = "parcelableModel";
public static final String USE_RUNTIME_EXCEPTION = "useRuntimeException";
public static final String USE_REFLECTION_EQUALS_HASHCODE = "useReflectionEqualsHashCode";

public static final String PLAY_24 = "play24";
public static final String PLAY_25 = "play25";
@@ -88,7 +89,9 @@
protected String gradleWrapperPackage = "gradle.wrapper";
protected boolean useRxJava = false;
protected boolean useRxJava2 = false;
protected boolean doNotUseRx = true; // backwards compatibility for swagger configs that specify neither rx1 nor rx2 (mustache does not allow for boolean operators so we need this extra field)
// backwards compatibility for openapi configs that specify neither rx1 nor rx2
// (mustache does not allow for boolean operators so we need this extra field)
protected boolean doNotUseRx = true;
protected boolean usePlayWS = false;
protected String playVersion = PLAY_25;
protected String feignVersion = FEIGN_9;
@@ -97,7 +100,7 @@
protected boolean performBeanValidation = false;
protected boolean useGzipFeature = false;
protected boolean useRuntimeException = false;

protected boolean useReflectionEqualsHashCode = false;

public JavaClientCodegen() {
super();
@@ -108,6 +111,8 @@ public JavaClientCodegen() {
apiPackage = "org.openapitools.client.api";
modelPackage = "org.openapitools.client.model";

modelTestTemplateFiles.put("model_test.mustache", ".java");

cliOptions.add(CliOption.newBoolean(USE_RX_JAVA, "Whether to use the RxJava adapter with the retrofit2 library."));
cliOptions.add(CliOption.newBoolean(USE_RX_JAVA2, "Whether to use the RxJava2 adapter with the retrofit2 library."));
cliOptions.add(CliOption.newBoolean(PARCELABLE_MODEL, "Whether to generate models for Android that implement Parcelable with the okhttp-gson library."));
@@ -119,6 +124,7 @@ public JavaClientCodegen() {
cliOptions.add(CliOption.newBoolean(USE_GZIP_FEATURE, "Send gzip-encoded requests"));
cliOptions.add(CliOption.newBoolean(USE_RUNTIME_EXCEPTION, "Use RuntimeException instead of Exception"));
cliOptions.add(CliOption.newBoolean(FEIGN_VERSION, "Version of OpenFeign: '10.x', '9.x' (default)"));
cliOptions.add(CliOption.newBoolean(USE_REFLECTION_EQUALS_HASHCODE, "Use org.apache.commons.lang3.builder for equals and hashCode in the models. WARNING: This will fail under a security manager, unless the appropriate permissions are set up correctly and also there's potential performance impact."));

supportedLibraries.put(JERSEY1, "HTTP client: Jersey client 1.19.4. JSON processing: Jackson 2.8.9. Enable Java6 support using '-DsupportJava6=true'. Enable gzip request encoding using '-DuseGzipFeature=true'.");
supportedLibraries.put(FEIGN, "HTTP client: OpenFeign 9.4.0. JSON processing: Jackson 2.8.9. To enable OpenFeign 10.x, set the 'feignVersion' option to '10.x'");
@@ -225,6 +231,10 @@ public void processOpts() {
this.setUseRuntimeException(convertPropertyToBooleanAndWriteBack(USE_RUNTIME_EXCEPTION));
}

if (additionalProperties.containsKey(USE_REFLECTION_EQUALS_HASHCODE)) {
this.setUseReflectionEqualsHashCode(convertPropertyToBooleanAndWriteBack(USE_REFLECTION_EQUALS_HASHCODE));
}

final String invokerFolder = (sourceFolder + '/' + invokerPackage).replace(".", "/");
final String authFolder = (sourceFolder + '/' + invokerPackage + ".auth").replace(".", "/");
final String apiFolder = (sourceFolder + '/' + apiPackage).replace(".", "/");
@@ -632,6 +642,10 @@ public void setUseRuntimeException(boolean useRuntimeException) {
this.useRuntimeException = useRuntimeException;
}

public void setUseReflectionEqualsHashCode(boolean useReflectionEqualsHashCode) {
this.useReflectionEqualsHashCode = useReflectionEqualsHashCode;
}

final private static Pattern JSON_MIME_PATTERN = Pattern.compile("(?i)application\\/json(;.*)?");
final private static Pattern JSON_VENDOR_MIME_PATTERN = Pattern.compile("(?i)application\\/vnd.(.*)+json(;.*)?");

@@ -125,6 +125,7 @@ dependencies {
compile 'com.google.code.gson:gson:2.8.1'
compile 'io.gsonfire:gson-fire:1.8.0'
compile group: 'org.apache.oltu.oauth2', name: 'org.apache.oltu.oauth2.client', version: '1.0.1'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'
{{#joda}}
compile 'joda-time:joda-time:2.9.9'
{{/joda}}
@@ -13,6 +13,7 @@ lazy val root = (project in file(".")).
"com.squareup.okhttp" % "okhttp" % "2.7.5",
"com.squareup.okhttp" % "logging-interceptor" % "2.7.5",
"com.google.code.gson" % "gson" % "2.8.1",
"org.apache.commons" % "commons-lang3" % "3.8.1",
{{#joda}}
"joda-time" % "joda-time" % "2.9.9" % "compile",
{{/joda}}
Oops, something went wrong.

0 comments on commit 2c051f2

Please sign in to comment.