forked from loafle/openapi-generator-original
[Java][jersey2]Support enum discriminator value in child objects (#7267)
* support enum discriminator value in child (java jersey2) * update samples * add tests, use public
This commit is contained in:
parent
f11b2e6772
commit
8c1f6fcdc1
@ -784,7 +784,32 @@ public class JavaClientCodegen extends AbstractJavaCodegen
|
||||
// only add JsonNullable and related imports to optional and nullable values
|
||||
addImports |= isOptionalNullable;
|
||||
var.getVendorExtensions().put("x-is-jackson-optional-nullable", isOptionalNullable);
|
||||
|
||||
if (Boolean.TRUE.equals(var.getVendorExtensions().get("x-enum-as-string"))) {
|
||||
// treat enum string as just string
|
||||
var.datatypeWithEnum = var.dataType;
|
||||
|
||||
if (StringUtils.isNotEmpty(var.defaultValue)) { // has default value
|
||||
String defaultValue = var.defaultValue.substring(var.defaultValue.lastIndexOf('.') + 1);
|
||||
for (Map<String, Object> enumVars : (List<Map<String, Object>>) var.getAllowableValues().get("enumVars")) {
|
||||
if (defaultValue.equals(enumVars.get("name"))) {
|
||||
// update default to use the string directly instead of enum string
|
||||
var.defaultValue = (String) enumVars.get("value");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// add import for Set, HashSet
|
||||
cm.imports.add("Set");
|
||||
Map<String, String> importsSet = new HashMap<String, String>();
|
||||
importsSet.put("import", "java.util.Set");
|
||||
imports.add(importsSet);
|
||||
Map<String, String> importsHashSet = new HashMap<String, String>();
|
||||
importsHashSet.put("import", "java.util.HashSet");
|
||||
imports.add(importsHashSet);
|
||||
}
|
||||
}
|
||||
|
||||
if (addImports) {
|
||||
Map<String, String> imports2Classnames = new HashMap<String, String>() {{
|
||||
put("JsonNullable", "org.openapitools.jackson.nullable.JsonNullable");
|
||||
|
@ -18,7 +18,9 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE
|
||||
{{#vars}}
|
||||
{{#isEnum}}
|
||||
{{^isContainer}}
|
||||
{{^vendorExtensions.x-enum-as-string}}
|
||||
{{>modelInnerEnum}}
|
||||
{{/vendorExtensions.x-enum-as-string}}
|
||||
{{/isContainer}}
|
||||
{{#isContainer}}
|
||||
{{#mostInnerItems}}
|
||||
@ -98,7 +100,19 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE
|
||||
{{#vars}}
|
||||
|
||||
{{^isReadOnly}}
|
||||
{{#vendorExtensions.x-enum-as-string}}
|
||||
public static final Set<String> {{{nameInSnakeCase}}}_VALUES = new HashSet<>(Arrays.asList(
|
||||
{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}
|
||||
));
|
||||
|
||||
{{/vendorExtensions.x-enum-as-string}}
|
||||
public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) {
|
||||
{{#vendorExtensions.x-enum-as-string}}
|
||||
if (!{{{nameInSnakeCase}}}_VALUES.contains({{name}})) {
|
||||
throw new IllegalArgumentException({{name}} + " is invalid. Possible values for {{name}}: " + String.join(", ", {{{nameInSnakeCase}}}_VALUES));
|
||||
}
|
||||
|
||||
{{/vendorExtensions.x-enum-as-string}}
|
||||
{{#vendorExtensions.x-is-jackson-optional-nullable}}
|
||||
this.{{name}} = JsonNullable.<{{{datatypeWithEnum}}}>of({{name}});
|
||||
{{/vendorExtensions.x-is-jackson-optional-nullable}}
|
||||
@ -220,6 +234,12 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE
|
||||
|
||||
{{^isReadOnly}}
|
||||
public void {{setter}}({{{datatypeWithEnum}}} {{name}}) {
|
||||
{{#vendorExtensions.x-enum-as-string}}
|
||||
if (!{{{nameInSnakeCase}}}_VALUES.contains({{name}})) {
|
||||
throw new IllegalArgumentException({{name}} + " is invalid. Possible values for {{name}}: " + String.join(", ", {{{nameInSnakeCase}}}_VALUES));
|
||||
}
|
||||
|
||||
{{/vendorExtensions.x-enum-as-string}}
|
||||
{{#vendorExtensions.x-is-jackson-optional-nullable}}
|
||||
this.{{name}} = JsonNullable.<{{{datatypeWithEnum}}}>of({{name}});
|
||||
{{/vendorExtensions.x-is-jackson-optional-nullable}}
|
||||
|
@ -2087,6 +2087,12 @@ components:
|
||||
properties:
|
||||
name:
|
||||
type: string
|
||||
pet_type:
|
||||
x-enum-as-string: true
|
||||
type: string
|
||||
enum:
|
||||
- ChildCat
|
||||
default: ChildCat
|
||||
ArrayOfEnums:
|
||||
type: array
|
||||
items:
|
||||
|
@ -4,6 +4,7 @@
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**Name** | **string** | | [optional]
|
||||
**PetType** | **string** | | [optional] [default to PetTypeEnum.ChildCat]
|
||||
|
||||
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
|
||||
|
||||
|
@ -4,6 +4,7 @@
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**Name** | **string** | | [optional]
|
||||
**PetType** | **string** | | [optional] [default to PetTypeEnum.ChildCat]
|
||||
|
||||
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
|
||||
|
||||
|
@ -31,6 +31,25 @@ namespace Org.OpenAPITools.Model
|
||||
[DataContract(Name = "ChildCat")]
|
||||
public partial class ChildCat : ParentPet, IEquatable<ChildCat>, IValidatableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines PetType
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public enum PetTypeEnum
|
||||
{
|
||||
/// <summary>
|
||||
/// Enum ChildCat for value: ChildCat
|
||||
/// </summary>
|
||||
[EnumMember(Value = "ChildCat")]
|
||||
ChildCat = 1
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or Sets PetType
|
||||
/// </summary>
|
||||
[DataMember(Name = "pet_type", EmitDefaultValue = false)]
|
||||
public PetTypeEnum? PetType { get; set; }
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ChildCat" /> class.
|
||||
/// </summary>
|
||||
@ -40,10 +59,11 @@ namespace Org.OpenAPITools.Model
|
||||
/// Initializes a new instance of the <see cref="ChildCat" /> class.
|
||||
/// </summary>
|
||||
/// <param name="name">name.</param>
|
||||
/// <param name="petType">petType (required).</param>
|
||||
public ChildCat(string name = default(string), string petType = default(string)) : base()
|
||||
/// <param name="petType">petType (default to PetTypeEnum.ChildCat).</param>
|
||||
public ChildCat(string name = default(string), PetTypeEnum? petType = PetTypeEnum.ChildCat) : base()
|
||||
{
|
||||
this.Name = name;
|
||||
this.PetType = petType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -62,6 +82,7 @@ namespace Org.OpenAPITools.Model
|
||||
sb.Append("class ChildCat {\n");
|
||||
sb.Append(" ").Append(base.ToString().Replace("\n", "\n ")).Append("\n");
|
||||
sb.Append(" Name: ").Append(Name).Append("\n");
|
||||
sb.Append(" PetType: ").Append(PetType).Append("\n");
|
||||
sb.Append("}\n");
|
||||
return sb.ToString();
|
||||
}
|
||||
@ -106,6 +127,7 @@ namespace Org.OpenAPITools.Model
|
||||
int hashCode = base.GetHashCode();
|
||||
if (this.Name != null)
|
||||
hashCode = hashCode * 59 + this.Name.GetHashCode();
|
||||
hashCode = hashCode * 59 + this.PetType.GetHashCode();
|
||||
return hashCode;
|
||||
}
|
||||
}
|
||||
|
@ -31,13 +31,34 @@ namespace Org.OpenAPITools.Model
|
||||
[DataContract(Name = "ChildCat_allOf")]
|
||||
public partial class ChildCatAllOf : IEquatable<ChildCatAllOf>, IValidatableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines PetType
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public enum PetTypeEnum
|
||||
{
|
||||
/// <summary>
|
||||
/// Enum ChildCat for value: ChildCat
|
||||
/// </summary>
|
||||
[EnumMember(Value = "ChildCat")]
|
||||
ChildCat = 1
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or Sets PetType
|
||||
/// </summary>
|
||||
[DataMember(Name = "pet_type", EmitDefaultValue = false)]
|
||||
public PetTypeEnum? PetType { get; set; }
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ChildCatAllOf" /> class.
|
||||
/// </summary>
|
||||
/// <param name="name">name.</param>
|
||||
public ChildCatAllOf(string name = default(string))
|
||||
/// <param name="petType">petType (default to PetTypeEnum.ChildCat).</param>
|
||||
public ChildCatAllOf(string name = default(string), PetTypeEnum? petType = PetTypeEnum.ChildCat)
|
||||
{
|
||||
this.Name = name;
|
||||
this.PetType = petType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -55,6 +76,7 @@ namespace Org.OpenAPITools.Model
|
||||
var sb = new StringBuilder();
|
||||
sb.Append("class ChildCatAllOf {\n");
|
||||
sb.Append(" Name: ").Append(Name).Append("\n");
|
||||
sb.Append(" PetType: ").Append(PetType).Append("\n");
|
||||
sb.Append("}\n");
|
||||
return sb.ToString();
|
||||
}
|
||||
@ -99,6 +121,7 @@ namespace Org.OpenAPITools.Model
|
||||
int hashCode = 41;
|
||||
if (this.Name != null)
|
||||
hashCode = hashCode * 59 + this.Name.GetHashCode();
|
||||
hashCode = hashCode * 59 + this.PetType.GetHashCode();
|
||||
return hashCode;
|
||||
}
|
||||
}
|
||||
|
@ -2401,6 +2401,12 @@ components:
|
||||
properties:
|
||||
name:
|
||||
type: string
|
||||
pet_type:
|
||||
default: ChildCat
|
||||
enum:
|
||||
- ChildCat
|
||||
type: string
|
||||
x-enum-as-string: true
|
||||
type: object
|
||||
securitySchemes:
|
||||
petstore_auth:
|
||||
|
@ -7,6 +7,15 @@
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**name** | **String** | | [optional]
|
||||
**petType** | [**String**](#String) | | [optional]
|
||||
|
||||
|
||||
|
||||
## Enum: String
|
||||
|
||||
Name | Value
|
||||
---- | -----
|
||||
CHILDCAT | "ChildCat"
|
||||
|
||||
|
||||
|
||||
|
@ -7,6 +7,15 @@
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**name** | **String** | | [optional]
|
||||
**petType** | [**String**](#String) | | [optional]
|
||||
|
||||
|
||||
|
||||
## Enum: String
|
||||
|
||||
Name | Value
|
||||
---- | -----
|
||||
CHILDCAT | "ChildCat"
|
||||
|
||||
|
||||
|
||||
|
@ -32,6 +32,8 @@ import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import org.openapitools.client.model.ChildCatAllOf;
|
||||
import org.openapitools.client.model.ParentPet;
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
import org.openapitools.client.JSON;
|
||||
|
||||
@ -40,7 +42,8 @@ import org.openapitools.client.JSON;
|
||||
* ChildCat
|
||||
*/
|
||||
@JsonPropertyOrder({
|
||||
ChildCat.JSON_PROPERTY_NAME
|
||||
ChildCat.JSON_PROPERTY_NAME,
|
||||
ChildCat.JSON_PROPERTY_PET_TYPE
|
||||
})
|
||||
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "pet_type", visible = true)
|
||||
@ -49,6 +52,9 @@ public class ChildCat extends ParentPet {
|
||||
public static final String JSON_PROPERTY_NAME = "name";
|
||||
private String name;
|
||||
|
||||
public static final String JSON_PROPERTY_PET_TYPE = "pet_type";
|
||||
private String petType = "ChildCat";
|
||||
|
||||
|
||||
public ChildCat name(String name) {
|
||||
this.name = name;
|
||||
@ -73,6 +79,42 @@ public class ChildCat extends ParentPet {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
public static final Set<String> PET_TYPE_VALUES = new HashSet<>(Arrays.asList(
|
||||
"ChildCat"
|
||||
));
|
||||
|
||||
public ChildCat petType(String petType) {
|
||||
if (!PET_TYPE_VALUES.contains(petType)) {
|
||||
throw new IllegalArgumentException(petType + " is invalid. Possible values for petType: " + String.join(", ", PET_TYPE_VALUES));
|
||||
}
|
||||
|
||||
this.petType = petType;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get petType
|
||||
* @return petType
|
||||
**/
|
||||
@javax.annotation.Nullable
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty(JSON_PROPERTY_PET_TYPE)
|
||||
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
|
||||
|
||||
public String getPetType() {
|
||||
return petType;
|
||||
}
|
||||
|
||||
|
||||
public void setPetType(String petType) {
|
||||
if (!PET_TYPE_VALUES.contains(petType)) {
|
||||
throw new IllegalArgumentException(petType + " is invalid. Possible values for petType: " + String.join(", ", PET_TYPE_VALUES));
|
||||
}
|
||||
|
||||
this.petType = petType;
|
||||
}
|
||||
|
||||
/**
|
||||
* A container for additional, undeclared properties.
|
||||
* This is a holder for any undeclared properties as specified with
|
||||
@ -123,14 +165,15 @@ public class ChildCat extends ParentPet {
|
||||
return false;
|
||||
}
|
||||
ChildCat childCat = (ChildCat) o;
|
||||
return Objects.equals(this.name, childCat.name)&&
|
||||
return Objects.equals(this.name, childCat.name) &&
|
||||
Objects.equals(this.petType, childCat.petType)&&
|
||||
Objects.equals(this.additionalProperties, childCat.additionalProperties) &&
|
||||
super.equals(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(name, super.hashCode(), additionalProperties);
|
||||
return Objects.hash(name, petType, super.hashCode(), additionalProperties);
|
||||
}
|
||||
|
||||
|
||||
@ -140,6 +183,7 @@ public class ChildCat extends ParentPet {
|
||||
sb.append("class ChildCat {\n");
|
||||
sb.append(" ").append(toIndentedString(super.toString())).append("\n");
|
||||
sb.append(" name: ").append(toIndentedString(name)).append("\n");
|
||||
sb.append(" petType: ").append(toIndentedString(petType)).append("\n");
|
||||
sb.append(" additionalProperties: ").append(toIndentedString(additionalProperties)).append("\n");
|
||||
sb.append("}");
|
||||
return sb.toString();
|
||||
|
@ -24,6 +24,8 @@ import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
import org.openapitools.client.JSON;
|
||||
|
||||
@ -32,13 +34,17 @@ import org.openapitools.client.JSON;
|
||||
* ChildCatAllOf
|
||||
*/
|
||||
@JsonPropertyOrder({
|
||||
ChildCatAllOf.JSON_PROPERTY_NAME
|
||||
ChildCatAllOf.JSON_PROPERTY_NAME,
|
||||
ChildCatAllOf.JSON_PROPERTY_PET_TYPE
|
||||
})
|
||||
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
|
||||
public class ChildCatAllOf {
|
||||
public static final String JSON_PROPERTY_NAME = "name";
|
||||
private String name;
|
||||
|
||||
public static final String JSON_PROPERTY_PET_TYPE = "pet_type";
|
||||
private String petType = "ChildCat";
|
||||
|
||||
|
||||
public ChildCatAllOf name(String name) {
|
||||
this.name = name;
|
||||
@ -64,6 +70,42 @@ public class ChildCatAllOf {
|
||||
}
|
||||
|
||||
|
||||
public static final Set<String> PET_TYPE_VALUES = new HashSet<>(Arrays.asList(
|
||||
"ChildCat"
|
||||
));
|
||||
|
||||
public ChildCatAllOf petType(String petType) {
|
||||
if (!PET_TYPE_VALUES.contains(petType)) {
|
||||
throw new IllegalArgumentException(petType + " is invalid. Possible values for petType: " + String.join(", ", PET_TYPE_VALUES));
|
||||
}
|
||||
|
||||
this.petType = petType;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get petType
|
||||
* @return petType
|
||||
**/
|
||||
@javax.annotation.Nullable
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty(JSON_PROPERTY_PET_TYPE)
|
||||
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
|
||||
|
||||
public String getPetType() {
|
||||
return petType;
|
||||
}
|
||||
|
||||
|
||||
public void setPetType(String petType) {
|
||||
if (!PET_TYPE_VALUES.contains(petType)) {
|
||||
throw new IllegalArgumentException(petType + " is invalid. Possible values for petType: " + String.join(", ", PET_TYPE_VALUES));
|
||||
}
|
||||
|
||||
this.petType = petType;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return true if this ChildCat_allOf object is equal to o.
|
||||
*/
|
||||
@ -76,12 +118,13 @@ public class ChildCatAllOf {
|
||||
return false;
|
||||
}
|
||||
ChildCatAllOf childCatAllOf = (ChildCatAllOf) o;
|
||||
return Objects.equals(this.name, childCatAllOf.name);
|
||||
return Objects.equals(this.name, childCatAllOf.name) &&
|
||||
Objects.equals(this.petType, childCatAllOf.petType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(name);
|
||||
return Objects.hash(name, petType);
|
||||
}
|
||||
|
||||
|
||||
@ -90,6 +133,7 @@ public class ChildCatAllOf {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("class ChildCatAllOf {\n");
|
||||
sb.append(" name: ").append(toIndentedString(name)).append("\n");
|
||||
sb.append(" petType: ").append(toIndentedString(petType)).append("\n");
|
||||
sb.append("}");
|
||||
return sb.toString();
|
||||
}
|
||||
|
@ -66,6 +66,18 @@ public class JSONComposedSchemaTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to ensure the setter will throw IllegalArgumentException
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testEnumDiscriminator() throws Exception {
|
||||
ChildCat cc = new ChildCat();
|
||||
cc.setPetType("ChildCat");
|
||||
assertEquals("ChildCat", cc.getPetType());
|
||||
|
||||
cc.setPetType("WrongValue");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to ensure the getter will throw ClassCastException
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user