Skip to content

Commit b41e61c

Browse files
committed
Update samples
1 parent 4767cb6 commit b41e61c

29 files changed

Lines changed: 234 additions & 189 deletions

File tree

  • modules/openapi-generator/src/main/resources/Java
  • samples
    • client/petstore/java
      • apache-httpclient/src/main/java/org/openapitools/client/model
      • feign-no-nullable/src/main/java/org/openapitools/client/model
      • feign/src/main/java/org/openapitools/client/model
      • google-api-client/src/main/java/org/openapitools/client/model
      • jersey2-java8-localdatetime/src/main/java/org/openapitools/client/model
      • jersey2-java8/src/main/java/org/openapitools/client/model
      • jersey3/src/main/java/org/openapitools/client/model
      • native-async/src/main/java/org/openapitools/client/model
      • native/src/main/java/org/openapitools/client/model
      • rest-assured-jackson/src/main/java/org/openapitools/client/model
      • rest-assured/src/main/java/org/openapitools/client/model
      • restclient-swagger2/src/main/java/org/openapitools/client/model
      • restclient/src/main/java/org/openapitools/client/model
      • resteasy/src/main/java/org/openapitools/client/model
      • resttemplate-withXml/src/main/java/org/openapitools/client/model
      • resttemplate/src/main/java/org/openapitools/client/model
      • retrofit2-play26/src/main/java/org/openapitools/client/model
      • retrofit2rx2/src/main/java/org/openapitools/client/model
      • retrofit2rx3/src/main/java/org/openapitools/client/model
      • retrofit2/src/main/java/org/openapitools/client/model
      • vertx-no-nullable/src/main/java/org/openapitools/client/model
      • vertx/src/main/java/org/openapitools/client/model
      • webclient-jakarta/src/main/java/org/openapitools/client/model
      • webclient-swagger2/src/main/java/org/openapitools/client/model
      • webclient/src/main/java/org/openapitools/client/model
    • openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/model

