[Java][jersey2] Add helper methods for oneOf Java classes (#7115)

* add helper methods for oneOf java class

* better test comment
This commit is contained in:
William Cheng 2020-08-05 10:27:38 +08:00 committed by GitHub
parent aa698633b3
commit c1b8c294aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 359 additions and 11 deletions

View File

@ -183,7 +183,8 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
/** /**
* Set the instance that matches the oneOf child schema, check * Set the instance that matches the oneOf child schema, check
* the instance parameter is valid against the oneOf child schemas. * the instance parameter is valid against the oneOf child schemas:
* {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}
* *
* It could be an instance of the 'oneOf' schemas. * It could be an instance of the 'oneOf' schemas.
* The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf). * 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
throw new RuntimeException("Invalid instance type. Must be {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}"); throw new RuntimeException("Invalid instance type. Must be {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}");
} }
/**
* Get the actual instance, which can be the following:
* {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}
*
* @return The actual instance ({{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}})
*/
@Override
public Object getActualInstance() {
return super.getActualInstance();
}
{{#oneOf}}
/**
* Get the actual instance of `{{{.}}}`. If the actual instanct is not `{{{.}}}`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `{{{.}}}`
* @throws ClassCastException if the instance is not `{{{.}}}`
*/
public {{{.}}} get{{{.}}}() throws ClassCastException {
return ({{{.}}})super.getActualInstance();
}
{{/oneOf}}
} }

View File

@ -195,7 +195,8 @@ public class Fruit extends AbstractOpenApiSchema {
/** /**
* Set the instance that matches the oneOf child schema, check * Set the instance that matches the oneOf child schema, check
* the instance parameter is valid against the oneOf child schemas. * the instance parameter is valid against the oneOf child schemas:
* Apple, Banana
* *
* It could be an instance of the 'oneOf' schemas. * It could be an instance of the 'oneOf' schemas.
* The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf). * The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf).
@ -215,7 +216,38 @@ public class Fruit extends AbstractOpenApiSchema {
throw new RuntimeException("Invalid instance type. Must be Apple, Banana"); throw new RuntimeException("Invalid instance type. Must be Apple, Banana");
} }
/**
* Get the actual instance, which can be the following:
* Apple, Banana
*
* @return The actual instance (Apple, Banana)
*/
@Override
public Object getActualInstance() {
return super.getActualInstance();
}
/**
* Get the actual instance of `Apple`. If the actual instanct is not `Apple`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `Apple`
* @throws ClassCastException if the instance is not `Apple`
*/
public Apple getApple() throws ClassCastException {
return (Apple)super.getActualInstance();
}
/**
* Get the actual instance of `Banana`. If the actual instanct is not `Banana`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `Banana`
* @throws ClassCastException if the instance is not `Banana`
*/
public Banana getBanana() throws ClassCastException {
return (Banana)super.getActualInstance();
}
} }

View File

@ -197,7 +197,8 @@ public class FruitReq extends AbstractOpenApiSchema {
/** /**
* Set the instance that matches the oneOf child schema, check * Set the instance that matches the oneOf child schema, check
* the instance parameter is valid against the oneOf child schemas. * the instance parameter is valid against the oneOf child schemas:
* AppleReq, BananaReq
* *
* It could be an instance of the 'oneOf' schemas. * It could be an instance of the 'oneOf' schemas.
* The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf). * The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf).
@ -222,7 +223,38 @@ public class FruitReq extends AbstractOpenApiSchema {
throw new RuntimeException("Invalid instance type. Must be AppleReq, BananaReq"); throw new RuntimeException("Invalid instance type. Must be AppleReq, BananaReq");
} }
/**
* Get the actual instance, which can be the following:
* AppleReq, BananaReq
*
* @return The actual instance (AppleReq, BananaReq)
*/
@Override
public Object getActualInstance() {
return super.getActualInstance();
}
/**
* Get the actual instance of `AppleReq`. If the actual instanct is not `AppleReq`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `AppleReq`
* @throws ClassCastException if the instance is not `AppleReq`
*/
public AppleReq getAppleReq() throws ClassCastException {
return (AppleReq)super.getActualInstance();
}
/**
* Get the actual instance of `BananaReq`. If the actual instanct is not `BananaReq`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `BananaReq`
* @throws ClassCastException if the instance is not `BananaReq`
*/
public BananaReq getBananaReq() throws ClassCastException {
return (BananaReq)super.getActualInstance();
}
} }

View File

