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][jersey2]Support enum discriminator value in child objects #7267

Merged
merged 3 commits into from
Aug 27, 2020
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 @@ -784,7 +784,32 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
// 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down Expand Up @@ -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}}
Expand Down Expand Up @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Original file line number Diff line number Diff line change
Expand Up @@ -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)

Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -40,10 +59,11 @@ protected ChildCat() { }
/// 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>
Expand All @@ -62,6 +82,7 @@ public override string ToString()
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();
}
Expand Down Expand Up @@ -106,6 +127,7 @@ public override int GetHashCode()
int hashCode = base.GetHashCode();
if (this.Name != null)
hashCode = hashCode * 59 + this.Name.GetHashCode();
hashCode = hashCode * 59 + this.PetType.GetHashCode();
return hashCode;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -55,6 +76,7 @@ public override string ToString()
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();
}
Expand Down Expand Up @@ -99,6 +121,7 @@ public override int GetHashCode()
int hashCode = 41;
if (this.Name != null)
hashCode = hashCode * 59 + this.Name.GetHashCode();
hashCode = hashCode * 59 + this.PetType.GetHashCode();
return hashCode;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**name** | **String** | | [optional]
**petType** | [**String**](#String) | | [optional]



## Enum: String

Name | Value
---- | -----
CHILDCAT | &quot;ChildCat&quot;



Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**name** | **String** | | [optional]
**petType** | [**String**](#String) | | [optional]



## Enum: String

Name | Value
---- | -----
CHILDCAT | &quot;ChildCat&quot;



Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
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;

Expand All @@ -40,7 +42,8 @@
* 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)
Expand All @@ -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;
Expand All @@ -73,6 +79,42 @@ public void setName(String name) {
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
Expand Down Expand Up @@ -123,14 +165,15 @@ public boolean equals(java.lang.Object o) {
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);
}


Expand All @@ -140,6 +183,7 @@ public String toString() {
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();
Expand Down
Loading