Compare commits

...

5 Commits

Author SHA1 Message Date
William Cheng
66f8d15e75 fix format 2025-01-14 14:30:06 +08:00
William Cheng
b2e51982c0 update 2025-01-14 14:27:31 +08:00
William Cheng
01584fed76 better formnat 2025-01-14 14:26:24 +08:00
William Cheng
19f307d6cd better format 2025-01-14 14:22:52 +08:00
William Cheng
754207a636 fix schema with just a description 2025-01-14 14:20:18 +08:00
3 changed files with 82 additions and 57 deletions

View File

@ -430,14 +430,14 @@ public class ModelUtils {
* <p> * <p>
* For example, an OpenAPI schema is considered an ObjectSchema in the following scenarios: * For example, an OpenAPI schema is considered an ObjectSchema in the following scenarios:
* <p> * <p>
* * <p>
* type: object * type: object
* additionalProperties: false * additionalProperties: false
* properties: * properties:
* name: * name:
* type: string * type: string
* address: * address:
* type: string * type: string
* *
* @param schema the OAS schema * @param schema the OAS schema
* @return true if the specified schema is an Object schema. * @return true if the specified schema is an Object schema.
@ -527,28 +527,28 @@ public class ModelUtils {
* A MapSchema differs from an ObjectSchema in the following way: * A MapSchema differs from an ObjectSchema in the following way:
* - An ObjectSchema is not extensible, i.e. it has a fixed number of properties. * - 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. * - A MapSchema is an object that can be extended with an arbitrary set of properties.
* The payload may include dynamic properties. * The payload may include dynamic properties.
* <p> * <p>
* Note that isMapSchema returns true for a composed schema (allOf, anyOf, oneOf) that also defines * Note that isMapSchema returns true for a composed schema (allOf, anyOf, oneOf) that also defines
* additionalproperties. * additionalproperties.
* <p> * <p>
* For example, an OpenAPI schema is considered a MapSchema in the following scenarios: * For example, an OpenAPI schema is considered a MapSchema in the following scenarios:
* <p> * <p>
* * <p>
* type: object
* additionalProperties: true
* <p>
* type: object
* additionalProperties:
* type: object * type: object
* additionalProperties: true
*
* type: object
* additionalProperties:
* type: object
* properties: * properties:
* code: * code:
* type: integer * type: integer
* * <p>
* allOf: * allOf:
* - $ref: '#/components/schemas/Class1' * - $ref: '#/components/schemas/Class1'
* - $ref: '#/components/schemas/Class2' * - $ref: '#/components/schemas/Class2'
* additionalProperties: true * additionalProperties: true
* *
* @param schema the OAS schema * @param schema the OAS schema
* @return true if the specified schema is a Map schema. * @return true if the specified schema is a Map schema.
@ -857,7 +857,7 @@ public class ModelUtils {
* The value can be any type except the 'null' value. * The value can be any type except the 'null' value.
* *
* @param schema potentially containing a '$ref' * @param schema potentially containing a '$ref'
* @param openAPI document containing the Schema. * @param openAPI document containing the Schema.
* @return true if it's a free-form object * @return true if it's a free-form object
*/ */
public static boolean isFreeFormObject(Schema schema, OpenAPI openAPI) { public static boolean isFreeFormObject(Schema schema, OpenAPI openAPI) {
@ -999,8 +999,8 @@ public class ModelUtils {
/** /**
* Get the schema referenced by $ref to schema's properties, e.g. #/components/schemas/Pet/properties/category. * Get the schema referenced by $ref to schema's properties, e.g. #/components/schemas/Pet/properties/category.
* *
* @param openAPI specification being checked * @param openAPI specification being checked
* @param refString schema reference * @param refString schema reference
* @return schema * @return schema
*/ */
public static Schema getSchemaFromRefToSchemaWithProperties(OpenAPI openAPI, String refString) { public static Schema getSchemaFromRefToSchemaWithProperties(OpenAPI openAPI, String refString) {
@ -1021,7 +1021,7 @@ public class ModelUtils {
/** /**
* Returns true if $ref to a reference to schema's properties, e.g. #/components/schemas/Pet/properties/category. * Returns true if $ref to a reference to schema's properties, e.g. #/components/schemas/Pet/properties/category.
* *
* @param refString schema reference * @param refString schema reference
* @return true if $ref to a reference to schema's properties * @return true if $ref to a reference to schema's properties
*/ */
public static boolean isRefToSchemaWithProperties(String refString) { public static boolean isRefToSchemaWithProperties(String refString) {
@ -1198,7 +1198,7 @@ public class ModelUtils {
/** /**
* Return the first defined Schema for a ApiResponse * Return the first defined Schema for a ApiResponse
* *
* @param openAPI OpenAPI spec. * @param openAPI OpenAPI spec.
* @param response API response of the operation * @param response API response of the operation
* @return firstSchema * @return firstSchema
*/ */
@ -1218,13 +1218,13 @@ public class ModelUtils {
* for the 'application/json' content type because it is listed first in the OAS. * for the 'application/json' content type because it is listed first in the OAS.
* <p> * <p>
* responses: * responses:
* '200': * '200':
* content: * content:
* application/json: * application/json:
* schema: * schema:
* $ref: '#/components/schemas/XYZ' * $ref: '#/components/schemas/XYZ'
* application/xml: * application/xml:
* ... * ...
* *
* @param content a 'content' section in the OAS specification. * @param content a 'content' section in the OAS specification.
* @return the Schema. * @return the Schema.
@ -1427,7 +1427,7 @@ public class ModelUtils {
* any additional properties are allowed. This is equivalent to setting additionalProperties * any additional properties are allowed. This is equivalent to setting additionalProperties
* to the boolean value True or setting additionalProperties: {} * to the boolean value True or setting additionalProperties: {}
* *
* @param schema the input schema that may or may not have the additionalProperties keyword. * @param schema the input schema that may or may not have the additionalProperties keyword.
* @return the Schema of the additionalProperties. The null value is returned if no additional * @return the Schema of the additionalProperties. The null value is returned if no additional
* properties are allowed. * properties are allowed.
*/ */
@ -1540,19 +1540,19 @@ public class ModelUtils {
* because 'Animal' specifies a discriminator. * because 'Animal' specifies a discriminator.
* <p> * <p>
* animal: * animal:
* type: object * type: object
* discriminator: * discriminator:
* propertyName: type * propertyName: type
* properties: * properties:
* type: string * type: string
* *
* <p> * <p>
* dog: * dog:
* allOf: * allOf:
* - $ref: '#/components/schemas/animal' * - $ref: '#/components/schemas/animal'
* - type: object * - type: object
* properties: * properties:
* breed: string * breed: string
* *
* @param composedSchema schema (alias or direct reference) * @param composedSchema schema (alias or direct reference)
* @param allSchemas all schemas * @param allSchemas all schemas
@ -1683,7 +1683,7 @@ public class ModelUtils {
* If it's string, return true if it's non-empty. * If it's string, return true if it's non-empty.
* If the return value is `true`, the schema is a parent. * If the return value is `true`, the schema is a parent.
* *
* @param schema Schema * @param schema Schema
* @return boolean * @return boolean
*/ */
public static boolean isExtensionParent(Schema schema) { public static boolean isExtensionParent(Schema schema) {
@ -1750,9 +1750,9 @@ public class ModelUtils {
* type is one of the elements under 'oneOf'. * type is one of the elements under 'oneOf'.
* <p> * <p>
* OptionalOrder: * OptionalOrder:
* oneOf: * oneOf:
* - type: 'null' * - type: 'null'
* - $ref: '#/components/schemas/Order' * - $ref: '#/components/schemas/Order'
* *
* @param schema the OAS composed schema. * @param schema the OAS composed schema.
* @return true if the composed schema is nullable. * @return true if the composed schema is nullable.
@ -1935,7 +1935,7 @@ public class ModelUtils {
private static void logWarnMessagesForIneffectiveValidations(Set<String> setValidations, Schema schema, Set<String> effectiveValidations) { private static void logWarnMessagesForIneffectiveValidations(Set<String> setValidations, Schema schema, Set<String> effectiveValidations) {
setValidations.removeAll(effectiveValidations); setValidations.removeAll(effectiveValidations);
setValidations.stream().forEach(validation -> { setValidations.stream().forEach(validation -> {
LOGGER.warn("Validation '" + validation + "' has no effect on schema '" + getType(schema) +"'. Ignoring!"); LOGGER.warn("Validation '" + validation + "' has no effect on schema '" + getType(schema) + "'. Ignoring!");
}); });
} }
@ -2214,9 +2214,8 @@ public class ModelUtils {
* <p> * <p>
* Return true if the schema's type is 'null' or not specified * Return true if the schema's type is 'null' or not specified
* *
* @param schema Schema * @param schema Schema
* @param openAPI OpenAPI * @param openAPI OpenAPI
*
* @return true if schema is null type * @return true if schema is null type
*/ */
public static boolean isNullTypeSchema(OpenAPI openAPI, Schema schema) { public static boolean isNullTypeSchema(OpenAPI openAPI, Schema schema) {
@ -2260,11 +2259,14 @@ public class ModelUtils {
} }
// for `type: null` // for `type: null`
if (schema.getTypes() == null && schema.get$ref() == null) { if (schema.getTypes() == null && schema.get$ref() == null
&& schema.getDescription() == null) { // ensure it's not schema with just a description
return true; return true;
} }
} else { // 3.0.x or 2.x spec } else { // 3.0.x or 2.x spec
if ((schema.getType() == null || schema.getType().equals("null")) && schema.get$ref() == null) { if ((schema.getType() == null || schema.getType().equals("null"))
&& schema.get$ref() == null
&& schema.getDescription() == null) { // ensure it's not schema with just a description
return true; return true;
} }
} }
@ -2277,9 +2279,8 @@ public class ModelUtils {
* <p> * <p>
* Return true if the schema can be handled by OpenAPI Generator * Return true if the schema can be handled by OpenAPI Generator
* *
* @param schema Schema * @param schema Schema
* @param openAPI OpenAPIs * @param openAPI OpenAPIs
*
* @return true if schema is null type * @return true if schema is null type
*/ */
public static boolean isUnsupportedSchema(OpenAPI openAPI, Schema schema) { public static boolean isUnsupportedSchema(OpenAPI openAPI, Schema schema) {
@ -2290,7 +2291,7 @@ public class ModelUtils {
// dereference the schema // dereference the schema
schema = ModelUtils.getReferencedSchema(openAPI, schema); schema = ModelUtils.getReferencedSchema(openAPI, schema);
if (schema.getTypes() == null && hasValidation(schema)) { if (schema.getTypes() == null && hasValidation(schema)) {
// just validation without type // just validation without type
return true; return true;
} else if (schema.getIf() != null && schema.getThen() != null) { } else if (schema.getIf() != null && schema.getThen() != null) {

View File

@ -497,9 +497,22 @@ public class ModelUtilsTest {
schema = openAPI.getComponents().getSchemas().get("AnyOfTest"); schema = openAPI.getComponents().getSchemas().get("AnyOfTest");
assertFalse(ModelUtils.isNullTypeSchema(openAPI, schema)); assertFalse(ModelUtils.isNullTypeSchema(openAPI, schema));
// first element (getAnyOf().get(0)) is a string. no need to test
assertTrue(ModelUtils.isNullTypeSchema(openAPI, (Schema) schema.getAnyOf().get(1))); assertTrue(ModelUtils.isNullTypeSchema(openAPI, (Schema) schema.getAnyOf().get(1)));
assertTrue(ModelUtils.isNullTypeSchema(openAPI, (Schema) schema.getAnyOf().get(2))); assertTrue(ModelUtils.isNullTypeSchema(openAPI, (Schema) schema.getAnyOf().get(2)));
assertTrue(ModelUtils.isNullTypeSchema(openAPI, (Schema) schema.getAnyOf().get(3))); assertTrue(ModelUtils.isNullTypeSchema(openAPI, (Schema) schema.getAnyOf().get(3)));
schema = openAPI.getComponents().getSchemas().get("OneOfRef");
assertFalse(ModelUtils.isNullTypeSchema(openAPI, schema));
assertFalse(ModelUtils.isNullTypeSchema(openAPI, (Schema) schema.getOneOf().get(0)));
schema = openAPI.getComponents().getSchemas().get("OneOfMultiRef");
assertFalse(ModelUtils.isNullTypeSchema(openAPI, schema));
assertFalse(ModelUtils.isNullTypeSchema(openAPI, (Schema) schema.getOneOf().get(0)));
assertFalse(ModelUtils.isNullTypeSchema(openAPI, (Schema) schema.getOneOf().get(1)));
schema = openAPI.getComponents().getSchemas().get("JustDescription");
assertFalse(ModelUtils.isNullTypeSchema(openAPI, schema));
} }
@Test @Test

View File

@ -58,7 +58,7 @@ components:
$ref: '#/components/schemas/IntegerRef' $ref: '#/components/schemas/IntegerRef'
number: number:
anyOf: anyOf:
- $ref: '#/components/schemas/Number' - $ref: '#/components/schemas/StringRef'
AnyOfStringArrayOfString: AnyOfStringArrayOfString:
anyOf: anyOf:
- type: string - type: string
@ -85,6 +85,8 @@ components:
- $ref: '#/components/schemas/IntegerRef' - $ref: '#/components/schemas/IntegerRef'
IntegerRef: IntegerRef:
type: integer type: integer
StringRef:
type: string
OneOfAnyType: OneOfAnyType:
oneOf: oneOf:
- type: object - type: object
@ -94,3 +96,12 @@ components:
- type: integer - type: integer
- type: array - type: array
items: {} items: {}
OneOfRef:
oneOf:
- $ref: '#/components/schemas/IntegerRef'
OneOfMultiRef:
oneOf:
- $ref: '#/components/schemas/IntegerRef'
- $ref: '#/components/schemas/StringRef'
JustDescription:
Description: A schema with just description