@ -310,7 +310,8 @@ public class Mammal extends AbstractOpenApiSchema {
/** /**
* Set the instance that matches the oneOf child schema, check * Set the instance that matches the oneOf child schema, check
* the instance parameter is valid against the oneOf child schemas. * the instance parameter is valid against the oneOf child schemas:
* Pig, Whale, Zebra
* *
* It could be an instance of the 'oneOf' schemas. * It could be an instance of the 'oneOf' schemas.
* The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf). * The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf).
@ -335,7 +336,49 @@ public class Mammal extends AbstractOpenApiSchema {
throw new RuntimeException("Invalid instance type. Must be Pig, Whale, Zebra"); throw new RuntimeException("Invalid instance type. Must be Pig, Whale, Zebra");
} }
/**
* Get the actual instance, which can be the following:
* Pig, Whale, Zebra
*
* @return The actual instance (Pig, Whale, Zebra)
*/
@Override
public Object getActualInstance() {
return super.getActualInstance();
}
/**
* Get the actual instance of `Pig`. If the actual instanct is not `Pig`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `Pig`
* @throws ClassCastException if the instance is not `Pig`
*/
public Pig getPig() throws ClassCastException {
return (Pig)super.getActualInstance();
}
/**
* Get the actual instance of `Whale`. If the actual instanct is not `Whale`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `Whale`
* @throws ClassCastException if the instance is not `Whale`
*/
public Whale getWhale() throws ClassCastException {
return (Whale)super.getActualInstance();
}
/**
* Get the actual instance of `Zebra`. If the actual instanct is not `Zebra`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `Zebra`
* @throws ClassCastException if the instance is not `Zebra`
*/
public Zebra getZebra() throws ClassCastException {
return (Zebra)super.getActualInstance();
}
} }

View File

@ -273,7 +273,8 @@ public class NullableShape extends AbstractOpenApiSchema {
/** /**
* Set the instance that matches the oneOf child schema, check * Set the instance that matches the oneOf child schema, check
* the instance parameter is valid against the oneOf child schemas. * the instance parameter is valid against the oneOf child schemas:
* Quadrilateral, Triangle
* *
* It could be an instance of the 'oneOf' schemas. * It could be an instance of the 'oneOf' schemas.
* The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf). * The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf).
@ -298,7 +299,38 @@ public class NullableShape extends AbstractOpenApiSchema {
throw new RuntimeException("Invalid instance type. Must be Quadrilateral, Triangle"); throw new RuntimeException("Invalid instance type. Must be Quadrilateral, Triangle");
} }
/**
* Get the actual instance, which can be the following:
* Quadrilateral, Triangle
*
* @return The actual instance (Quadrilateral, Triangle)
*/
@Override
public Object getActualInstance() {
return super.getActualInstance();
}
/**
* Get the actual instance of `Quadrilateral`. If the actual instanct is not `Quadrilateral`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `Quadrilateral`
* @throws ClassCastException if the instance is not `Quadrilateral`
*/
public Quadrilateral getQuadrilateral() throws ClassCastException {
return (Quadrilateral)super.getActualInstance();
}
/**
* Get the actual instance of `Triangle`. If the actual instanct is not `Triangle`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `Triangle`
* @throws ClassCastException if the instance is not `Triangle`
*/
public Triangle getTriangle() throws ClassCastException {
return (Triangle)super.getActualInstance();
}
} }

View File

@ -271,7 +271,8 @@ public class Pig extends AbstractOpenApiSchema {
/** /**
* Set the instance that matches the oneOf child schema, check * Set the instance that matches the oneOf child schema, check
* the instance parameter is valid against the oneOf child schemas. * the instance parameter is valid against the oneOf child schemas:
* BasquePig, DanishPig
* *
* It could be an instance of the 'oneOf' schemas. * It could be an instance of the 'oneOf' schemas.
* The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf). * The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf).
@ -291,7 +292,38 @@ public class Pig extends AbstractOpenApiSchema {
throw new RuntimeException("Invalid instance type. Must be BasquePig, DanishPig"); throw new RuntimeException("Invalid instance type. Must be BasquePig, DanishPig");
} }
/**
* Get the actual instance, which can be the following:
* BasquePig, DanishPig
*
* @return The actual instance (BasquePig, DanishPig)
*/
@Override
public Object getActualInstance() {
return super.getActualInstance();
}
/**
* Get the actual instance of `BasquePig`. If the actual instanct is not `BasquePig`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `BasquePig`
* @throws ClassCastException if the instance is not `BasquePig`
*/
public BasquePig getBasquePig() throws ClassCastException {
return (BasquePig)super.getActualInstance();
}
/**
* Get the actual instance of `DanishPig`. If the actual instanct is not `DanishPig`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `DanishPig`
* @throws ClassCastException if the instance is not `DanishPig`
*/
public DanishPig getDanishPig() throws ClassCastException {
return (DanishPig)super.getActualInstance();
}
} }

