[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:
William Cheng 2021-12-07 15:11:06 +08:00 committed by GitHub
parent 95377f68bf
commit 000a18d3b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 67 additions and 0 deletions

View File

@ -50,6 +50,11 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
return (TypeAdapter<T>) new TypeAdapter<{{classname}}>() {
@Override
public void write(JsonWriter out, {{classname}} value) throws IOException {
if (value == null || value.getActualInstance() == null) {
elementAdapter.write(out, null);
return;
}
{{#anyOf}}
// check if the actual instance is of the type `{{.}}`
if (value.getActualInstance() instanceof {{.}}) {

View File

@ -50,6 +50,11 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
return (TypeAdapter<T>) new TypeAdapter<{{classname}}>() {
@Override
public void write(JsonWriter out, {{classname}} value) throws IOException {
if (value == null || value.getActualInstance() == null) {
elementAdapter.write(out, null);
return;
}
{{#oneOf}}
// check if the actual instance is of the type `{{.}}`
if (value.getActualInstance() instanceof {{.}}) {

View File

@ -78,6 +78,11 @@ public class Fruit extends AbstractOpenApiSchema {
return (TypeAdapter<T>) new TypeAdapter<Fruit>() {
@Override
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`
if (value.getActualInstance() instanceof Apple) {
JsonObject obj = adapterApple.toJsonTree((Apple)value.getActualInstance()).getAsJsonObject();

View File

@ -78,6 +78,11 @@ public class FruitReq extends AbstractOpenApiSchema {
return (TypeAdapter<T>) new TypeAdapter<FruitReq>() {
@Override
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`
if (value.getActualInstance() instanceof AppleReq) {
JsonObject obj = adapterAppleReq.toJsonTree((AppleReq)value.getActualInstance()).getAsJsonObject();

View File

@ -78,6 +78,11 @@ public class GmFruit extends AbstractOpenApiSchema {
return (TypeAdapter<T>) new TypeAdapter<GmFruit>() {
@Override
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`
if (value.getActualInstance() instanceof Apple) {
JsonObject obj = adapterApple.toJsonTree((Apple)value.getActualInstance()).getAsJsonObject();

View File

@ -79,6 +79,11 @@ public class Mammal extends AbstractOpenApiSchema {
return (TypeAdapter<T>) new TypeAdapter<Mammal>() {
@Override
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`
if (value.getActualInstance() instanceof Pig) {
JsonObject obj = adapterPig.toJsonTree((Pig)value.getActualInstance()).getAsJsonObject();

View File

@ -77,6 +77,11 @@ public class NullableShape extends AbstractOpenApiSchema {
return (TypeAdapter<T>) new TypeAdapter<NullableShape>() {
@Override
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`
if (value.getActualInstance() instanceof Quadrilateral) {
JsonObject obj = adapterQuadrilateral.toJsonTree((Quadrilateral)value.getActualInstance()).getAsJsonObject();

View File

@ -77,6 +77,11 @@ public class Pig extends AbstractOpenApiSchema {
return (TypeAdapter<T>) new TypeAdapter<Pig>() {
@Override
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`
if (value.getActualInstance() instanceof BasquePig) {
JsonObject obj = adapterBasquePig.toJsonTree((BasquePig)value.getActualInstance()).getAsJsonObject();

View File

@ -77,6 +77,11 @@ public class Quadrilateral extends AbstractOpenApiSchema {
return (TypeAdapter<T>) new TypeAdapter<Quadrilateral>() {
@Override
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`
if (value.getActualInstance() instanceof ComplexQuadrilateral) {
JsonObject obj = adapterComplexQuadrilateral.toJsonTree((ComplexQuadrilateral)value.getActualInstance()).getAsJsonObject();

View File

@ -77,6 +77,11 @@ public class Shape extends AbstractOpenApiSchema {
return (TypeAdapter<T>) new TypeAdapter<Shape>() {
@Override
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`
if (value.getActualInstance() instanceof Quadrilateral) {
JsonObject obj = adapterQuadrilateral.toJsonTree((Quadrilateral)value.getActualInstance()).getAsJsonObject();

View File

@ -77,6 +77,11 @@ public class ShapeOrNull extends AbstractOpenApiSchema {
return (TypeAdapter<T>) new TypeAdapter<ShapeOrNull>() {
@Override
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`
if (value.getActualInstance() instanceof Quadrilateral) {
JsonObject obj = adapterQuadrilateral.toJsonTree((Quadrilateral)value.getActualInstance()).getAsJsonObject();

View File

@ -79,6 +79,11 @@ public class Triangle extends AbstractOpenApiSchema {
return (TypeAdapter<T>) new TypeAdapter<Triangle>() {
@Override
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`
if (value.getActualInstance() instanceof EquilateralTriangle) {
JsonObject obj = adapterEquilateralTriangle.toJsonTree((EquilateralTriangle)value.getActualInstance()).getAsJsonObject();

View File

@ -314,6 +314,13 @@ public class JSONTest {
assertEquals(inst2.getMealy(), 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.
// Since FruitReq has additionalProperties: false, deserialization should fail.