forked from loafle/openapi-generator-original
[java][okhttp-gson-nextgen] Better null handling in oneOf, anyOf model (#11056)
* better null handling in oneOf model * update anyof with better null handling * add null test
This commit is contained in:
parent
95377f68bf
commit
000a18d3b9
@ -50,6 +50,11 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
|
|||||||
return (TypeAdapter<T>) new TypeAdapter<{{classname}}>() {
|
return (TypeAdapter<T>) new TypeAdapter<{{classname}}>() {
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, {{classname}} value) throws IOException {
|
public void write(JsonWriter out, {{classname}} value) throws IOException {
|
||||||
|
if (value == null || value.getActualInstance() == null) {
|
||||||
|
elementAdapter.write(out, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
{{#anyOf}}
|
{{#anyOf}}
|
||||||
// check if the actual instance is of the type `{{.}}`
|
// check if the actual instance is of the type `{{.}}`
|
||||||
if (value.getActualInstance() instanceof {{.}}) {
|
if (value.getActualInstance() instanceof {{.}}) {
|
||||||
|
@ -50,6 +50,11 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
|
|||||||
return (TypeAdapter<T>) new TypeAdapter<{{classname}}>() {
|
return (TypeAdapter<T>) new TypeAdapter<{{classname}}>() {
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, {{classname}} value) throws IOException {
|
public void write(JsonWriter out, {{classname}} value) throws IOException {
|
||||||
|
if (value == null || value.getActualInstance() == null) {
|
||||||
|
elementAdapter.write(out, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
{{#oneOf}}
|
{{#oneOf}}
|
||||||
// check if the actual instance is of the type `{{.}}`
|
// check if the actual instance is of the type `{{.}}`
|
||||||
if (value.getActualInstance() instanceof {{.}}) {
|
if (value.getActualInstance() instanceof {{.}}) {
|
||||||
|
@ -78,6 +78,11 @@ public class Fruit extends AbstractOpenApiSchema {
|
|||||||
return (TypeAdapter<T>) new TypeAdapter<Fruit>() {
|
return (TypeAdapter<T>) new TypeAdapter<Fruit>() {
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, Fruit value) throws IOException {
|
public void write(JsonWriter out, Fruit value) throws IOException {
|
||||||
|
if (value == null || value.getActualInstance() == null) {
|
||||||
|
elementAdapter.write(out, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// check if the actual instance is of the type `Apple`
|
// check if the actual instance is of the type `Apple`
|
||||||
if (value.getActualInstance() instanceof Apple) {
|
if (value.getActualInstance() instanceof Apple) {
|
||||||
JsonObject obj = adapterApple.toJsonTree((Apple)value.getActualInstance()).getAsJsonObject();
|
JsonObject obj = adapterApple.toJsonTree((Apple)value.getActualInstance()).getAsJsonObject();
|
||||||
|
@ -78,6 +78,11 @@ public class FruitReq extends AbstractOpenApiSchema {
|
|||||||
return (TypeAdapter<T>) new TypeAdapter<FruitReq>() {
|
return (TypeAdapter<T>) new TypeAdapter<FruitReq>() {
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, FruitReq value) throws IOException {
|
public void write(JsonWriter out, FruitReq value) throws IOException {
|
||||||
|
if (value == null || value.getActualInstance() == null) {
|
||||||
|
elementAdapter.write(out, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// check if the actual instance is of the type `AppleReq`
|
// check if the actual instance is of the type `AppleReq`
|
||||||
if (value.getActualInstance() instanceof AppleReq) {
|
if (value.getActualInstance() instanceof AppleReq) {
|
||||||
JsonObject obj = adapterAppleReq.toJsonTree((AppleReq)value.getActualInstance()).getAsJsonObject();
|
JsonObject obj = adapterAppleReq.toJsonTree((AppleReq)value.getActualInstance()).getAsJsonObject();
|
||||||
|
@ -78,6 +78,11 @@ public class GmFruit extends AbstractOpenApiSchema {
|
|||||||
return (TypeAdapter<T>) new TypeAdapter<GmFruit>() {
|
return (TypeAdapter<T>) new TypeAdapter<GmFruit>() {
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, GmFruit value) throws IOException {
|
public void write(JsonWriter out, GmFruit value) throws IOException {
|
||||||
|
if (value == null || value.getActualInstance() == null) {
|
||||||
|
elementAdapter.write(out, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// check if the actual instance is of the type `Apple`
|
// check if the actual instance is of the type `Apple`
|
||||||
if (value.getActualInstance() instanceof Apple) {
|
if (value.getActualInstance() instanceof Apple) {
|
||||||
JsonObject obj = adapterApple.toJsonTree((Apple)value.getActualInstance()).getAsJsonObject();
|
JsonObject obj = adapterApple.toJsonTree((Apple)value.getActualInstance()).getAsJsonObject();
|
||||||
|
@ -79,6 +79,11 @@ public class Mammal extends AbstractOpenApiSchema {
|
|||||||
return (TypeAdapter<T>) new TypeAdapter<Mammal>() {
|
return (TypeAdapter<T>) new TypeAdapter<Mammal>() {
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, Mammal value) throws IOException {
|
public void write(JsonWriter out, Mammal value) throws IOException {
|
||||||
|
if (value == null || value.getActualInstance() == null) {
|
||||||
|
elementAdapter.write(out, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// check if the actual instance is of the type `Pig`
|
// check if the actual instance is of the type `Pig`
|
||||||
if (value.getActualInstance() instanceof Pig) {
|
if (value.getActualInstance() instanceof Pig) {
|
||||||
JsonObject obj = adapterPig.toJsonTree((Pig)value.getActualInstance()).getAsJsonObject();
|
JsonObject obj = adapterPig.toJsonTree((Pig)value.getActualInstance()).getAsJsonObject();
|
||||||
|
@ -77,6 +77,11 @@ public class NullableShape extends AbstractOpenApiSchema {
|
|||||||
return (TypeAdapter<T>) new TypeAdapter<NullableShape>() {
|
return (TypeAdapter<T>) new TypeAdapter<NullableShape>() {
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, NullableShape value) throws IOException {
|
public void write(JsonWriter out, NullableShape value) throws IOException {
|
||||||
|
if (value == null || value.getActualInstance() == null) {
|
||||||
|
elementAdapter.write(out, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// check if the actual instance is of the type `Quadrilateral`
|
// check if the actual instance is of the type `Quadrilateral`
|
||||||
if (value.getActualInstance() instanceof Quadrilateral) {
|
if (value.getActualInstance() instanceof Quadrilateral) {
|
||||||
JsonObject obj = adapterQuadrilateral.toJsonTree((Quadrilateral)value.getActualInstance()).getAsJsonObject();
|
JsonObject obj = adapterQuadrilateral.toJsonTree((Quadrilateral)value.getActualInstance()).getAsJsonObject();
|
||||||
|
@ -77,6 +77,11 @@ public class Pig extends AbstractOpenApiSchema {
|
|||||||
return (TypeAdapter<T>) new TypeAdapter<Pig>() {
|
return (TypeAdapter<T>) new TypeAdapter<Pig>() {
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, Pig value) throws IOException {
|
public void write(JsonWriter out, Pig value) throws IOException {
|
||||||
|
if (value == null || value.getActualInstance() == null) {
|
||||||
|
elementAdapter.write(out, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// check if the actual instance is of the type `BasquePig`
|
// check if the actual instance is of the type `BasquePig`
|
||||||
if (value.getActualInstance() instanceof BasquePig) {
|
if (value.getActualInstance() instanceof BasquePig) {
|
||||||
JsonObject obj = adapterBasquePig.toJsonTree((BasquePig)value.getActualInstance()).getAsJsonObject();
|
JsonObject obj = adapterBasquePig.toJsonTree((BasquePig)value.getActualInstance()).getAsJsonObject();
|
||||||
|
@ -77,6 +77,11 @@ public class Quadrilateral extends AbstractOpenApiSchema {
|
|||||||
return (TypeAdapter<T>) new TypeAdapter<Quadrilateral>() {
|
return (TypeAdapter<T>) new TypeAdapter<Quadrilateral>() {
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, Quadrilateral value) throws IOException {
|
public void write(JsonWriter out, Quadrilateral value) throws IOException {
|
||||||
|
if (value == null || value.getActualInstance() == null) {
|
||||||
|
elementAdapter.write(out, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// check if the actual instance is of the type `ComplexQuadrilateral`
|
// check if the actual instance is of the type `ComplexQuadrilateral`
|
||||||
if (value.getActualInstance() instanceof ComplexQuadrilateral) {
|
if (value.getActualInstance() instanceof ComplexQuadrilateral) {
|
||||||
JsonObject obj = adapterComplexQuadrilateral.toJsonTree((ComplexQuadrilateral)value.getActualInstance()).getAsJsonObject();
|
JsonObject obj = adapterComplexQuadrilateral.toJsonTree((ComplexQuadrilateral)value.getActualInstance()).getAsJsonObject();
|
||||||
|
@ -77,6 +77,11 @@ public class Shape extends AbstractOpenApiSchema {
|
|||||||
return (TypeAdapter<T>) new TypeAdapter<Shape>() {
|
return (TypeAdapter<T>) new TypeAdapter<Shape>() {
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, Shape value) throws IOException {
|
public void write(JsonWriter out, Shape value) throws IOException {
|
||||||
|
if (value == null || value.getActualInstance() == null) {
|
||||||
|
elementAdapter.write(out, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// check if the actual instance is of the type `Quadrilateral`
|
// check if the actual instance is of the type `Quadrilateral`
|
||||||
if (value.getActualInstance() instanceof Quadrilateral) {
|
if (value.getActualInstance() instanceof Quadrilateral) {
|
||||||
JsonObject obj = adapterQuadrilateral.toJsonTree((Quadrilateral)value.getActualInstance()).getAsJsonObject();
|
JsonObject obj = adapterQuadrilateral.toJsonTree((Quadrilateral)value.getActualInstance()).getAsJsonObject();
|
||||||
|
@ -77,6 +77,11 @@ public class ShapeOrNull extends AbstractOpenApiSchema {
|
|||||||
return (TypeAdapter<T>) new TypeAdapter<ShapeOrNull>() {
|
return (TypeAdapter<T>) new TypeAdapter<ShapeOrNull>() {
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, ShapeOrNull value) throws IOException {
|
public void write(JsonWriter out, ShapeOrNull value) throws IOException {
|
||||||
|
if (value == null || value.getActualInstance() == null) {
|
||||||
|
elementAdapter.write(out, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// check if the actual instance is of the type `Quadrilateral`
|
// check if the actual instance is of the type `Quadrilateral`
|
||||||
if (value.getActualInstance() instanceof Quadrilateral) {
|
if (value.getActualInstance() instanceof Quadrilateral) {
|
||||||
JsonObject obj = adapterQuadrilateral.toJsonTree((Quadrilateral)value.getActualInstance()).getAsJsonObject();
|
JsonObject obj = adapterQuadrilateral.toJsonTree((Quadrilateral)value.getActualInstance()).getAsJsonObject();
|
||||||
|
@ -79,6 +79,11 @@ public class Triangle extends AbstractOpenApiSchema {
|
|||||||
return (TypeAdapter<T>) new TypeAdapter<Triangle>() {
|
return (TypeAdapter<T>) new TypeAdapter<Triangle>() {
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, Triangle value) throws IOException {
|
public void write(JsonWriter out, Triangle value) throws IOException {
|
||||||
|
if (value == null || value.getActualInstance() == null) {
|
||||||
|
elementAdapter.write(out, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// check if the actual instance is of the type `EquilateralTriangle`
|
// check if the actual instance is of the type `EquilateralTriangle`
|
||||||
if (value.getActualInstance() instanceof EquilateralTriangle) {
|
if (value.getActualInstance() instanceof EquilateralTriangle) {
|
||||||
JsonObject obj = adapterEquilateralTriangle.toJsonTree((EquilateralTriangle)value.getActualInstance()).getAsJsonObject();
|
JsonObject obj = adapterEquilateralTriangle.toJsonTree((EquilateralTriangle)value.getActualInstance()).getAsJsonObject();
|
||||||
|
@ -314,6 +314,13 @@ public class JSONTest {
|
|||||||
assertEquals(inst2.getMealy(), false);
|
assertEquals(inst2.getMealy(), false);
|
||||||
assertEquals(json.getGson().toJson(inst2), "{\"cultivar\":\"golden delicious\",\"mealy\":false}");
|
assertEquals(json.getGson().toJson(inst2), "{\"cultivar\":\"golden delicious\",\"mealy\":false}");
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
// test to ensure the oneOf object can be serialized to "null" correctly
|
||||||
|
FruitReq o = new FruitReq();
|
||||||
|
assertEquals(o.getActualInstance(), null);
|
||||||
|
assertEquals(json.getGson().toJson(o), "null");
|
||||||
|
assertEquals(json.getGson().toJson(null), "null");
|
||||||
|
}
|
||||||
{
|
{
|
||||||
// Same test, but this time with additional (undeclared) properties.
|
// Same test, but this time with additional (undeclared) properties.
|
||||||
// Since FruitReq has additionalProperties: false, deserialization should fail.
|
// Since FruitReq has additionalProperties: false, deserialization should fail.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user