View File

@ -271,7 +271,8 @@ public class Quadrilateral extends AbstractOpenApiSchema {
/** /**
* Set the instance that matches the oneOf child schema, check * Set the instance that matches the oneOf child schema, check
* the instance parameter is valid against the oneOf child schemas. * the instance parameter is valid against the oneOf child schemas:
* ComplexQuadrilateral, SimpleQuadrilateral
* *
* It could be an instance of the 'oneOf' schemas. * It could be an instance of the 'oneOf' schemas.
* The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf). * The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf).
@ -291,7 +292,38 @@ public class Quadrilateral extends AbstractOpenApiSchema {
throw new RuntimeException("Invalid instance type. Must be ComplexQuadrilateral, SimpleQuadrilateral"); throw new RuntimeException("Invalid instance type. Must be ComplexQuadrilateral, SimpleQuadrilateral");
} }
/**
* Get the actual instance, which can be the following:
* ComplexQuadrilateral, SimpleQuadrilateral
*
* @return The actual instance (ComplexQuadrilateral, SimpleQuadrilateral)
*/
@Override
public Object getActualInstance() {
return super.getActualInstance();
}
/**
* Get the actual instance of `ComplexQuadrilateral`. If the actual instanct is not `ComplexQuadrilateral`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `ComplexQuadrilateral`
* @throws ClassCastException if the instance is not `ComplexQuadrilateral`
*/
public ComplexQuadrilateral getComplexQuadrilateral() throws ClassCastException {
return (ComplexQuadrilateral)super.getActualInstance();
}
/**
* Get the actual instance of `SimpleQuadrilateral`. If the actual instanct is not `SimpleQuadrilateral`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `SimpleQuadrilateral`
* @throws ClassCastException if the instance is not `SimpleQuadrilateral`
*/
public SimpleQuadrilateral getSimpleQuadrilateral() throws ClassCastException {
return (SimpleQuadrilateral)super.getActualInstance();
}
} }

View File

@ -271,7 +271,8 @@ public class Shape extends AbstractOpenApiSchema {
/** /**
* Set the instance that matches the oneOf child schema, check * Set the instance that matches the oneOf child schema, check
* the instance parameter is valid against the oneOf child schemas. * the instance parameter is valid against the oneOf child schemas:
* Quadrilateral, Triangle
* *
* It could be an instance of the 'oneOf' schemas. * It could be an instance of the 'oneOf' schemas.
* The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf). * The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf).
@ -291,7 +292,38 @@ public class Shape extends AbstractOpenApiSchema {
throw new RuntimeException("Invalid instance type. Must be Quadrilateral, Triangle"); throw new RuntimeException("Invalid instance type. Must be Quadrilateral, Triangle");
} }
/**
* Get the actual instance, which can be the following:
* Quadrilateral, Triangle
*
* @return The actual instance (Quadrilateral, Triangle)
*/
@Override
public Object getActualInstance() {
return super.getActualInstance();
}
/**
* Get the actual instance of `Quadrilateral`. If the actual instanct is not `Quadrilateral`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `Quadrilateral`
* @throws ClassCastException if the instance is not `Quadrilateral`
*/
public Quadrilateral getQuadrilateral() throws ClassCastException {
return (Quadrilateral)super.getActualInstance();
}
/**
* Get the actual instance of `Triangle`. If the actual instanct is not `Triangle`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `Triangle`
* @throws ClassCastException if the instance is not `Triangle`
*/
public Triangle getTriangle() throws ClassCastException {
return (Triangle)super.getActualInstance();
}
} }

View File

@ -273,7 +273,8 @@ public class ShapeOrNull extends AbstractOpenApiSchema {
/** /**
* Set the instance that matches the oneOf child schema, check * Set the instance that matches the oneOf child schema, check
* the instance parameter is valid against the oneOf child schemas. * the instance parameter is valid against the oneOf child schemas:
* Quadrilateral, Triangle
* *
* It could be an instance of the 'oneOf' schemas. * It could be an instance of the 'oneOf' schemas.
* The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf). * The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf).
@ -298,7 +299,38 @@ public class ShapeOrNull extends AbstractOpenApiSchema {
throw new RuntimeException("Invalid instance type. Must be Quadrilateral, Triangle"); throw new RuntimeException("Invalid instance type. Must be Quadrilateral, Triangle");
} }
/**
* Get the actual instance, which can be the following:
* Quadrilateral, Triangle
*
* @return The actual instance (Quadrilateral, Triangle)
*/
@Override
public Object getActualInstance() {
return super.getActualInstance();
}
/**
* Get the actual instance of `Quadrilateral`. If the actual instanct is not `Quadrilateral`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `Quadrilateral`
* @throws ClassCastException if the instance is not `Quadrilateral`
*/
public Quadrilateral getQuadrilateral() throws ClassCastException {
return (Quadrilateral)super.getActualInstance();
}
/**
* Get the actual instance of `Triangle`. If the actual instanct is not `Triangle`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `Triangle`
* @throws ClassCastException if the instance is not `Triangle`
*/
public Triangle getTriangle() throws ClassCastException {
return (Triangle)super.getActualInstance();
}
} }

