Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Inheritance with use of anyOf and no discriminator #12607

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2688,6 +2688,9 @@ protected void updateModelForAnyType(CodegenModel m, Schema schema) {
addAdditionPropertiesToCodeGenModel(m, schema);
m.isMap = true;
}
if (m.parent == null && !Objects.equals(m.dataType, "Object")) {
m.parent = m.dataType;
}
if (schema.getProperties() != null || schema.getRequired() != null && !(schema instanceof ComposedSchema)) {
// passing null to allProperties and allRequired as there's no parent
addVars(m, unaliasPropertySchema(schema.getProperties()), schema.getRequired(), null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -984,19 +984,19 @@ public void testAllOfSingleAndDoubleRefWithOwnPropsNoDiscriminator() {
// to test allOf with double refs
Schema supermanSchema = openAPI.getComponents().getSchemas().get("SuperMan");
CodegenModel supermanModel = codegen.fromModel("SuperMan", supermanSchema);
Assert.assertNull(supermanModel.parent);
Assert.assertEquals(supermanModel.parent, "Human");
Assert.assertEquals(supermanModel.allParents, null);

// to test allOf with single ref
Schema superboySchema = openAPI.getComponents().getSchemas().get("SuperBoy");
CodegenModel superboyModel = codegen.fromModel("SuperBoy", superboySchema);
Assert.assertNull(superboyModel.parent);
Assert.assertEquals(superboyModel.parent, "Human");
Assert.assertEquals(superboyModel.allParents, null);

// to test allOf with single ref and no "type: object" in the (last) inline schema
Schema superbabySchema = openAPI.getComponents().getSchemas().get("SuperBaby");
CodegenModel superbabyModel = codegen.fromModel("SuperBaby", superbabySchema);
Assert.assertNull(superbabyModel.parent);
Assert.assertEquals(superbabyModel.parent, "Human");
Assert.assertEquals(superbabyModel.allParents, null);
}

Expand Down Expand Up @@ -1602,7 +1602,7 @@ public void testAllOfSingleRefNoOwnProps() {
codegen.setOpenAPI(openAPI);
CodegenModel model = codegen.fromModel("NewMessageEventCoreNoOwnProps", schema);
Assert.assertEquals(getNames(model.getVars()), Arrays.asList("id", "message"));
Assert.assertNull(model.parent);
Assert.assertEquals(model.parent, "MessageEventCore");
Assert.assertNull(model.allParents);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public void mapModelTest() {
Assert.assertEquals(cm.classname, "Sample");
Assert.assertEquals(cm.description, "a map model");
Assert.assertEquals(cm.vars.size(), 0);
Assert.assertEquals(cm.parent, null);
Assert.assertEquals(cm.parent, "dict");
Assert.assertEquals(cm.imports.size(), 1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
ComplexQuadrilateral.JSON_PROPERTY_QUADRILATERAL_TYPE
})
@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class ComplexQuadrilateral {
public class ComplexQuadrilateral extends ShapeInterface {
public static final String JSON_PROPERTY_SHAPE_TYPE = "shapeType";
private String shapeType;

Expand Down Expand Up @@ -155,18 +155,20 @@ public boolean equals(Object o) {
ComplexQuadrilateral complexQuadrilateral = (ComplexQuadrilateral) o;
return Objects.equals(this.shapeType, complexQuadrilateral.shapeType) &&
Objects.equals(this.quadrilateralType, complexQuadrilateral.quadrilateralType)&&
Objects.equals(this.additionalProperties, complexQuadrilateral.additionalProperties);
Objects.equals(this.additionalProperties, complexQuadrilateral.additionalProperties) &&
super.equals(o);
}

@Override
public int hashCode() {
return Objects.hash(shapeType, quadrilateralType, additionalProperties);
return Objects.hash(shapeType, quadrilateralType, super.hashCode(), additionalProperties);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class ComplexQuadrilateral {\n");
sb.append(" ").append(toIndentedString(super.toString())).append("\n");
sb.append(" shapeType: ").append(toIndentedString(shapeType)).append("\n");
sb.append(" quadrilateralType: ").append(toIndentedString(quadrilateralType)).append("\n");
sb.append(" additionalProperties: ").append(toIndentedString(additionalProperties)).append("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
EquilateralTriangle.JSON_PROPERTY_TRIANGLE_TYPE
})
@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class EquilateralTriangle {
public class EquilateralTriangle extends ShapeInterface {
public static final String JSON_PROPERTY_SHAPE_TYPE = "shapeType";
private String shapeType;

Expand Down Expand Up @@ -155,18 +155,20 @@ public boolean equals(Object o) {
EquilateralTriangle equilateralTriangle = (EquilateralTriangle) o;
return Objects.equals(this.shapeType, equilateralTriangle.shapeType) &&
Objects.equals(this.triangleType, equilateralTriangle.triangleType)&&
Objects.equals(this.additionalProperties, equilateralTriangle.additionalProperties);
Objects.equals(this.additionalProperties, equilateralTriangle.additionalProperties) &&
super.equals(o);
}

@Override
public int hashCode() {
return Objects.hash(shapeType, triangleType, additionalProperties);
return Objects.hash(shapeType, triangleType, super.hashCode(), additionalProperties);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class EquilateralTriangle {\n");
sb.append(" ").append(toIndentedString(super.toString())).append("\n");
sb.append(" shapeType: ").append(toIndentedString(shapeType)).append("\n");
sb.append(" triangleType: ").append(toIndentedString(triangleType)).append("\n");
sb.append(" additionalProperties: ").append(toIndentedString(additionalProperties)).append("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
IsoscelesTriangle.JSON_PROPERTY_TRIANGLE_TYPE
})
@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class IsoscelesTriangle {
public class IsoscelesTriangle extends ShapeInterface {
public static final String JSON_PROPERTY_SHAPE_TYPE = "shapeType";
private String shapeType;

Expand Down Expand Up @@ -113,18 +113,20 @@ public boolean equals(Object o) {
}
IsoscelesTriangle isoscelesTriangle = (IsoscelesTriangle) o;
return Objects.equals(this.shapeType, isoscelesTriangle.shapeType) &&
Objects.equals(this.triangleType, isoscelesTriangle.triangleType);
Objects.equals(this.triangleType, isoscelesTriangle.triangleType) &&
super.equals(o);
}

@Override
public int hashCode() {
return Objects.hash(shapeType, triangleType);
return Objects.hash(shapeType, triangleType, super.hashCode());
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class IsoscelesTriangle {\n");
sb.append(" ").append(toIndentedString(super.toString())).append("\n");
sb.append(" shapeType: ").append(toIndentedString(shapeType)).append("\n");
sb.append(" triangleType: ").append(toIndentedString(triangleType)).append("\n");
sb.append("}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
ScaleneTriangle.JSON_PROPERTY_TRIANGLE_TYPE
})
@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class ScaleneTriangle {
public class ScaleneTriangle extends ShapeInterface {
public static final String JSON_PROPERTY_SHAPE_TYPE = "shapeType";
private String shapeType;

Expand Down Expand Up @@ -155,18 +155,20 @@ public boolean equals(Object o) {
ScaleneTriangle scaleneTriangle = (ScaleneTriangle) o;
return Objects.equals(this.shapeType, scaleneTriangle.shapeType) &&
Objects.equals(this.triangleType, scaleneTriangle.triangleType)&&
Objects.equals(this.additionalProperties, scaleneTriangle.additionalProperties);
Objects.equals(this.additionalProperties, scaleneTriangle.additionalProperties) &&
super.equals(o);
}

@Override
public int hashCode() {
return Objects.hash(shapeType, triangleType, additionalProperties);
return Objects.hash(shapeType, triangleType, super.hashCode(), additionalProperties);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class ScaleneTriangle {\n");
sb.append(" ").append(toIndentedString(super.toString())).append("\n");
sb.append(" shapeType: ").append(toIndentedString(shapeType)).append("\n");
sb.append(" triangleType: ").append(toIndentedString(triangleType)).append("\n");
sb.append(" additionalProperties: ").append(toIndentedString(additionalProperties)).append("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
SimpleQuadrilateral.JSON_PROPERTY_QUADRILATERAL_TYPE
})
@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class SimpleQuadrilateral {
public class SimpleQuadrilateral extends ShapeInterface {
public static final String JSON_PROPERTY_SHAPE_TYPE = "shapeType";
private String shapeType;

Expand Down Expand Up @@ -155,18 +155,20 @@ public boolean equals(Object o) {
SimpleQuadrilateral simpleQuadrilateral = (SimpleQuadrilateral) o;
return Objects.equals(this.shapeType, simpleQuadrilateral.shapeType) &&
Objects.equals(this.quadrilateralType, simpleQuadrilateral.quadrilateralType)&&
Objects.equals(this.additionalProperties, simpleQuadrilateral.additionalProperties);
Objects.equals(this.additionalProperties, simpleQuadrilateral.additionalProperties) &&
super.equals(o);
}

@Override
public int hashCode() {
return Objects.hash(shapeType, quadrilateralType, additionalProperties);
return Objects.hash(shapeType, quadrilateralType, super.hashCode(), additionalProperties);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class SimpleQuadrilateral {\n");
sb.append(" ").append(toIndentedString(super.toString())).append("\n");
sb.append(" shapeType: ").append(toIndentedString(shapeType)).append("\n");
sb.append(" quadrilateralType: ").append(toIndentedString(quadrilateralType)).append("\n");
sb.append(" additionalProperties: ").append(toIndentedString(additionalProperties)).append("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri
.registerTypeAdapterFactory(new org.openapitools.client.model.DanishPig.CustomTypeAdapterFactory())
.registerTypeAdapterFactory(new org.openapitools.client.model.DeprecatedObject.CustomTypeAdapterFactory())
.registerTypeAdapterFactory(new org.openapitools.client.model.Dog.CustomTypeAdapterFactory())
.registerTypeAdapterFactory(new org.openapitools.client.model.DogAllOf.CustomTypeAdapterFactory())
.registerTypeAdapterFactory(new org.openapitools.client.model.Drawing.CustomTypeAdapterFactory())
.registerTypeAdapterFactory(new org.openapitools.client.model.EnumArrays.CustomTypeAdapterFactory())
.registerTypeAdapterFactory(new org.openapitools.client.model.EnumTest.CustomTypeAdapterFactory())
Expand Down Expand Up @@ -281,7 +280,6 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri
.registerTypeAdapterFactory(new org.openapitools.client.model.ReadOnlyFirst.CustomTypeAdapterFactory())
.registerTypeAdapterFactory(new org.openapitools.client.model.ScaleneTriangle.CustomTypeAdapterFactory())
.registerTypeAdapterFactory(new org.openapitools.client.model.Shape.CustomTypeAdapterFactory())
.registerTypeAdapterFactory(new org.openapitools.client.model.ShapeInterface.CustomTypeAdapterFactory())
.registerTypeAdapterFactory(new org.openapitools.client.model.ShapeOrNull.CustomTypeAdapterFactory())
.registerTypeAdapterFactory(new org.openapitools.client.model.SimpleQuadrilateral.CustomTypeAdapterFactory())
.registerTypeAdapterFactory(new org.openapitools.client.model.SpecialModelName.CustomTypeAdapterFactory())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
* ArrayOfInlineAllOfArrayAllofDogPropertyInner
*/
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class ArrayOfInlineAllOfArrayAllofDogPropertyInner {
public class ArrayOfInlineAllOfArrayAllofDogPropertyInner extends DogAllOf {
public static final String SERIALIZED_NAME_BREED = "breed";
@SerializedName(SERIALIZED_NAME_BREED)
private String breed;
Expand Down Expand Up @@ -155,18 +155,20 @@ public boolean equals(Object o) {
ArrayOfInlineAllOfArrayAllofDogPropertyInner arrayOfInlineAllOfArrayAllofDogPropertyInner = (ArrayOfInlineAllOfArrayAllofDogPropertyInner) o;
return Objects.equals(this.breed, arrayOfInlineAllOfArrayAllofDogPropertyInner.breed) &&
Objects.equals(this.color, arrayOfInlineAllOfArrayAllofDogPropertyInner.color)&&
Objects.equals(this.additionalProperties, arrayOfInlineAllOfArrayAllofDogPropertyInner.additionalProperties);
Objects.equals(this.additionalProperties, arrayOfInlineAllOfArrayAllofDogPropertyInner.additionalProperties) &&
super.equals(o);
}

@Override
public int hashCode() {
return Objects.hash(breed, color, additionalProperties);
return Objects.hash(breed, color, super.hashCode(), additionalProperties);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class ArrayOfInlineAllOfArrayAllofDogPropertyInner {\n");
sb.append(" ").append(toIndentedString(super.toString())).append("\n");
sb.append(" breed: ").append(toIndentedString(breed)).append("\n");
sb.append(" color: ").append(toIndentedString(color)).append("\n");
sb.append(" additionalProperties: ").append(toIndentedString(additionalProperties)).append("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
* ComplexQuadrilateral
*/
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class ComplexQuadrilateral {
public class ComplexQuadrilateral extends ShapeInterface {
public static final String SERIALIZED_NAME_SHAPE_TYPE = "shapeType";
@SerializedName(SERIALIZED_NAME_SHAPE_TYPE)
private String shapeType;
Expand Down Expand Up @@ -155,18 +155,20 @@ public boolean equals(Object o) {
ComplexQuadrilateral complexQuadrilateral = (ComplexQuadrilateral) o;
return Objects.equals(this.shapeType, complexQuadrilateral.shapeType) &&
Objects.equals(this.quadrilateralType, complexQuadrilateral.quadrilateralType)&&
Objects.equals(this.additionalProperties, complexQuadrilateral.additionalProperties);
Objects.equals(this.additionalProperties, complexQuadrilateral.additionalProperties) &&
super.equals(o);
}

@Override
public int hashCode() {
return Objects.hash(shapeType, quadrilateralType, additionalProperties);
return Objects.hash(shapeType, quadrilateralType, super.hashCode(), additionalProperties);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class ComplexQuadrilateral {\n");
sb.append(" ").append(toIndentedString(super.toString())).append("\n");
sb.append(" shapeType: ").append(toIndentedString(shapeType)).append("\n");
sb.append(" quadrilateralType: ").append(toIndentedString(quadrilateralType)).append("\n");
sb.append(" additionalProperties: ").append(toIndentedString(additionalProperties)).append("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,69 +186,6 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
}
}

public static class CustomTypeAdapterFactory implements TypeAdapterFactory {
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (!DogAllOf.class.isAssignableFrom(type.getRawType())) {
return null; // this class only serializes 'DogAllOf' and its subtypes
}
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
final TypeAdapter<DogAllOf> thisAdapter
= gson.getDelegateAdapter(this, TypeToken.get(DogAllOf.class));

return (TypeAdapter<T>) new TypeAdapter<DogAllOf>() {
@Override
public void write(JsonWriter out, DogAllOf value) throws IOException {
JsonObject obj = thisAdapter.toJsonTree(value).getAsJsonObject();
obj.remove("additionalProperties");
// serialize additonal properties
if (value.getAdditionalProperties() != null) {
for (Map.Entry<String, Object> entry : value.getAdditionalProperties().entrySet()) {
if (entry.getValue() instanceof String)
obj.addProperty(entry.getKey(), (String) entry.getValue());
else if (entry.getValue() instanceof Number)
obj.addProperty(entry.getKey(), (Number) entry.getValue());
else if (entry.getValue() instanceof Boolean)
obj.addProperty(entry.getKey(), (Boolean) entry.getValue());
else if (entry.getValue() instanceof Character)
obj.addProperty(entry.getKey(), (Character) entry.getValue());
else {
obj.add(entry.getKey(), gson.toJsonTree(entry.getValue()).getAsJsonObject());
}
}
}
elementAdapter.write(out, obj);
}

@Override
public DogAllOf read(JsonReader in) throws IOException {
JsonObject jsonObj = elementAdapter.read(in).getAsJsonObject();
validateJsonObject(jsonObj);
// store additional fields in the deserialized instance
DogAllOf instance = thisAdapter.fromJsonTree(jsonObj);
for (Map.Entry<String, JsonElement> entry : jsonObj.entrySet()) {
if (!openapiFields.contains(entry.getKey())) {
if (entry.getValue().isJsonPrimitive()) { // primitive type
if (entry.getValue().getAsJsonPrimitive().isString())
instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsString());
else if (entry.getValue().getAsJsonPrimitive().isNumber())
instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsNumber());
else if (entry.getValue().getAsJsonPrimitive().isBoolean())
instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsBoolean());
else
throw new IllegalArgumentException(String.format("The field `%s` has unknown primitive type. Value: %s", entry.getKey(), entry.getValue().toString()));
} else { // non-primitive type
instance.putAdditionalProperty(entry.getKey(), gson.fromJson(entry.getValue(), HashMap.class));
}
}
}
return instance;
}

}.nullSafe();
}
}

/**
* Create an instance of DogAllOf given an JSON string
Expand Down
Loading