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

Improve Generation of the API Definition for API Products #12694

Merged
merged 4 commits into from
Nov 8, 2024
Merged
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 @@ -654,6 +654,13 @@ private static void setRefOfApiResponse(ApiResponse response, SwaggerUpdateConte
addToReferenceObjectMap(ref, context);
}
}

Map<String, Header> headers = response.getHeaders();
if (headers != null) {
for (Header header : headers.values()) {
setRefOfApiResponseHeader(header, context);
}
}
}
}

Expand Down Expand Up @@ -683,9 +690,14 @@ private static void setRefOfApiResponseHeader(Header header, SwaggerUpdateContex
if (content != null) {
extractReferenceFromContent(content, context);
} else {
String ref = header.get$ref();
if (ref != null) {
addToReferenceObjectMap(ref, context);
Schema schema = header.getSchema();
if (schema != null) {
extractReferenceFromSchema(schema, context);
} else {
String ref = header.get$ref();
if (ref != null) {
addToReferenceObjectMap(ref, context);
}
}
}
}
Expand Down Expand Up @@ -773,17 +785,23 @@ private static void extractReferenceFromSchema(Schema schema, SwaggerUpdateConte
if (ref == null) {
if (schema instanceof ArraySchema) {
ArraySchema arraySchema = (ArraySchema) schema;
ref = arraySchema.getItems().get$ref();
Schema itemsSchema = arraySchema.getItems();
// Process $ref items
ref = itemsSchema.get$ref();
if (ref == null) {
// Process items in the form of Composed Schema such as allOf, oneOf, anyOf
extractReferenceFromSchema(itemsSchema, context);
}
} else if (schema instanceof ObjectSchema) {
references = addSchemaOfSchema(schema);
references = addSchemaOfSchema(schema, context);
} else if (schema instanceof MapSchema) {
Schema additionalPropertiesSchema = (Schema) schema.getAdditionalProperties();
extractReferenceFromSchema(additionalPropertiesSchema, context);
} else if (schema instanceof ComposedSchema) {
if (((ComposedSchema) schema).getAllOf() != null) {
for (Schema sc : ((ComposedSchema) schema).getAllOf()) {
if (OBJECT_DATA_TYPE.equalsIgnoreCase(sc.getType())) {
references.addAll(addSchemaOfSchema(sc));
references.addAll(addSchemaOfSchema(sc, context));
} else {
String schemaRef = sc.get$ref();
if (schemaRef != null) {
Expand All @@ -793,10 +811,11 @@ private static void extractReferenceFromSchema(Schema schema, SwaggerUpdateConte
}
}
}
} else if (((ComposedSchema) schema).getAnyOf() != null) {
}
if (((ComposedSchema) schema).getAnyOf() != null) {
for (Schema sc : ((ComposedSchema) schema).getAnyOf()) {
if (OBJECT_DATA_TYPE.equalsIgnoreCase(sc.getType())) {
references.addAll(addSchemaOfSchema(sc));
references.addAll(addSchemaOfSchema(sc, context));
} else {
String schemaRef = sc.get$ref();
if (schemaRef != null) {
Expand All @@ -806,10 +825,11 @@ private static void extractReferenceFromSchema(Schema schema, SwaggerUpdateConte
}
}
}
} else if (((ComposedSchema) schema).getOneOf() != null) {
}
if (((ComposedSchema) schema).getOneOf() != null) {
for (Schema sc : ((ComposedSchema) schema).getOneOf()) {
if (OBJECT_DATA_TYPE.equalsIgnoreCase(sc.getType())) {
references.addAll(addSchemaOfSchema(sc));
references.addAll(addSchemaOfSchema(sc, context));
} else {
String schemaRef = sc.get$ref();
if (schemaRef != null) {
Expand All @@ -819,7 +839,10 @@ private static void extractReferenceFromSchema(Schema schema, SwaggerUpdateConte
}
}
}
} else {
}
if (((ComposedSchema) schema).getAllOf() == null &&
((ComposedSchema) schema).getAnyOf() == null &&
((ComposedSchema) schema).getOneOf() == null) {
log.error("Unidentified schema. The schema is not available in the API definition.");
}
}
Expand Down Expand Up @@ -855,13 +878,14 @@ private static void processSchemaProperties(Schema schema, SwaggerUpdateContext
}
}

private static List<String> addSchemaOfSchema(Schema schema) {
private static List<String> addSchemaOfSchema(Schema schema, SwaggerUpdateContext context) {
List<String> references = new ArrayList<String>();
ObjectSchema os = (ObjectSchema) schema;
if (os.getProperties() != null) {
for (String propertyName : os.getProperties().keySet()) {
if (os.getProperties().get(propertyName) instanceof ComposedSchema) {
ComposedSchema cs = (ComposedSchema) os.getProperties().get(propertyName);
Schema propertySchema = os.getProperties().get(propertyName);
if (propertySchema instanceof ComposedSchema) {
ComposedSchema cs = (ComposedSchema) propertySchema;
if (cs.getAllOf() != null) {
for (Schema sc : cs.getAllOf()) {
references.add(sc.get$ref());
Expand All @@ -877,6 +901,9 @@ private static List<String> addSchemaOfSchema(Schema schema) {
} else {
log.error("Unidentified schema. The schema is not available in the API definition.");
}
} else if (propertySchema instanceof ObjectSchema ||
propertySchema instanceof ArraySchema) {
extractReferenceFromSchema(propertySchema, context);
}
}
}
Expand Down
Loading