Skip to content

Commit

Permalink
auto fix self-reference schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 committed Oct 12, 2024
1 parent 462f450 commit d1ca82c
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,64 @@ private void normalizeComponentsSchemas() {
}
}

// auto fix self reference schema to avoid stack overflow
fixSelfReferenceSchema(schemaName, schema);

// normalize the schemas
schemas.put(schemaName, normalizeSchema(schema, new HashSet<>()));
}
}
}

/**
* Auto fix a self referencing schema using any type to replace the self-referencing sub-item.
*
* @param name Schema name
* @param schema Schema
*/
public void fixSelfReferenceSchema(String name, Schema schema) {
if (ModelUtils.isArraySchema(schema)) {
if (isSelfReference(name, schema.getItems())) {
LOGGER.error("Array schema {} has a sub-item referencing itself. Worked around the self-reference schema using any type instead.", name);
schema.setItems(new Schema<>());
}
}

if (ModelUtils.isOneOf(schema)) {
for (int i = 0; i < schema.getOneOf().size(); i++) {
if (isSelfReference(name, (Schema) schema.getOneOf().get(i))) {
LOGGER.error("oneOf schema {} has a sub-item referencing itself. Worked around the self-reference schema by removing it.", name);
schema.getOneOf().remove(i);
}
}
}

if (ModelUtils.isAnyOf(schema)) {
for (int i = 0; i < schema.getAnyOf().size(); i++) {
if (isSelfReference(name, (Schema) schema.getAnyOf().get(i))) {
LOGGER.error("anyOf schema {} has a sub-item referencing itself. Worked around the self-reference schema by removing it.", name);
schema.getAnyOf().remove(i);
}
}
}

if (schema.getAdditionalProperties() != null && schema.getAdditionalProperties() instanceof Schema) {
if (isSelfReference(name, (Schema) schema.getAdditionalProperties())) {
LOGGER.error("Schema {} (with additional properties) has a sub-item referencing itself. Worked around the self-reference schema using any type instead.", name);
schema.setAdditionalProperties(new Schema<>());
}
}

}

private boolean isSelfReference(String name, Schema subSchema) {
if (subSchema != null && name.equals(ModelUtils.getSimpleRef(subSchema.get$ref()))) {
return true;
} else {
return false;
}
}

/**
* Normalizes a schema
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,7 @@ public void setParameterExampleValue(CodegenParameter codegenParameter, RequestB
MediaType mediaType = content.values().iterator().next();
if (mediaType.getExample() != null) {
if (isModel) {
LOGGER.warn("Ignoring complex example on request body");
once(LOGGER).warn("Ignoring complex example on request body");
} else {
codegenParameter.example = mediaType.getExample().toString();
return;
Expand All @@ -1504,7 +1504,7 @@ public void setParameterExampleValue(CodegenParameter codegenParameter, RequestB
Example example = mediaType.getExamples().values().iterator().next();
if (example.getValue() != null) {
if (isModel) {
LOGGER.warn("Ignoring complex example on request body");
once(LOGGER).warn("Ignoring complex example on request body");
} else {
codegenParameter.example = example.getValue().toString();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1066,3 +1066,24 @@ components:
- $ref: '#/components/schemas/SimpleModelWithArrayProperty'
myObject:
type: object
SelfReference:
type: array
items:
$ref: "#/components/schemas/SelfReference"
SelfReferenceOneOf:
oneOf:
- type: string
- type: boolean
- $ref: "#/components/schemas/SelfReferenceOneOf"
SelfReferenceAnyOf:
anyOf:
- type: string
- type: boolean
- $ref: "#/components/schemas/SelfReferenceAnyOf"
SelfReferenceAdditionalProperties:
type: object
additionalProperties:
$ref: "#/components/schemas/SelfReferenceAdditionalProperties"
properties:
dummy:
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ docs/Pet.md
docs/PetApi.md
docs/RefRefToPathLevelParameterOneofRefToOneofParameter.md
docs/RefToRefParameterAnyofRefToAnyofParameter.md
docs/SelfReferenceAdditionalProperties.md
docs/SelfReferenceAnyOf.md
docs/SelfReferenceOneOf.md
docs/SimpleModelWithArrayProperty.md
docs/StoreApi.md
docs/StringOrInt.md
Expand Down Expand Up @@ -82,6 +85,9 @@ src/main/java/org/openapitools/client/model/Order.java
src/main/java/org/openapitools/client/model/Pet.java
src/main/java/org/openapitools/client/model/RefRefToPathLevelParameterOneofRefToOneofParameter.java
src/main/java/org/openapitools/client/model/RefToRefParameterAnyofRefToAnyofParameter.java
src/main/java/org/openapitools/client/model/SelfReferenceAdditionalProperties.java
src/main/java/org/openapitools/client/model/SelfReferenceAnyOf.java
src/main/java/org/openapitools/client/model/SelfReferenceOneOf.java
src/main/java/org/openapitools/client/model/SimpleModelWithArrayProperty.java
src/main/java/org/openapitools/client/model/StringOrInt.java
src/main/java/org/openapitools/client/model/Tag.java
Expand Down
3 changes: 3 additions & 0 deletions samples/client/petstore/java/okhttp-gson-3.1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ Class | Method | HTTP request | Description
- [Pet](docs/Pet.md)
- [RefRefToPathLevelParameterOneofRefToOneofParameter](docs/RefRefToPathLevelParameterOneofRefToOneofParameter.md)
- [RefToRefParameterAnyofRefToAnyofParameter](docs/RefToRefParameterAnyofRefToAnyofParameter.md)
- [SelfReferenceAdditionalProperties](docs/SelfReferenceAdditionalProperties.md)
- [SelfReferenceAnyOf](docs/SelfReferenceAnyOf.md)
- [SelfReferenceOneOf](docs/SelfReferenceOneOf.md)
- [SimpleModelWithArrayProperty](docs/SimpleModelWithArrayProperty.md)
- [StringOrInt](docs/StringOrInt.md)
- [Tag](docs/Tag.md)
Expand Down
16 changes: 16 additions & 0 deletions samples/client/petstore/java/okhttp-gson-3.1/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,22 @@ components:
- $ref: '#/components/schemas/SimpleModelWithArrayProperty'
myObject:
type: object
SelfReference:
items: {}
type: array
SelfReferenceOneOf:
oneOf:
- type: string
- type: boolean
SelfReferenceAnyOf:
anyOf:
- type: string
- type: boolean
SelfReferenceAdditionalProperties:
additionalProperties: {}
properties:
dummy:
type: string
updatePetWithForm_request:
properties:
name:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Pet.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.RefRefToPathLevelParameterOneofRefToOneofParameter.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.RefToRefParameterAnyofRefToAnyofParameter.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.SelfReferenceAdditionalProperties.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.SelfReferenceAnyOf.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.SelfReferenceOneOf.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.SimpleModelWithArrayProperty.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.StringOrInt.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Tag.CustomTypeAdapterFactory());
Expand Down

0 comments on commit d1ca82c

Please sign in to comment.