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] Discriminator lookup should not be case insensitive by default #5894

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -59,6 +59,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
public static final String DISABLE_HTML_ESCAPING = "disableHtmlEscaping";
public static final String BOOLEAN_GETTER_PREFIX = "booleanGetterPrefix";
public static final String ADDITIONAL_MODEL_TYPE_ANNOTATIONS = "additionalModelTypeAnnotations";
public static final String DISCRIMINATOR_CASE_SENSITIVE = "discriminatorCaseSensitive";

protected String dateLibrary = "threetenbp";
protected boolean supportAsync = false;
Expand All @@ -84,6 +85,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
protected String sourceFolder = projectFolder + "/java";
protected String testFolder = projectTestFolder + "/java";
protected boolean fullJavaUtil;
protected boolean discriminatorCaseSensitive = true; // True if the discriminator value lookup should be case-sensitive.
protected String javaUtilPrefix = "";
protected Boolean serializableModel = false;
protected boolean serializeBigDecimalAsString = false;
Expand Down Expand Up @@ -189,6 +191,7 @@ public AbstractJavaCodegen() {
cliOptions.add(CliOption.newBoolean(CodegenConstants.SERIALIZABLE_MODEL, CodegenConstants.SERIALIZABLE_MODEL_DESC, this.getSerializableModel()));
cliOptions.add(CliOption.newBoolean(CodegenConstants.SERIALIZE_BIG_DECIMAL_AS_STRING, CodegenConstants.SERIALIZE_BIG_DECIMAL_AS_STRING_DESC, serializeBigDecimalAsString));
cliOptions.add(CliOption.newBoolean(FULL_JAVA_UTIL, "whether to use fully qualified name for classes under java.util. This option only works for Java API client", fullJavaUtil));
cliOptions.add(CliOption.newBoolean(DISCRIMINATOR_CASE_SENSITIVE, "Whether the discriminator value lookup should be case-sensitive or not. This option only works for Java API client", discriminatorCaseSensitive));
cliOptions.add(CliOption.newBoolean(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC, this.isHideGenerationTimestamp()));
cliOptions.add(CliOption.newBoolean(WITH_XML, "whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)"));

Expand Down Expand Up @@ -385,6 +388,15 @@ public void processOpts() {
if (additionalProperties.containsKey(FULL_JAVA_UTIL)) {
this.setFullJavaUtil(Boolean.valueOf(additionalProperties.get(FULL_JAVA_UTIL).toString()));
}
if (additionalProperties.containsKey(DISCRIMINATOR_CASE_SENSITIVE)) {
this.setDiscriminatorCaseSensitive(Boolean.valueOf(additionalProperties.get(DISCRIMINATOR_CASE_SENSITIVE).toString()));
} else {
// By default, the discriminator lookup should be case sensitive. There is nothing in the OpenAPI specification
// that indicates the lookup should be case insensitive. However, some implementations perform
// a case-insensitive lookup.
this.setDiscriminatorCaseSensitive(Boolean.TRUE);
}
additionalProperties.put(DISCRIMINATOR_CASE_SENSITIVE, this.discriminatorCaseSensitive);

if (fullJavaUtil) {
javaUtilPrefix = "java.util.";
Expand Down Expand Up @@ -1431,6 +1443,15 @@ public void setFullJavaUtil(boolean fullJavaUtil) {
this.fullJavaUtil = fullJavaUtil;
}

/**
* Set whether discriminator value lookup is case-sensitive or not.
*
* @param discriminatorCaseSensitive true if the discriminator value lookup should be case sensitive.
*/
public void setDiscriminatorCaseSensitive(boolean discriminatorCaseSensitive) {
this.discriminatorCaseSensitive = discriminatorCaseSensitive;
}

public void setWithXml(boolean withXml) {
this.withXml = withXml;
}
Expand Down
13 changes: 10 additions & 3 deletions modules/openapi-generator/src/main/resources/Java/JSON.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public class JSON {
public Class getClassForElement(JsonElement readElement) {
Map<String, Class> classByDiscriminatorValue = new HashMap<String, Class>();
{{#mappedModels}}
classByDiscriminatorValue.put("{{mappingName}}".toUpperCase(Locale.ROOT), {{modelName}}.class);
classByDiscriminatorValue.put("{{mappingName}}"{{^discriminatorCaseSensitive}}.toUpperCase(Locale.ROOT){{/discriminatorCaseSensitive}}, {{modelName}}.class);
{{/mappedModels}}
classByDiscriminatorValue.put("{{classname}}".toUpperCase(Locale.ROOT), {{classname}}.class);
classByDiscriminatorValue.put("{{classname}}"{{^discriminatorCaseSensitive}}.toUpperCase(Locale.ROOT){{/discriminatorCaseSensitive}}, {{classname}}.class);
return getClassByDiscriminator(classByDiscriminatorValue,
getDiscriminatorValue(readElement, "{{{propertyBaseName}}}"));
}
Expand All @@ -97,8 +97,15 @@ public class JSON {
return element.getAsString();
}

/**
* Returns the Java class that implements the OpenAPI schema for the specified discriminator value.
*
* @param classByDiscriminatorValue The map of discriminator values to Java classes.
* @param discriminatorValue The value of the OpenAPI discriminator in the input data.
* @return The Java class that implements the OpenAPI schema
*/
private static Class getClassByDiscriminator(Map classByDiscriminatorValue, String discriminatorValue) {
Class clazz = (Class) classByDiscriminatorValue.get(discriminatorValue.toUpperCase(Locale.ROOT));
Class clazz = (Class) classByDiscriminatorValue.get(discriminatorValue{{^discriminatorCaseSensitive}}.toUpperCase(Locale.ROOT){{/discriminatorCaseSensitive}});
if (null == clazz) {
throw new IllegalArgumentException("cannot determine model class of name: <" + discriminatorValue + ">");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

public class Animal {
public static final String JSON_PROPERTY_CLASS_NAME = "className";
private String className;
protected String className;
sebastien-rosset marked this conversation as resolved.
Show resolved Hide resolved

public static final String JSON_PROPERTY_COLOR = "color";
private String color = "red";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

public class Animal {
public static final String JSON_PROPERTY_CLASS_NAME = "className";
private String className;
protected String className;

public static final String JSON_PROPERTY_COLOR = "color";
private String color = "red";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

public class Animal {
public static final String JSON_PROPERTY_CLASS_NAME = "className";
private String className;
protected String className;

public static final String JSON_PROPERTY_COLOR = "color";
private String color = "red";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

public class Animal {
public static final String JSON_PROPERTY_CLASS_NAME = "className";
private String className;
protected String className;

public static final String JSON_PROPERTY_COLOR = "color";
private String color = "red";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

public class Animal {
public static final String JSON_PROPERTY_CLASS_NAME = "className";
private String className;
protected String className;

public static final String JSON_PROPERTY_COLOR = "color";
private String color = "red";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

public class Animal {
public static final String JSON_PROPERTY_CLASS_NAME = "className";
private String className;
protected String className;

public static final String JSON_PROPERTY_COLOR = "color";
private String color = "red";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

public class Animal {
public static final String JSON_PROPERTY_CLASS_NAME = "className";
private String className;
protected String className;

public static final String JSON_PROPERTY_COLOR = "color";
private String color = "red";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.openapitools.client.model.*;
import okio.ByteString;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Type;
Expand All @@ -49,17 +51,18 @@ public class JSON {
private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter();
private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
private ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter();
private final Log log = LogFactory.getLog(JSON.class);

public static GsonBuilder createGson() {
GsonFireBuilder fireBuilder = new GsonFireBuilder()
.registerTypeSelector(Animal.class, new TypeSelector() {
@Override
public Class getClassForElement(JsonElement readElement) {
Map<String, Class> classByDiscriminatorValue = new HashMap<String, Class>();
classByDiscriminatorValue.put("Dog".toUpperCase(Locale.ROOT), Dog.class);
classByDiscriminatorValue.put("Cat".toUpperCase(Locale.ROOT), Cat.class);
classByDiscriminatorValue.put("BigCat".toUpperCase(Locale.ROOT), BigCat.class);
classByDiscriminatorValue.put("Animal".toUpperCase(Locale.ROOT), Animal.class);
classByDiscriminatorValue.put("Dog", Dog.class);
classByDiscriminatorValue.put("Cat", Cat.class);
classByDiscriminatorValue.put("BigCat", BigCat.class);
classByDiscriminatorValue.put("Animal", Animal.class);
return getClassByDiscriminator(classByDiscriminatorValue,
getDiscriminatorValue(readElement, "className"));
}
Expand All @@ -77,8 +80,16 @@ private static String getDiscriminatorValue(JsonElement readElement, String disc
return element.getAsString();
}

/**
* Returns the Java class that implements the OpenAPI schema for the specified discriminator value.
*
* @param classByDiscriminatorValue The map of discriminator values to Java classes.
* @param discriminatorValue The value of the OpenAPI discriminator in the input data.
* @return The Java class that implements the OpenAPI schema
*/
private static Class getClassByDiscriminator(Map classByDiscriminatorValue, String discriminatorValue) {
Class clazz = (Class) classByDiscriminatorValue.get(discriminatorValue.toUpperCase(Locale.ROOT));
log.debug("getClassByDiscriminator: {}", discriminatorValue);
Class clazz = (Class) classByDiscriminatorValue.get(discriminatorValue);
if (null == clazz) {
throw new IllegalArgumentException("cannot determine model class of name: <" + discriminatorValue + ">");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
public class Animal implements Parcelable {
public static final String SERIALIZED_NAME_CLASS_NAME = "className";
@SerializedName(SERIALIZED_NAME_CLASS_NAME)
private String className;
protected String className;

public static final String SERIALIZED_NAME_COLOR = "color";
@SerializedName(SERIALIZED_NAME_COLOR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.openapitools.client.model.*;
import okio.ByteString;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Type;
Expand All @@ -49,17 +51,18 @@ public class JSON {
private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter();
private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
private ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter();
private final Log log = LogFactory.getLog(JSON.class);

public static GsonBuilder createGson() {
GsonFireBuilder fireBuilder = new GsonFireBuilder()
.registerTypeSelector(Animal.class, new TypeSelector() {
@Override
public Class getClassForElement(JsonElement readElement) {
Map<String, Class> classByDiscriminatorValue = new HashMap<String, Class>();
classByDiscriminatorValue.put("Dog".toUpperCase(Locale.ROOT), Dog.class);
classByDiscriminatorValue.put("Cat".toUpperCase(Locale.ROOT), Cat.class);
classByDiscriminatorValue.put("BigCat".toUpperCase(Locale.ROOT), BigCat.class);
classByDiscriminatorValue.put("Animal".toUpperCase(Locale.ROOT), Animal.class);
classByDiscriminatorValue.put("Dog", Dog.class);
classByDiscriminatorValue.put("Cat", Cat.class);
classByDiscriminatorValue.put("BigCat", BigCat.class);
classByDiscriminatorValue.put("Animal", Animal.class);
return getClassByDiscriminator(classByDiscriminatorValue,
getDiscriminatorValue(readElement, "className"));
}
Expand All @@ -77,8 +80,16 @@ private static String getDiscriminatorValue(JsonElement readElement, String disc
return element.getAsString();
}

/**
* Returns the Java class that implements the OpenAPI schema for the specified discriminator value.
*
* @param classByDiscriminatorValue The map of discriminator values to Java classes.
* @param discriminatorValue The value of the OpenAPI discriminator in the input data.
* @return The Java class that implements the OpenAPI schema
*/
private static Class getClassByDiscriminator(Map classByDiscriminatorValue, String discriminatorValue) {
Class clazz = (Class) classByDiscriminatorValue.get(discriminatorValue.toUpperCase(Locale.ROOT));
log.debug("getClassByDiscriminator: {}", discriminatorValue);
Class clazz = (Class) classByDiscriminatorValue.get(discriminatorValue);
if (null == clazz) {
throw new IllegalArgumentException("cannot determine model class of name: <" + discriminatorValue + ">");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public class Animal {
public static final String SERIALIZED_NAME_CLASS_NAME = "className";
@SerializedName(SERIALIZED_NAME_CLASS_NAME)
private String className;
protected String className;

public static final String SERIALIZED_NAME_COLOR = "color";
@SerializedName(SERIALIZED_NAME_COLOR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.openapitools.client.model.*;
import okio.ByteString;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Type;
Expand All @@ -49,17 +51,18 @@ public class JSON {
private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter();
private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
private ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter();
private final Log log = LogFactory.getLog(JSON.class);

public static GsonBuilder createGson() {
GsonFireBuilder fireBuilder = new GsonFireBuilder()
.registerTypeSelector(Animal.class, new TypeSelector() {
@Override
public Class getClassForElement(JsonElement readElement) {
Map<String, Class> classByDiscriminatorValue = new HashMap<String, Class>();
classByDiscriminatorValue.put("Dog".toUpperCase(Locale.ROOT), Dog.class);
classByDiscriminatorValue.put("Cat".toUpperCase(Locale.ROOT), Cat.class);
classByDiscriminatorValue.put("BigCat".toUpperCase(Locale.ROOT), BigCat.class);
classByDiscriminatorValue.put("Animal".toUpperCase(Locale.ROOT), Animal.class);
classByDiscriminatorValue.put("Dog", Dog.class);
classByDiscriminatorValue.put("Cat", Cat.class);
classByDiscriminatorValue.put("BigCat", BigCat.class);
classByDiscriminatorValue.put("Animal", Animal.class);
return getClassByDiscriminator(classByDiscriminatorValue,
getDiscriminatorValue(readElement, "className"));
}
Expand All @@ -77,8 +80,16 @@ private static String getDiscriminatorValue(JsonElement readElement, String disc
return element.getAsString();
}

/**
* Returns the Java class that implements the OpenAPI schema for the specified discriminator value.
*
* @param classByDiscriminatorValue The map of discriminator values to Java classes.
* @param discriminatorValue The value of the OpenAPI discriminator in the input data.
* @return The Java class that implements the OpenAPI schema
*/
private static Class getClassByDiscriminator(Map classByDiscriminatorValue, String discriminatorValue) {
Class clazz = (Class) classByDiscriminatorValue.get(discriminatorValue.toUpperCase(Locale.ROOT));
log.debug("getClassByDiscriminator: {}", discriminatorValue);
Class clazz = (Class) classByDiscriminatorValue.get(discriminatorValue);
if (null == clazz) {
throw new IllegalArgumentException("cannot determine model class of name: <" + discriminatorValue + ">");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public class Animal {
public static final String SERIALIZED_NAME_CLASS_NAME = "className";
@SerializedName(SERIALIZED_NAME_CLASS_NAME)
private String className;
protected String className;

public static final String SERIALIZED_NAME_COLOR = "color";
@SerializedName(SERIALIZED_NAME_COLOR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

public class Animal {
public static final String JSON_PROPERTY_CLASS_NAME = "className";
private String className;
protected String className;

public static final String JSON_PROPERTY_COLOR = "color";
private String color = "red";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
public class Animal {
public static final String JSON_PROPERTY_CLASS_NAME = "className";
@XmlElement(name = "className")
private String className;
protected String className;

public static final String JSON_PROPERTY_COLOR = "color";
@XmlElement(name = "color")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

public class Animal {
public static final String JSON_PROPERTY_CLASS_NAME = "className";
private String className;
protected String className;

public static final String JSON_PROPERTY_COLOR = "color";
private String color = "red";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public class Animal {
public static final String SERIALIZED_NAME_CLASS_NAME = "className";
@SerializedName(SERIALIZED_NAME_CLASS_NAME)
private String className;
protected String className;

public static final String SERIALIZED_NAME_COLOR = "color";
@SerializedName(SERIALIZED_NAME_COLOR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

public class Animal {
public static final String JSON_PROPERTY_CLASS_NAME = "className";
private String className;
protected String className;

public static final String JSON_PROPERTY_COLOR = "color";
private String color = "red";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

public class Animal {
public static final String JSON_PROPERTY_CLASS_NAME = "className";
private String className;
protected String className;

public static final String JSON_PROPERTY_COLOR = "color";
private String color = "red";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

public class Animal {
public static final String JSON_PROPERTY_CLASS_NAME = "className";
private String className;
protected String className;

public static final String JSON_PROPERTY_COLOR = "color";
private String color = "red";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public class Animal {
public static final String SERIALIZED_NAME_CLASS_NAME = "className";
@SerializedName(SERIALIZED_NAME_CLASS_NAME)
private String className;
protected String className;

public static final String SERIALIZED_NAME_COLOR = "color";
@SerializedName(SERIALIZED_NAME_COLOR)
Expand Down
Loading