View File

@ -310,7 +310,8 @@ public class Triangle extends AbstractOpenApiSchema {
/** /**
* Set the instance that matches the oneOf child schema, check * Set the instance that matches the oneOf child schema, check
* the instance parameter is valid against the oneOf child schemas. * the instance parameter is valid against the oneOf child schemas:
* EquilateralTriangle, IsoscelesTriangle, ScaleneTriangle
* *
* It could be an instance of the 'oneOf' schemas. * It could be an instance of the 'oneOf' schemas.
* The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf). * The oneOf child schemas may themselves be a composed schema (allOf, anyOf, oneOf).
@ -335,7 +336,49 @@ public class Triangle extends AbstractOpenApiSchema {
throw new RuntimeException("Invalid instance type. Must be EquilateralTriangle, IsoscelesTriangle, ScaleneTriangle"); throw new RuntimeException("Invalid instance type. Must be EquilateralTriangle, IsoscelesTriangle, ScaleneTriangle");
} }
/**
* Get the actual instance, which can be the following:
* EquilateralTriangle, IsoscelesTriangle, ScaleneTriangle
*
* @return The actual instance (EquilateralTriangle, IsoscelesTriangle, ScaleneTriangle)
*/
@Override
public Object getActualInstance() {
return super.getActualInstance();
}
/**
* Get the actual instance of `EquilateralTriangle`. If the actual instanct is not `EquilateralTriangle`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `EquilateralTriangle`
* @throws ClassCastException if the instance is not `EquilateralTriangle`
*/
public EquilateralTriangle getEquilateralTriangle() throws ClassCastException {
return (EquilateralTriangle)super.getActualInstance();
}
/**
* Get the actual instance of `IsoscelesTriangle`. If the actual instanct is not `IsoscelesTriangle`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `IsoscelesTriangle`
* @throws ClassCastException if the instance is not `IsoscelesTriangle`
*/
public IsoscelesTriangle getIsoscelesTriangle() throws ClassCastException {
return (IsoscelesTriangle)super.getActualInstance();
}
/**
* Get the actual instance of `ScaleneTriangle`. If the actual instanct is not `ScaleneTriangle`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `ScaleneTriangle`
* @throws ClassCastException if the instance is not `ScaleneTriangle`
*/
public ScaleneTriangle getScaleneTriangle() throws ClassCastException {
return (ScaleneTriangle)super.getActualInstance();
}
} }

View File

@ -66,6 +66,17 @@ public class JSONComposedSchemaTest {
} }
} }
/**
* Test to ensure the getter will throw ClassCastException
*/
@Test(expected = ClassCastException.class)
public void testCastException() throws Exception {
String str = "{ \"cultivar\": \"golden delicious\", \"mealy\": false }";
FruitReq o = json.getContext(null).readValue(str, FruitReq.class);
assertTrue(o.getActualInstance() instanceof AppleReq);
BananaReq inst2 = o.getBananaReq(); // should throw ClassCastException
}
/** /**
* Validate a oneOf schema can be deserialized into the expected class. * Validate a oneOf schema can be deserialized into the expected class.
* The oneOf schema does not have a discriminator. * The oneOf schema does not have a discriminator.
@ -81,6 +92,10 @@ public class JSONComposedSchemaTest {
AppleReq inst = (AppleReq) o.getActualInstance(); AppleReq inst = (AppleReq) o.getActualInstance();
assertEquals(inst.getCultivar(), "golden delicious"); assertEquals(inst.getCultivar(), "golden delicious");
assertEquals(inst.getMealy(), false); assertEquals(inst.getMealy(), false);
AppleReq inst2 = o.getAppleReq();
assertEquals(inst2.getCultivar(), "golden delicious");
assertEquals(inst2.getMealy(), false);
} }
{ {
// Same test, but this time with additional (undeclared) properties. // Same test, but this time with additional (undeclared) properties.
@ -116,7 +131,7 @@ public class JSONComposedSchemaTest {
String str = "null"; String str = "null";
FruitReq o = json.getContext(null).readValue(str, FruitReq.class); FruitReq o = json.getContext(null).readValue(str, FruitReq.class);
assertNull(o); assertNull(o);
} }
} }
/** /**