Skip to content

Commit c1b8c29

Browse files
authored
[Java][jersey2] Add helper methods for oneOf Java classes (#7115)
* add helper methods for oneOf java class * better test comment
1 parent aa69863 commit c1b8c29

File tree

11 files changed

+359
-11
lines changed

11 files changed

+359
-11
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
183183

184184
/**
185185
* Set the instance that matches the oneOf child schema, check
186-
* the instance parameter is valid against the oneOf child schemas.
186+
* the instance parameter is valid against the oneOf child schemas:
187+
* {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}
187188
*
188189
* It could be an instance of the 'oneOf' schemas.
189190
* The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf).
@@ -207,6 +208,28 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
207208
throw new RuntimeException("Invalid instance type. Must be {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}");
208209
}
209210

211+
/**
212+
* Get the actual instance, which can be the following:
213+
* {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}
214+
*
215+
* @return The actual instance ({{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}})
216+
*/
217+
@Override
218+
public Object getActualInstance() {
219+
return super.getActualInstance();
220+
}
210221

222+
{{#oneOf}}
223+
/**
224+
* Get the actual instance of `{{{.}}}`. If the actual instanct is not `{{{.}}}`,
225+
* the ClassCastException will be thrown.
226+
*
227+
* @return The actual instance of `{{{.}}}`
228+
* @throws ClassCastException if the instance is not `{{{.}}}`
229+
*/
230+
public {{{.}}} get{{{.}}}() throws ClassCastException {
231+
return ({{{.}}})super.getActualInstance();
232+
}
211233

234+
{{/oneOf}}
212235
}

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ public Map<String, GenericType> getSchemas() {
195195

196196
/**
197197
* Set the instance that matches the oneOf child schema, check
198-
* the instance parameter is valid against the oneOf child schemas.
198+
* the instance parameter is valid against the oneOf child schemas:
199+
* Apple, Banana
199200
*
200201
* It could be an instance of the 'oneOf' schemas.
201202
* The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf).
@@ -215,7 +216,38 @@ public void setActualInstance(Object instance) {
215216
throw new RuntimeException("Invalid instance type. Must be Apple, Banana");
216217
}
217218

219+
/**
220+
* Get the actual instance, which can be the following:
221+
* Apple, Banana
222+
*
223+
* @return The actual instance (Apple, Banana)
224+
*/
225+
@Override
226+
public Object getActualInstance() {
227+
return super.getActualInstance();
228+
}
229+
230+
/**
231+
* Get the actual instance of `Apple`. If the actual instanct is not `Apple`,
232+
* the ClassCastException will be thrown.
233+
*
234+
* @return The actual instance of `Apple`
235+
* @throws ClassCastException if the instance is not `Apple`
236+
*/
237+
public Apple getApple() throws ClassCastException {
238+
return (Apple)super.getActualInstance();
239+
}
218240

241+
/**
242+
* Get the actual instance of `Banana`. If the actual instanct is not `Banana`,
243+
* the ClassCastException will be thrown.
244+
*
245+
* @return The actual instance of `Banana`
246+
* @throws ClassCastException if the instance is not `Banana`
247+
*/
248+
public Banana getBanana() throws ClassCastException {
249+
return (Banana)super.getActualInstance();
250+
}
219251

220252
}
221253

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ public Map<String, GenericType> getSchemas() {
197197

198198
/**
199199
* Set the instance that matches the oneOf child schema, check
200-
* the instance parameter is valid against the oneOf child schemas.
200+
* the instance parameter is valid against the oneOf child schemas:
201+
* AppleReq, BananaReq
201202
*
202203
* It could be an instance of the 'oneOf' schemas.
203204
* The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf).
@@ -222,7 +223,38 @@ public void setActualInstance(Object instance) {
222223
throw new RuntimeException("Invalid instance type. Must be AppleReq, BananaReq");
223224
}
224225

226+
/**
227+
* Get the actual instance, which can be the following:
228+
* AppleReq, BananaReq
229+
*
230+
* @return The actual instance (AppleReq, BananaReq)
231+
*/
232+
@Override
233+
public Object getActualInstance() {
234+
return super.getActualInstance();
235+
}
236+
237+
/**
238+
* Get the actual instance of `AppleReq`. If the actual instanct is not `AppleReq`,
239+
* the ClassCastException will be thrown.
240+
*
241+
* @return The actual instance of `AppleReq`
242+
* @throws ClassCastException if the instance is not `AppleReq`
243+
*/
244+
public AppleReq getAppleReq() throws ClassCastException {
245+
return (AppleReq)super.getActualInstance();
246+
}
225247

248+
/**
249+
* Get the actual instance of `BananaReq`. If the actual instanct is not `BananaReq`,
250+
* the ClassCastException will be thrown.
251+
*
252+
* @return The actual instance of `BananaReq`
253+
* @throws ClassCastException if the instance is not `BananaReq`
254+
*/
255+
public BananaReq getBananaReq() throws ClassCastException {
256+
return (BananaReq)super.getActualInstance();
257+
}
226258

227259
}
228260

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ public Map<String, GenericType> getSchemas() {
310310

311311
/**
312312
* Set the instance that matches the oneOf child schema, check
313-
* the instance parameter is valid against the oneOf child schemas.
313+
* the instance parameter is valid against the oneOf child schemas:
314+
* Pig, Whale, Zebra
314315
*
315316
* It could be an instance of the 'oneOf' schemas.
316317
* The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf).
@@ -335,7 +336,49 @@ public void setActualInstance(Object instance) {
335336
throw new RuntimeException("Invalid instance type. Must be Pig, Whale, Zebra");
336337
}
337338

339+
/**
340+
* Get the actual instance, which can be the following:
341+
* Pig, Whale, Zebra
342+
*
343+
* @return The actual instance (Pig, Whale, Zebra)
344+
*/
345+
@Override
346+
public Object getActualInstance() {
347+
return super.getActualInstance();
348+
}
349+
350+
/**
351+
* Get the actual instance of `Pig`. If the actual instanct is not `Pig`,
352+
* the ClassCastException will be thrown.
353+
*
354+
* @return The actual instance of `Pig`
355+
* @throws ClassCastException if the instance is not `Pig`
356+
*/
357+
public Pig getPig() throws ClassCastException {
358+
return (Pig)super.getActualInstance();
359+
}
338360

361+
/**
362+
* Get the actual instance of `Whale`. If the actual instanct is not `Whale`,
363+
* the ClassCastException will be thrown.
364+
*
365+
* @return The actual instance of `Whale`
366+
* @throws ClassCastException if the instance is not `Whale`
367+
*/
368+
public Whale getWhale() throws ClassCastException {
369+
return (Whale)super.getActualInstance();
370+
}
371+
372+
/**
373+
* Get the actual instance of `Zebra`. If the actual instanct is not `Zebra`,
374+
* the ClassCastException will be thrown.
375+
*
376+
* @return The actual instance of `Zebra`
377+
* @throws ClassCastException if the instance is not `Zebra`
378+
*/
379+
public Zebra getZebra() throws ClassCastException {
380+
return (Zebra)super.getActualInstance();
381+
}
339382

340383
}
341384

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ public Map<String, GenericType> getSchemas() {
273273

274274
/**
275275
* Set the instance that matches the oneOf child schema, check
276-
* the instance parameter is valid against the oneOf child schemas.
276+
* the instance parameter is valid against the oneOf child schemas:
277+
* Quadrilateral, Triangle
277278
*
278279
* It could be an instance of the 'oneOf' schemas.
279280
* The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf).
@@ -298,7 +299,38 @@ public void setActualInstance(Object instance) {
298299
throw new RuntimeException("Invalid instance type. Must be Quadrilateral, Triangle");
299300
}
300301

302+
/**
303+
* Get the actual instance, which can be the following:
304+
* Quadrilateral, Triangle
305+
*
306+
* @return The actual instance (Quadrilateral, Triangle)
307+
*/
308+
@Override
309+
public Object getActualInstance() {
310+
return super.getActualInstance();
311+
}
312+
313+
/**
314+
* Get the actual instance of `Quadrilateral`. If the actual instanct is not `Quadrilateral`,
315+
* the ClassCastException will be thrown.
316+
*
317+
* @return The actual instance of `Quadrilateral`
318+
* @throws ClassCastException if the instance is not `Quadrilateral`
319+
*/
320+
public Quadrilateral getQuadrilateral() throws ClassCastException {
321+
return (Quadrilateral)super.getActualInstance();
322+
}
301323

324+
/**
325+
* Get the actual instance of `Triangle`. If the actual instanct is not `Triangle`,
326+
* the ClassCastException will be thrown.
327+
*
328+
* @return The actual instance of `Triangle`
329+
* @throws ClassCastException if the instance is not `Triangle`
330+
*/
331+
public Triangle getTriangle() throws ClassCastException {
332+
return (Triangle)super.getActualInstance();
333+
}
302334

303335
}
304336

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ public Map<String, GenericType> getSchemas() {
271271

272272
/**
273273
* Set the instance that matches the oneOf child schema, check
274-
* the instance parameter is valid against the oneOf child schemas.
274+
* the instance parameter is valid against the oneOf child schemas:
275+
* BasquePig, DanishPig
275276
*
276277
* It could be an instance of the 'oneOf' schemas.
277278
* The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf).
@@ -291,7 +292,38 @@ public void setActualInstance(Object instance) {
291292
throw new RuntimeException("Invalid instance type. Must be BasquePig, DanishPig");
292293
}
293294

295+
/**
296+
* Get the actual instance, which can be the following:
297+
* BasquePig, DanishPig
298+
*
299+
* @return The actual instance (BasquePig, DanishPig)
300+
*/
301+
@Override
302+
public Object getActualInstance() {
303+
return super.getActualInstance();
304+
}
305+
306+
/**
307+
* Get the actual instance of `BasquePig`. If the actual instanct is not `BasquePig`,
308+
* the ClassCastException will be thrown.
309+
*
310+
* @return The actual instance of `BasquePig`
311+
* @throws ClassCastException if the instance is not `BasquePig`
312+
*/
313+
public BasquePig getBasquePig() throws ClassCastException {
314+
return (BasquePig)super.getActualInstance();
315+
}
294316

317+
/**
318+
* Get the actual instance of `DanishPig`. If the actual instanct is not `DanishPig`,
319+
* the ClassCastException will be thrown.
320+
*
321+
* @return The actual instance of `DanishPig`
322+
* @throws ClassCastException if the instance is not `DanishPig`
323+
*/
324+
public DanishPig getDanishPig() throws ClassCastException {
325+
return (DanishPig)super.getActualInstance();
326+
}
295327

296328
}
297329

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ public Map<String, GenericType> getSchemas() {
271271

272272
/**
273273
* Set the instance that matches the oneOf child schema, check
274-
* the instance parameter is valid against the oneOf child schemas.
274+
* the instance parameter is valid against the oneOf child schemas:
275+
* ComplexQuadrilateral, SimpleQuadrilateral
275276
*
276277
* It could be an instance of the 'oneOf' schemas.
277278
* The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf).
@@ -291,7 +292,38 @@ public void setActualInstance(Object instance) {
291292
throw new RuntimeException("Invalid instance type. Must be ComplexQuadrilateral, SimpleQuadrilateral");
292293
}
293294

295+
/**
296+
* Get the actual instance, which can be the following:
297+
* ComplexQuadrilateral, SimpleQuadrilateral
298+
*
299+
* @return The actual instance (ComplexQuadrilateral, SimpleQuadrilateral)
300+
*/
301+
@Override
302+
public Object getActualInstance() {
303+
return super.getActualInstance();
304+
}
305+
306+
/**
307+
* Get the actual instance of `ComplexQuadrilateral`. If the actual instanct is not `ComplexQuadrilateral`,
308+
* the ClassCastException will be thrown.
309+
*
310+
* @return The actual instance of `ComplexQuadrilateral`
311+
* @throws ClassCastException if the instance is not `ComplexQuadrilateral`
312+
*/
313+
public ComplexQuadrilateral getComplexQuadrilateral() throws ClassCastException {
314+
return (ComplexQuadrilateral)super.getActualInstance();
315+
}
294316

317+
/**
318+
* Get the actual instance of `SimpleQuadrilateral`. If the actual instanct is not `SimpleQuadrilateral`,
319+
* the ClassCastException will be thrown.
320+
*
321+
* @return The actual instance of `SimpleQuadrilateral`
322+
* @throws ClassCastException if the instance is not `SimpleQuadrilateral`
323+
*/
324+
public SimpleQuadrilateral getSimpleQuadrilateral() throws ClassCastException {
325+
return (SimpleQuadrilateral)super.getActualInstance();
326+
}
295327

296328
}
297329

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ public Map<String, GenericType> getSchemas() {
271271

272272
/**
273273
* Set the instance that matches the oneOf child schema, check
274-
* the instance parameter is valid against the oneOf child schemas.
274+
* the instance parameter is valid against the oneOf child schemas:
275+
* Quadrilateral, Triangle
275276
*
276277
* It could be an instance of the 'oneOf' schemas.
277278
* The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf).
@@ -291,7 +292,38 @@ public void setActualInstance(Object instance) {
291292
throw new RuntimeException("Invalid instance type. Must be Quadrilateral, Triangle");
292293
}
293294

295+
/**
296+
* Get the actual instance, which can be the following:
297+
* Quadrilateral, Triangle
298+
*
299+
* @return The actual instance (Quadrilateral, Triangle)
300+
*/
301+
@Override
302+
public Object getActualInstance() {
303+
return super.getActualInstance();
304+
}
305+
306+
/**
307+
* Get the actual instance of `Quadrilateral`. If the actual instanct is not `Quadrilateral`,
308+
* the ClassCastException will be thrown.
309+
*
310+
* @return The actual instance of `Quadrilateral`
311+
* @throws ClassCastException if the instance is not `Quadrilateral`
312+
*/
313+
public Quadrilateral getQuadrilateral() throws ClassCastException {
314+
return (Quadrilateral)super.getActualInstance();
315+
}
294316

317+
/**
318+
* Get the actual instance of `Triangle`. If the actual instanct is not `Triangle`,
319+
* the ClassCastException will be thrown.
320+
*
321+
* @return The actual instance of `Triangle`
322+
* @throws ClassCastException if the instance is not `Triangle`
323+
*/
324+
public Triangle getTriangle() throws ClassCastException {
325+
return (Triangle)super.getActualInstance();
326+
}
295327

296328
}
297329

0 commit comments

Comments
 (0)