Skip to content

Commit 8c1f6fc

Browse files
authored
[Java][jersey2]Support enum discriminator value in child objects (#7267)
* support enum discriminator value in child (java jersey2) * update samples * add tests, use public
1 parent f11b2e6 commit 8c1f6fc

File tree

13 files changed

+231
-9
lines changed

13 files changed

+231
-9
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,32 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
784784
// only add JsonNullable and related imports to optional and nullable values
785785
addImports |= isOptionalNullable;
786786
var.getVendorExtensions().put("x-is-jackson-optional-nullable", isOptionalNullable);
787+
788+
if (Boolean.TRUE.equals(var.getVendorExtensions().get("x-enum-as-string"))) {
789+
// treat enum string as just string
790+
var.datatypeWithEnum = var.dataType;
791+
792+
if (StringUtils.isNotEmpty(var.defaultValue)) { // has default value
793+
String defaultValue = var.defaultValue.substring(var.defaultValue.lastIndexOf('.') + 1);
794+
for (Map<String, Object> enumVars : (List<Map<String, Object>>) var.getAllowableValues().get("enumVars")) {
795+
if (defaultValue.equals(enumVars.get("name"))) {
796+
// update default to use the string directly instead of enum string
797+
var.defaultValue = (String) enumVars.get("value");
798+
}
799+
}
800+
}
801+
802+
// add import for Set, HashSet
803+
cm.imports.add("Set");
804+
Map<String, String> importsSet = new HashMap<String, String>();
805+
importsSet.put("import", "java.util.Set");
806+
imports.add(importsSet);
807+
Map<String, String> importsHashSet = new HashMap<String, String>();
808+
importsHashSet.put("import", "java.util.HashSet");
809+
imports.add(importsHashSet);
810+
}
787811
}
812+
788813
if (addImports) {
789814
Map<String, String> imports2Classnames = new HashMap<String, String>() {{
790815
put("JsonNullable", "org.openapitools.jackson.nullable.JsonNullable");

modules/openapi-generator/src/main/resources/Java/libraries/jersey2/pojo.mustache

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE
1818
{{#vars}}
1919
{{#isEnum}}
2020
{{^isContainer}}
21+
{{^vendorExtensions.x-enum-as-string}}
2122
{{>modelInnerEnum}}
23+
{{/vendorExtensions.x-enum-as-string}}
2224
{{/isContainer}}
2325
{{#isContainer}}
2426
{{#mostInnerItems}}
@@ -98,7 +100,19 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE
98100
{{#vars}}
99101

100102
{{^isReadOnly}}
103+
{{#vendorExtensions.x-enum-as-string}}
104+
public static final Set<String> {{{nameInSnakeCase}}}_VALUES = new HashSet<>(Arrays.asList(
105+
{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}
106+
));
107+
108+
{{/vendorExtensions.x-enum-as-string}}
101109
public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) {
110+
{{#vendorExtensions.x-enum-as-string}}
111+
if (!{{{nameInSnakeCase}}}_VALUES.contains({{name}})) {
112+
throw new IllegalArgumentException({{name}} + " is invalid. Possible values for {{name}}: " + String.join(", ", {{{nameInSnakeCase}}}_VALUES));
113+
}
114+
115+
{{/vendorExtensions.x-enum-as-string}}
102116
{{#vendorExtensions.x-is-jackson-optional-nullable}}
103117
this.{{name}} = JsonNullable.<{{{datatypeWithEnum}}}>of({{name}});
104118
{{/vendorExtensions.x-is-jackson-optional-nullable}}
@@ -220,6 +234,12 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE
220234
221235
{{^isReadOnly}}
222236
public void {{setter}}({{{datatypeWithEnum}}} {{name}}) {
237+
{{#vendorExtensions.x-enum-as-string}}
238+
if (!{{{nameInSnakeCase}}}_VALUES.contains({{name}})) {
239+
throw new IllegalArgumentException({{name}} + " is invalid. Possible values for {{name}}: " + String.join(", ", {{{nameInSnakeCase}}}_VALUES));
240+
}
241+
242+
{{/vendorExtensions.x-enum-as-string}}
223243
{{#vendorExtensions.x-is-jackson-optional-nullable}}
224244
this.{{name}} = JsonNullable.<{{{datatypeWithEnum}}}>of({{name}});
225245
{{/vendorExtensions.x-is-jackson-optional-nullable}}

modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,6 +2087,12 @@ components:
20872087
properties:
20882088
name:
20892089
type: string
2090+
pet_type:
2091+
x-enum-as-string: true
2092+
type: string
2093+
enum:
2094+
- ChildCat
2095+
default: ChildCat
20902096
ArrayOfEnums:
20912097
type: array
20922098
items:

samples/client/petstore/csharp-netcore/OpenAPIClientCore/docs/ChildCat.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Name | Type | Description | Notes
55
------------ | ------------- | ------------- | -------------
66
**Name** | **string** | | [optional]
7+
**PetType** | **string** | | [optional] [default to PetTypeEnum.ChildCat]
78

89
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
910

samples/client/petstore/csharp-netcore/OpenAPIClientCore/docs/ChildCatAllOf.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Name | Type | Description | Notes
55
------------ | ------------- | ------------- | -------------
66
**Name** | **string** | | [optional]
7+
**PetType** | **string** | | [optional] [default to PetTypeEnum.ChildCat]
78

89
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
910

samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/ChildCat.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,25 @@ namespace Org.OpenAPITools.Model
3131
[DataContract(Name = "ChildCat")]
3232
public partial class ChildCat : ParentPet, IEquatable<ChildCat>, IValidatableObject
3333
{
34+
/// <summary>
35+
/// Defines PetType
36+
/// </summary>
37+
[JsonConverter(typeof(StringEnumConverter))]
38+
public enum PetTypeEnum
39+
{
40+
/// <summary>
41+
/// Enum ChildCat for value: ChildCat
42+
/// </summary>
43+
[EnumMember(Value = "ChildCat")]
44+
ChildCat = 1
45+
46+
}
47+
48+
/// <summary>
49+
/// Gets or Sets PetType
50+
/// </summary>
51+
[DataMember(Name = "pet_type", EmitDefaultValue = false)]
52+
public PetTypeEnum? PetType { get; set; }
3453
/// <summary>
3554
/// Initializes a new instance of the <see cref="ChildCat" /> class.
3655
/// </summary>
@@ -40,10 +59,11 @@ protected ChildCat() { }
4059
/// Initializes a new instance of the <see cref="ChildCat" /> class.
4160
/// </summary>
4261
/// <param name="name">name.</param>
43-
/// <param name="petType">petType (required).</param>
44-
public ChildCat(string name = default(string), string petType = default(string)) : base()
62+
/// <param name="petType">petType (default to PetTypeEnum.ChildCat).</param>
63+
public ChildCat(string name = default(string), PetTypeEnum? petType = PetTypeEnum.ChildCat) : base()
4564
{
4665
this.Name = name;
66+
this.PetType = petType;
4767
}
4868

4969
/// <summary>
@@ -62,6 +82,7 @@ public override string ToString()
6282
sb.Append("class ChildCat {\n");
6383
sb.Append(" ").Append(base.ToString().Replace("\n", "\n ")).Append("\n");
6484
sb.Append(" Name: ").Append(Name).Append("\n");
85+
sb.Append(" PetType: ").Append(PetType).Append("\n");
6586
sb.Append("}\n");
6687
return sb.ToString();
6788
}
@@ -106,6 +127,7 @@ public override int GetHashCode()
106127
int hashCode = base.GetHashCode();
107128
if (this.Name != null)
108129
hashCode = hashCode * 59 + this.Name.GetHashCode();
130+
hashCode = hashCode * 59 + this.PetType.GetHashCode();
109131
return hashCode;
110132
}
111133
}

samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/ChildCatAllOf.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,34 @@ namespace Org.OpenAPITools.Model
3131
[DataContract(Name = "ChildCat_allOf")]
3232
public partial class ChildCatAllOf : IEquatable<ChildCatAllOf>, IValidatableObject
3333
{
34+
/// <summary>
35+
/// Defines PetType
36+
/// </summary>
37+
[JsonConverter(typeof(StringEnumConverter))]
38+
public enum PetTypeEnum
39+
{
40+
/// <summary>
41+
/// Enum ChildCat for value: ChildCat
42+
/// </summary>
43+
[EnumMember(Value = "ChildCat")]
44+
ChildCat = 1
45+
46+
}
47+
48+
/// <summary>
49+
/// Gets or Sets PetType
50+
/// </summary>
51+
[DataMember(Name = "pet_type", EmitDefaultValue = false)]
52+
public PetTypeEnum? PetType { get; set; }
3453
/// <summary>
3554
/// Initializes a new instance of the <see cref="ChildCatAllOf" /> class.
3655
/// </summary>
3756
/// <param name="name">name.</param>
38-
public ChildCatAllOf(string name = default(string))
57+
/// <param name="petType">petType (default to PetTypeEnum.ChildCat).</param>
58+
public ChildCatAllOf(string name = default(string), PetTypeEnum? petType = PetTypeEnum.ChildCat)
3959
{
4060
this.Name = name;
61+
this.PetType = petType;
4162
}
4263

4364
/// <summary>
@@ -55,6 +76,7 @@ public override string ToString()
5576
var sb = new StringBuilder();
5677
sb.Append("class ChildCatAllOf {\n");
5778
sb.Append(" Name: ").Append(Name).Append("\n");
79+
sb.Append(" PetType: ").Append(PetType).Append("\n");
5880
sb.Append("}\n");
5981
return sb.ToString();
6082
}
@@ -99,6 +121,7 @@ public override int GetHashCode()
99121
int hashCode = 41;
100122
if (this.Name != null)
101123
hashCode = hashCode * 59 + this.Name.GetHashCode();
124+
hashCode = hashCode * 59 + this.PetType.GetHashCode();
102125
return hashCode;
103126
}
104127
}

samples/openapi3/client/petstore/java/jersey2-java8/api/openapi.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,6 +2401,12 @@ components:
24012401
properties:
24022402
name:
24032403
type: string
2404+
pet_type:
2405+
default: ChildCat
2406+
enum:
2407+
- ChildCat
2408+
type: string
2409+
x-enum-as-string: true
24042410
type: object
24052411
securitySchemes:
24062412
petstore_auth:

samples/openapi3/client/petstore/java/jersey2-java8/docs/ChildCat.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
Name | Type | Description | Notes
88
------------ | ------------- | ------------- | -------------
99
**name** | **String** | | [optional]
10+
**petType** | [**String**](#String) | | [optional]
11+
12+
13+
14+
## Enum: String
15+
16+
Name | Value
17+
---- | -----
18+
CHILDCAT | &quot;ChildCat&quot;
1019

1120

1221

samples/openapi3/client/petstore/java/jersey2-java8/docs/ChildCatAllOf.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
Name | Type | Description | Notes
88
------------ | ------------- | ------------- | -------------
99
**name** | **String** | | [optional]
10+
**petType** | [**String**](#String) | | [optional]
11+
12+
13+
14+
## Enum: String
15+
16+
Name | Value
17+
---- | -----
18+
CHILDCAT | &quot;ChildCat&quot;
1019

1120

1221

0 commit comments

Comments
 (0)