diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/definitions/OASParserUtil.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/definitions/OASParserUtil.java index dfbef31f08b8..ed94bf83c601 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/definitions/OASParserUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/definitions/OASParserUtil.java @@ -654,6 +654,13 @@ private static void setRefOfApiResponse(ApiResponse response, SwaggerUpdateConte addToReferenceObjectMap(ref, context); } } + + Map headers = response.getHeaders(); + if (headers != null) { + for (Header header : headers.values()) { + setRefOfApiResponseHeader(header, context); + } + } } } @@ -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); + } } } } @@ -773,9 +785,15 @@ 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); @@ -783,7 +801,7 @@ private static void extractReferenceFromSchema(Schema schema, SwaggerUpdateConte 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) { @@ -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) { @@ -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) { @@ -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."); } } @@ -855,13 +878,14 @@ private static void processSchemaProperties(Schema schema, SwaggerUpdateContext } } - private static List addSchemaOfSchema(Schema schema) { + private static List addSchemaOfSchema(Schema schema, SwaggerUpdateContext context) { List references = new ArrayList(); 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()); @@ -877,6 +901,9 @@ private static List 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); } } }