modules/openapi-generator/src/main/resources/Java/modelInnerEnum.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
{{#withXml}}
2424
@XmlEnumValue({{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}})
2525
{{/withXml}}
26-
{{{name}}}({{^isUri}}{{dataType}}.valueOf({{/isUri}}{{{value}}}{{^isUri}}){{/isUri}}){{^-last}},
26+
{{{name}}}({{^isUri}}{{^isString}}{{dataType}}.valueOf({{/isString}}{{/isUri}}{{{value}}}{{^isUri}}{{^isString}}){{/isString}}{{/isUri}}){{^-last}},
2727
{{/-last}}{{#-last}};{{/-last}}
2828
{{/enumVars}}
2929
{{/allowableValues}}

samples/client/petstore/java/apache-httpclient/src/main/java/org/openapitools/client/model/EnumTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ public static EnumStringRequiredEnum fromValue(String value) {
134134
* Gets or Sets enumInteger
135135
*/
136136
public enum EnumIntegerEnum {
137-
NUMBER_1(1),
137+
NUMBER_1(Integer.valueOf(1)),
138138

139-
NUMBER_MINUS_1(-1);
139+
NUMBER_MINUS_1(Integer.valueOf(-1));
140140

141141
private Integer value;
142142

@@ -172,9 +172,9 @@ public static EnumIntegerEnum fromValue(Integer value) {
172172
* Gets or Sets enumNumber
173173
*/
174174
public enum EnumNumberEnum {
175-
NUMBER_1_DOT_1(1.1),
175+
NUMBER_1_DOT_1(Double.valueOf(1.1)),
176176

177-
NUMBER_MINUS_1_DOT_2(-1.2);
177+
NUMBER_MINUS_1_DOT_2(Double.valueOf(-1.2));
178178

179179
private Double value;
180180

samples/client/petstore/java/feign-no-nullable/src/main/java/org/openapitools/client/model/EnumTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ public static EnumStringRequiredEnum fromValue(String value) {
122122
* Gets or Sets enumInteger
123123
*/
124124
public enum EnumIntegerEnum {
125-
NUMBER_1(1),
125+
NUMBER_1(Integer.valueOf(1)),
126126

127-
NUMBER_MINUS_1(-1);
127+
NUMBER_MINUS_1(Integer.valueOf(-1));
128128

129129
private Integer value;
130130

@@ -160,9 +160,9 @@ public static EnumIntegerEnum fromValue(Integer value) {
160160
* Gets or Sets enumNumber
161161
*/
162162
public enum EnumNumberEnum {
163-
NUMBER_1_DOT_1(1.1),
163+
NUMBER_1_DOT_1(Double.valueOf(1.1)),
164164

165-
NUMBER_MINUS_1_DOT_2(-1.2);
165+
NUMBER_MINUS_1_DOT_2(Double.valueOf(-1.2));
166166

167167
private Double value;
168168

samples/client/petstore/java/feign/src/main/java/org/openapitools/client/model/EnumTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ public static EnumStringRequiredEnum fromValue(String value) {
131131
* Gets or Sets enumInteger
132132
*/
133133
public enum EnumIntegerEnum {
134-
NUMBER_1(1),
134+
NUMBER_1(Integer.valueOf(1)),
135135

136-
NUMBER_MINUS_1(-1);
136+
NUMBER_MINUS_1(Integer.valueOf(-1));
137137

138138
private Integer value;
139139

@@ -169,9 +169,9 @@ public static EnumIntegerEnum fromValue(Integer value) {
169169
* Gets or Sets enumNumber
170170
*/
171171
public enum EnumNumberEnum {
172-
NUMBER_1_DOT_1(1.1),
172+
NUMBER_1_DOT_1(Double.valueOf(1.1)),
173173

174-
NUMBER_MINUS_1_DOT_2(-1.2);
174+
NUMBER_MINUS_1_DOT_2(Double.valueOf(-1.2));
175175

176176
private Double value;
177177

samples/client/petstore/java/google-api-client/src/main/java/org/openapitools/client/model/EnumTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ public static EnumStringRequiredEnum fromValue(String value) {
121121
* Gets or Sets enumInteger
122122
*/
123123
public enum EnumIntegerEnum {
124-
NUMBER_1(1),
124+
NUMBER_1(Integer.valueOf(1)),
125125

126-
NUMBER_MINUS_1(-1);
126+
NUMBER_MINUS_1(Integer.valueOf(-1));
127127

128128
private Integer value;
129129

@@ -159,9 +159,9 @@ public static EnumIntegerEnum fromValue(Integer value) {
159159
* Gets or Sets enumNumber
160160
*/
161161
public enum EnumNumberEnum {
162-
NUMBER_1_DOT_1(1.1),
162+
NUMBER_1_DOT_1(Double.valueOf(1.1)),
163163

164-
NUMBER_MINUS_1_DOT_2(-1.2);
164+
NUMBER_MINUS_1_DOT_2(Double.valueOf(-1.2));
165165

166166
private Double value;
167167

samples/client/petstore/java/jersey2-java8-localdatetime/src/main/java/org/openapitools/client/model/EnumTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ public static EnumStringRequiredEnum fromValue(String value) {
124124
* Gets or Sets enumInteger
125125
*/
126126
public enum EnumIntegerEnum {
127-
NUMBER_1(1),
127+
NUMBER_1(Integer.valueOf(1)),
128128

129-
NUMBER_MINUS_1(-1);
129+
NUMBER_MINUS_1(Integer.valueOf(-1));
130130

131131
private Integer value;
132132

@@ -162,9 +162,9 @@ public static EnumIntegerEnum fromValue(Integer value) {
162162
* Gets or Sets enumNumber
163163
*/
164164
public enum EnumNumberEnum {
165-
NUMBER_1_DOT_1(1.1),
165+
NUMBER_1_DOT_1(Double.valueOf(1.1)),
166166

167-
NUMBER_MINUS_1_DOT_2(-1.2);
167+
NUMBER_MINUS_1_DOT_2(Double.valueOf(-1.2));
168168

169169
private Double value;
170170

samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/model/EnumTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ public static EnumStringRequiredEnum fromValue(String value) {
124124
* Gets or Sets enumInteger
125125
*/
126126
public enum EnumIntegerEnum {
127-
NUMBER_1(1),
127+
NUMBER_1(Integer.valueOf(1)),
128128

129-
NUMBER_MINUS_1(-1);
129+
NUMBER_MINUS_1(Integer.valueOf(-1));
130130

131131
private Integer value;
132132

@@ -162,9 +162,9 @@ public static EnumIntegerEnum fromValue(Integer value) {
162162
* Gets or Sets enumNumber
163163
*/
164164
public enum EnumNumberEnum {
165-
NUMBER_1_DOT_1(1.1),
165+
NUMBER_1_DOT_1(Double.valueOf(1.1)),
166166

167-
NUMBER_MINUS_1_DOT_2(-1.2);
167+
NUMBER_MINUS_1_DOT_2(Double.valueOf(-1.2));
168168

169169
private Double value;
170170

samples/client/petstore/java/jersey3/src/main/java/org/openapitools/client/model/EnumTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ public static EnumStringRequiredEnum fromValue(String value) {
137137
* Gets or Sets enumInteger
138138
*/
139139
public enum EnumIntegerEnum {
140-
NUMBER_1(1),
140+
NUMBER_1(Integer.valueOf(1)),
141141

142-
NUMBER_MINUS_1(-1);
142+
NUMBER_MINUS_1(Integer.valueOf(-1));
143143

144144
private Integer value;
145145

@@ -175,9 +175,9 @@ public static EnumIntegerEnum fromValue(Integer value) {
175175
* Gets or Sets enumIntegerOnly
176176
*/
177177
public enum EnumIntegerOnlyEnum {
178-
NUMBER_2(2),
178+
NUMBER_2(Integer.valueOf(2)),
179179

180-
NUMBER_MINUS_2(-2);
180+
NUMBER_MINUS_2(Integer.valueOf(-2));
181181

182182
private Integer value;
183183

@@ -213,9 +213,9 @@ public static EnumIntegerOnlyEnum fromValue(Integer value) {
213213
* Gets or Sets enumNumber
214214
*/
215215
public enum EnumNumberEnum {
216-
NUMBER_1_DOT_1(1.1),
216+
NUMBER_1_DOT_1(Double.valueOf(1.1)),
217217

218-
NUMBER_MINUS_1_DOT_2(-1.2);
218+
NUMBER_MINUS_1_DOT_2(Double.valueOf(-1.2));
219219

220220
private Double value;
221221

samples/client/petstore/java/jersey3/src/main/java/org/openapitools/client/model/GmFruit.java

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@
4343
import com.fasterxml.jackson.core.JsonGenerator;
4444
import com.fasterxml.jackson.core.JsonParser;
4545
import com.fasterxml.jackson.core.JsonProcessingException;
46+
import com.fasterxml.jackson.core.JsonToken;
4647
import com.fasterxml.jackson.core.type.TypeReference;
4748
import com.fasterxml.jackson.databind.DeserializationContext;
4849
import com.fasterxml.jackson.databind.JsonMappingException;
4950
import com.fasterxml.jackson.databind.JsonNode;
51+
import com.fasterxml.jackson.databind.MapperFeature;
5052
import com.fasterxml.jackson.databind.SerializerProvider;
5153
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
5254
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@@ -89,29 +91,47 @@ public GmFruit deserialize(JsonParser jp, DeserializationContext ctxt) throws IO
8991
JsonNode tree = jp.readValueAsTree();
9092

9193
Object deserialized = null;
92-
// deserialize Apple
94+
boolean typeCoercion = ctxt.isEnabled(MapperFeature.ALLOW_COERCION_OF_SCALARS);
95+
int match = 0;
96+
JsonToken token = tree.traverse(jp.getCodec()).nextToken();
97+
// deserialize Apple (nullable)
9398
try {
94-
deserialized = tree.traverse(jp.getCodec()).readValueAs(Apple.class);
95-
GmFruit ret = new GmFruit();
96-
ret.setActualInstance(deserialized);
97-
return ret;
99+
boolean attemptParsing = true;
100+
if (attemptParsing) {
101+
deserialized = tree.traverse(jp.getCodec()).readValueAs(Apple.class);
102+
// TODO: there is no validation against JSON schema constraints
103+
// (min, max, enum, pattern...), this does not perform a strict JSON
104+
// validation, which means the 'match' count may be higher than it should be.
105+
match++;
106+
log.log(Level.FINER, "Input data matches schema 'Apple'");
107+
}
98108
} catch (Exception e) {
99-
// deserialization failed, continue, log to help debugging
100-
log.log(Level.FINER, "Input data does not match 'GmFruit'", e);
109+
// deserialization failed, continue
110+
log.log(Level.FINER, "Input data does not match schema 'Apple'", e);
101111
}
102112

103113
// deserialize Banana
104114
try {
105-
deserialized = tree.traverse(jp.getCodec()).readValueAs(Banana.class);
115+
boolean attemptParsing = true;
116+
if (attemptParsing) {
117+
deserialized = tree.traverse(jp.getCodec()).readValueAs(Banana.class);
118+
// TODO: there is no validation against JSON schema constraints
119+
// (min, max, enum, pattern...), this does not perform a strict JSON
120+
// validation, which means the 'match' count may be higher than it should be.
121+
match++;
122+
log.log(Level.FINER, "Input data matches schema 'Banana'");
123+
}
124+
} catch (Exception e) {
125+
// deserialization failed, continue
126+
log.log(Level.FINER, "Input data does not match schema 'Banana'", e);
127+
}
128+
129+
if (match == 1) {
106130
GmFruit ret = new GmFruit();
107131
ret.setActualInstance(deserialized);
108132
return ret;
109-
} catch (Exception e) {
110-
// deserialization failed, continue, log to help debugging
111-
log.log(Level.FINER, "Input data does not match 'GmFruit'", e);
112133
}
113-
114-
throw new IOException(String.format("Failed deserialization for GmFruit: no match found"));
134+
throw new IOException(String.format("Failed deserialization for GmFruit: %d classes match result, expected 1", match));
115135
}
116136

117137
/**
@@ -131,7 +151,7 @@ public GmFruit() {
131151
}
132152

133153
public GmFruit(Apple o) {
134-
super("anyOf", Boolean.FALSE);
154+
super("anyOf", Boolean.TRUE);
135155
setActualInstance(o);
136156
}
137157

@@ -188,23 +208,23 @@ public Object getActualInstance() {
188208
}
189209

190210
/**
191-
* Get the actual instance of `Apple`. If the actual instance is not `Apple`,
192-
* the ClassCastException will be thrown.
193-
*
194-
* @return The actual instance of `Apple`
195-
* @throws ClassCastException if the instance is not `Apple`
196-
*/
211+
* Get the actual instance of `Apple`. If the actual instance is not `Apple`,
212+
* the ClassCastException will be thrown.
213+
*
214+
* @return The actual instance of `Apple`
215+
* @throws ClassCastException if the instance is not `Apple`
216+
*/
197217
public Apple getApple() throws ClassCastException {
198218
return (Apple)super.getActualInstance();
199219
}
200220

201221
/**
202-
* Get the actual instance of `Banana`. If the actual instance is not `Banana`,
203-
* the ClassCastException will be thrown.
204-
*
205-
* @return The actual instance of `Banana`
206-
* @throws ClassCastException if the instance is not `Banana`
207-
*/
222+
* Get the actual instance of `Banana`. If the actual instance is not `Banana`,
223+
* the ClassCastException will be thrown.
224+
*
225+
* @return The actual instance of `Banana`
226+
* @throws ClassCastException if the instance is not `Banana`
227+
*/
208228
public Banana getBanana() throws ClassCastException {
209229
return (Banana)super.getActualInstance();
210230
}

0 commit comments

Comments
 (0)