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

Adds parseFlattenSpec #5526

Merged
merged 10 commits into from
Mar 17, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,10 @@ private static void visitContent(OpenAPI openAPI, Content content, OpenAPISchema

/**
* Invoke the specified visitor function for every schema that matches mimeType in the OpenAPI document.
*
*
* To avoid infinite recursion, referenced schemas are visited only once. When a referenced schema is visited,
* it is added to visitedSchemas.
*
*
* @param openAPI the OpenAPI document that contains schema objects.
* @param schema the root schema object to be visited.
* @param mimeType the mime type. TODO: does not seem to be used in a meaningful way.
Expand Down Expand Up @@ -355,22 +355,22 @@ public static String getSimpleRef(String ref) {

/**
* Return true if the specified schema is an object with a fixed number of properties.
*
*
* A ObjectSchema differs from an MapSchema in the following way:
* - An ObjectSchema is not extensible, i.e. it has a fixed number of properties.
* - A MapSchema is an object that can be extended with an arbitrary set of properties.
* The payload may include dynamic properties.
*
*
* For example, an OpenAPI schema is considered an ObjectSchema in the following scenarios:
*
*
* type: object
* additionalProperties: false
* properties:
* name:
* type: string
* address:
* type: string
*
*
* @param schema the OAS schema
* @return true if the specified schema is an Object schema.
*/
Expand All @@ -394,7 +394,7 @@ public static boolean isObjectSchema(Schema schema) {
/**
* Return true if the specified schema is composed, i.e. if it uses
* 'oneOf', 'anyOf' or 'allOf'.
*
*
* @param schema the OAS schema
* @return true if the specified schema is a Composed schema.
*/
Expand Down Expand Up @@ -659,7 +659,7 @@ public static boolean isModel(Schema schema) {

/**
* Check to see if the schema is a free form object.
*
*
* A free form object is an object (i.e. 'type: object' in a OAS document) that:
* 1) Does not define properties, and
* 2) Is not a composed schema (no anyOf, oneOf, allOf), and
Expand Down Expand Up @@ -737,7 +737,7 @@ public static Schema getSchema(OpenAPI openAPI, String name) {
* Return a Map of the schemas defined under /components/schemas in the OAS document.
* The returned Map only includes the direct children of /components/schemas in the OAS document; the Map
* does not include inlined schemas.
*
*
* @param openAPI the OpenAPI document.
* @return a map of schemas in the OAS document.
*/
Expand Down Expand Up @@ -767,7 +767,7 @@ public static List<Schema> getAllSchemas(OpenAPI openAPI) {
});
return allSchemas;
}

/**
* If a RequestBody contains a reference to an other RequestBody with '$ref', returns the referenced RequestBody if it is found or the actual RequestBody in the other cases.
*
Expand Down Expand Up @@ -906,10 +906,10 @@ public static Schema getSchemaFromResponse(ApiResponse response) {

/**
* Return the first Schema from a specified OAS 'content' section.
*
*
* For example, given the following OAS, this method returns the schema
* for the 'application/json' content type because it is listed first in the OAS.
*
*
* responses:
* '200':
* content:
Expand All @@ -918,8 +918,8 @@ public static Schema getSchemaFromResponse(ApiResponse response) {
* $ref: '#/components/schemas/XYZ'
* application/xml:
* ...
*
* @param content a 'content' section in the OAS specification.
*
* @param content a 'content' section in the OAS specification.
* @return the Schema.
*/
private static Schema getSchemaFromContent(Content content) {
Expand Down Expand Up @@ -1110,6 +1110,14 @@ public static String getParentName(ComposedSchema composedSchema, Map<String, Sc
int nullSchemaChildrenCount = 0;
boolean hasAmbiguousParents = false;
List<String> refedWithoutDiscriminator = new ArrayList<>();
String schemaName = "";
jimschubert marked this conversation as resolved.
Show resolved Hide resolved
for (String thisSchemaName : allSchemas.keySet()) {
Schema sc = allSchemas.get(thisSchemaName);
if (isComposedSchema(sc) && (ComposedSchema) sc == composedSchema) {
schemaName = thisSchemaName;
break;
}
}

if (interfaces != null && !interfaces.isEmpty()) {
for (Schema schema : interfaces) {
Expand All @@ -1126,7 +1134,10 @@ public static String getParentName(ComposedSchema composedSchema, Map<String, Sc
} else {
// not a parent since discriminator.propertyName is not set
hasAmbiguousParents = true;
refedWithoutDiscriminator.add(parentName);
boolean isNotExtractedInlineSchema = !parentName.equals(schemaName+"_allOf");
if (isNotExtractedInlineSchema) {
refedWithoutDiscriminator.add(parentName);
}
}
} else {
// not a ref, doing nothing, except counting the number of times the 'null' type
Expand Down Expand Up @@ -1235,19 +1246,19 @@ else if (schema instanceof ComposedSchema) {
/**
* Return true if the 'nullable' attribute is set to true in the schema, i.e. if the value
* of the property can be the null value.
*
*
* In addition, if the OAS document is 3.1 or above, isNullable returns true if the input
* schema is a 'oneOf' composed document with at most two children, and one of the children
* is the 'null' type.
*
*
* The caller is responsible for resolving schema references before invoking isNullable.
* If the input schema is a $ref and the referenced schema has 'nullable: true', this method
* returns false (because the nullable attribute is defined in the referenced schema).
*
*
* The 'nullable' attribute was introduced in OAS 3.0.
* The 'nullable' attribute is deprecated in OAS 3.1. In a OAS 3.1 document, the preferred way
* to specify nullable properties is to use the 'null' type.
*
*
* @param schema the OAS schema.
* @return true if the schema is nullable.
*/
Expand All @@ -1273,7 +1284,7 @@ public static boolean isNullable(Schema schema) {
/**
* Return true if the specified composed schema is 'oneOf', contains one or two elements,
* and at least one of the elements is the 'null' type.
*
*
* The 'null' type is supported in OAS 3.1 and above.
* In the example below, the 'OptionalOrder' can have the null value because the 'null'
* type is one of the elements under 'oneOf'.
Expand All @@ -1282,7 +1293,7 @@ public static boolean isNullable(Schema schema) {
* oneOf:
* - type: 'null'
* - $ref: '#/components/schemas/Order'
*
*
* @param schema the OAS composed schema.
* @return true if the composed schema is nullable.
*/
Expand All @@ -1296,22 +1307,22 @@ public static boolean isNullableComposedSchema(ComposedSchema schema) {
}
}
return false;
}
}

/**
* isNullType returns true if the input schema is the 'null' type.
*
*
* The 'null' type is supported in OAS 3.1 and above. It is not supported
* in OAS 2.0 and OAS 3.0.x.
*
*
* For example, the "null" type could be used to specify that a value must
* either be null or a specified type:
*
*
* OptionalOrder:
* oneOf:
* - type: 'null'
* - $ref: '#/components/schemas/Order'
*
*
* @param schema the OpenAPI schema
* @return true if the schema is the 'null' type
*/
Expand Down Expand Up @@ -1350,4 +1361,4 @@ public static void syncValidationProperties(Schema schema, IJsonSchemaValidation
if (maxProperties != null) target.setMaxProperties(maxProperties);
}
}
}
}
Loading