Package org.openapitools.codegen
Class OpenAPINormalizer
- java.lang.Object
-
- org.openapitools.codegen.OpenAPINormalizer
-
public class OpenAPINormalizer extends Object
-
-
Constructor Summary
Constructors Constructor Description OpenAPINormalizer(io.swagger.v3.oas.models.OpenAPI openAPI, Map<String,String> inputRules)Initializes OpenAPI Normalizer with a set of rules
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static OpenAPINormalizercreateNormalizer(io.swagger.v3.oas.models.OpenAPI openAPI, Map<String,String> inputRules)Factory constructor for OpenAPINormalizer.voidfixSelfReferenceSchema(String name, io.swagger.v3.oas.models.media.Schema schema)Auto fix a self referencing schema using any type to replace the self-referencing sub-item.booleangetRule(String ruleName)Get the rule.protected booleanisSelfReference(String name, io.swagger.v3.oas.models.media.Schema subSchema)protected voidmarkSchemaAsVisited(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)Add the schema to the collection of visited schemas.protected voidnormalize()Normalizes the OpenAPI input, which may not perfectly conform to the specification.protected io.swagger.v3.oas.models.media.SchemanormalizeAllOf(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)protected io.swagger.v3.oas.models.media.SchemanormalizeAllOfWithProperties(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)protected io.swagger.v3.oas.models.media.SchemanormalizeAnyOf(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)protected io.swagger.v3.oas.models.media.SchemanormalizeArraySchema(io.swagger.v3.oas.models.media.Schema schema)protected voidnormalizeBooleanSchema(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)protected io.swagger.v3.oas.models.media.SchemanormalizeComplexComposedSchema(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)protected voidnormalizeComponentsResponses()Normalizes schemas in component's responses.protected voidnormalizeComponentsSchemas()Normalizes schemas in componentsprotected voidnormalizeComponentsSecuritySchemes()Normalizes securitySchemes in componentsprotected voidnormalizeContent(io.swagger.v3.oas.models.media.Content content)Normalizes schemas in contentprotected voidnormalizeHeaders(Map<String,io.swagger.v3.oas.models.headers.Header> headers)Normalizes schemas in headersprotected voidnormalizeInfo()Pre-populate info if it's not defined.protected voidnormalizeIntegerSchema(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)protected io.swagger.v3.oas.models.media.SchemanormalizeMapSchema(io.swagger.v3.oas.models.media.Schema schema)protected io.swagger.v3.oas.models.media.SchemanormalizeOneOf(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)protected voidnormalizeOperation(io.swagger.v3.oas.models.Operation operation)Normalizes operationprotected voidnormalizeParameters(List<io.swagger.v3.oas.models.parameters.Parameter> parameters)Normalizes schemas in parametersprotected voidnormalizePaths()Normalizes inline models in Pathsprotected voidnormalizeProperties(Map<String,io.swagger.v3.oas.models.media.Schema> properties, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)protected voidnormalizeRequestBody(io.swagger.v3.oas.models.Operation operation)Normalizes schemas in RequestBodyprotected voidnormalizeResponse(io.swagger.v3.oas.models.responses.ApiResponse apiResponse)Normalizes schemas in ApiResponseprotected voidnormalizeResponses(io.swagger.v3.oas.models.Operation operation)Normalizes schemas in ApiResponsesio.swagger.v3.oas.models.media.SchemanormalizeSchema(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)Normalizes a schemaprotected io.swagger.v3.oas.models.media.SchemanormalizeSimpleSchema(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)protected voidprocessAddUnsignedToIntegerWithInvalidMaxValue(io.swagger.v3.oas.models.media.Schema schema)If the schema is integer and the max value is invalid (out of bound) then add x-unsigned to use unsigned integer/long instead.protected voidprocessFixDuplicatedOperationId(io.swagger.v3.oas.models.Operation operation)protected voidprocessKeepOnlyFirstTagInOperation(io.swagger.v3.oas.models.Operation operation)Keep only first tag in the operation if the operation has more than one tag.protected io.swagger.v3.oas.models.media.SchemaprocessNormalize31Spec(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)When set to true, normalize schema so that it works well with the generator.protected io.swagger.v3.oas.models.media.SchemaprocessRefactorAllOfWithPropertiesOnly(io.swagger.v3.oas.models.media.Schema schema)When set to true, refactor schema with allOf and properties in the same level to a schema with allOf only and the allOf contains a new schema containing the properties in the top level.protected voidprocessRemoveAnyOfOneOfAndKeepPropertiesOnly(io.swagger.v3.oas.models.media.Schema schema)If the schema contains anyOf/oneOf and properties, remove oneOf/anyOf as these serve as rules to ensure inter-dependency between properties.protected voidprocessRemoveXInternalFromOperation(io.swagger.v3.oas.models.Operation operation)Remove/hide the x-internal in operations and model.voidprocessRules(Map<String,String> inputRules)Process the rules.protected io.swagger.v3.oas.models.media.SchemaprocessSetArraytoNullable(io.swagger.v3.oas.models.media.Schema schema)Set nullable to true in array/set if needed.protected io.swagger.v3.oas.models.media.SchemaprocessSetMapToNullable(io.swagger.v3.oas.models.media.Schema schema)Set nullable to true in map if needed.protected io.swagger.v3.oas.models.media.SchemaprocessSetPrimitiveTypesToNullable(io.swagger.v3.oas.models.media.Schema schema)Set nullable to true in primitive types (e.g. string) if needed.protected voidprocessSetTagsForAllOperations(io.swagger.v3.oas.models.Operation operation)Set the tag name for all operationsprotected voidprocessSetTagsToOperationId(io.swagger.v3.oas.models.Operation operation)Set the tag name to operationId (or "default" if operationId is empty)protected voidprocessSetTagsToVendorExtension(io.swagger.v3.oas.models.Operation operation)Set the tag name to the value of the provided vendor extensionprotected io.swagger.v3.oas.models.media.SchemaprocessSimplifyAnyOf(io.swagger.v3.oas.models.media.Schema schema)If the schema is anyOf and the sub-schemas is null, set `nullable: true` instead.protected io.swagger.v3.oas.models.media.SchemaprocessSimplifyAnyOfStringAndEnumString(io.swagger.v3.oas.models.media.Schema schema)If the schema is anyOf and the sub-schemas are either string or enum of string, then simplify it to just enum of string as many generators do not yet support anyOf.protected voidprocessSimplifyBooleanEnum(io.swagger.v3.oas.models.media.Schema schema)If the schema is boolean and its enum is defined, then simply it to just boolean.protected io.swagger.v3.oas.models.media.SchemaprocessSimplifyOneOf(io.swagger.v3.oas.models.media.Schema schema)If the schema is oneOf and the sub-schemas is null, set `nullable: true` instead.protected voidprocessUseAllOfRefAsParent(io.swagger.v3.oas.models.media.Schema schema)Child schemas in `allOf` is considered a parent if it's a `$ref` (instead of inline schema).protected voidrefactorAllOfWithMetadataOnlySchemas(io.swagger.v3.oas.models.media.Schema schema)protected voidremoveUnsupportedSchemasFromAllOf(io.swagger.v3.oas.models.media.Schema schema)protected io.swagger.v3.oas.models.media.SchemasetNullable(io.swagger.v3.oas.models.media.Schema schema)protected booleanskipNormalization(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)Check if normalization is needed.
-
-
-
Method Detail
-
createNormalizer
public static OpenAPINormalizer createNormalizer(io.swagger.v3.oas.models.OpenAPI openAPI, Map<String,String> inputRules)
Factory constructor for OpenAPINormalizer. Default can be overriden by setting the NORMALIZER_CLASS rule
-
getRule
public boolean getRule(String ruleName)
Get the rule.- Parameters:
ruleName- the name of the rule- Returns:
- true if the rule is set
-
processRules
public void processRules(Map<String,String> inputRules)
Process the rules.- Parameters:
inputRules- a map of rules
-
normalize
protected void normalize()
Normalizes the OpenAPI input, which may not perfectly conform to the specification.
-
normalizeInfo
protected void normalizeInfo()
Pre-populate info if it's not defined.
-
normalizePaths
protected void normalizePaths()
Normalizes inline models in Paths
-
normalizeOperation
protected void normalizeOperation(io.swagger.v3.oas.models.Operation operation)
Normalizes operation- Parameters:
operation- Operation
-
normalizeContent
protected void normalizeContent(io.swagger.v3.oas.models.media.Content content)
Normalizes schemas in content- Parameters:
content- target content
-
normalizeRequestBody
protected void normalizeRequestBody(io.swagger.v3.oas.models.Operation operation)
Normalizes schemas in RequestBody- Parameters:
operation- target operation
-
normalizeParameters
protected void normalizeParameters(List<io.swagger.v3.oas.models.parameters.Parameter> parameters)
Normalizes schemas in parameters- Parameters:
parameters- List parameters
-
normalizeResponses
protected void normalizeResponses(io.swagger.v3.oas.models.Operation operation)
Normalizes schemas in ApiResponses- Parameters:
operation- target operation
-
normalizeResponse
protected void normalizeResponse(io.swagger.v3.oas.models.responses.ApiResponse apiResponse)
Normalizes schemas in ApiResponse- Parameters:
apiResponse- API response
-
normalizeHeaders
protected void normalizeHeaders(Map<String,io.swagger.v3.oas.models.headers.Header> headers)
Normalizes schemas in headers- Parameters:
headers- a map of headers
-
normalizeComponentsSecuritySchemes
protected void normalizeComponentsSecuritySchemes()
Normalizes securitySchemes in components
-
normalizeComponentsSchemas
protected void normalizeComponentsSchemas()
Normalizes schemas in components
-
normalizeComponentsResponses
protected void normalizeComponentsResponses()
Normalizes schemas in component's responses.
-
fixSelfReferenceSchema
public void fixSelfReferenceSchema(String name, io.swagger.v3.oas.models.media.Schema schema)
Auto fix a self referencing schema using any type to replace the self-referencing sub-item.- Parameters:
name- Schema nameschema- Schema
-
isSelfReference
protected boolean isSelfReference(String name, io.swagger.v3.oas.models.media.Schema subSchema)
-
normalizeSchema
public io.swagger.v3.oas.models.media.Schema normalizeSchema(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)Normalizes a schema- Parameters:
schema- SchemavisitedSchemas- a set of visited schemas- Returns:
- Schema
-
skipNormalization
protected boolean skipNormalization(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)Check if normalization is needed. No normalization needed if the schema is null or is a $ref or already processed.- Parameters:
schema- SchemavisitedSchemas- a set of visited schemas- Returns:
- false if normalization is needed
-
markSchemaAsVisited
protected void markSchemaAsVisited(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)Add the schema to the collection of visited schemas.- Parameters:
schema- schema to addvisitedSchemas- current visited schemas
-
normalizeArraySchema
protected io.swagger.v3.oas.models.media.Schema normalizeArraySchema(io.swagger.v3.oas.models.media.Schema schema)
-
normalizeMapSchema
protected io.swagger.v3.oas.models.media.Schema normalizeMapSchema(io.swagger.v3.oas.models.media.Schema schema)
-
normalizeSimpleSchema
protected io.swagger.v3.oas.models.media.Schema normalizeSimpleSchema(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)
-
normalizeBooleanSchema
protected void normalizeBooleanSchema(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)
-
normalizeIntegerSchema
protected void normalizeIntegerSchema(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)
-
normalizeProperties
protected void normalizeProperties(Map<String,io.swagger.v3.oas.models.media.Schema> properties, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)
-
refactorAllOfWithMetadataOnlySchemas
protected void refactorAllOfWithMetadataOnlySchemas(io.swagger.v3.oas.models.media.Schema schema)
-
removeUnsupportedSchemasFromAllOf
protected void removeUnsupportedSchemasFromAllOf(io.swagger.v3.oas.models.media.Schema schema)
-
normalizeAllOf
protected io.swagger.v3.oas.models.media.Schema normalizeAllOf(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)
-
normalizeAllOfWithProperties
protected io.swagger.v3.oas.models.media.Schema normalizeAllOfWithProperties(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)
-
normalizeOneOf
protected io.swagger.v3.oas.models.media.Schema normalizeOneOf(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)
-
normalizeAnyOf
protected io.swagger.v3.oas.models.media.Schema normalizeAnyOf(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)
-
normalizeComplexComposedSchema
protected io.swagger.v3.oas.models.media.Schema normalizeComplexComposedSchema(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)
-
processUseAllOfRefAsParent
protected void processUseAllOfRefAsParent(io.swagger.v3.oas.models.media.Schema schema)
Child schemas in `allOf` is considered a parent if it's a `$ref` (instead of inline schema).- Parameters:
schema- Schema
-
processRemoveXInternalFromOperation
protected void processRemoveXInternalFromOperation(io.swagger.v3.oas.models.Operation operation)
Remove/hide the x-internal in operations and model.- Parameters:
operation- Operation
-
processKeepOnlyFirstTagInOperation
protected void processKeepOnlyFirstTagInOperation(io.swagger.v3.oas.models.Operation operation)
Keep only first tag in the operation if the operation has more than one tag.- Parameters:
operation- Operation
-
processSetTagsForAllOperations
protected void processSetTagsForAllOperations(io.swagger.v3.oas.models.Operation operation)
Set the tag name for all operations- Parameters:
operation- Operation
-
processSetTagsToOperationId
protected void processSetTagsToOperationId(io.swagger.v3.oas.models.Operation operation)
Set the tag name to operationId (or "default" if operationId is empty)- Parameters:
operation- Operation
-
processSetTagsToVendorExtension
protected void processSetTagsToVendorExtension(io.swagger.v3.oas.models.Operation operation)
Set the tag name to the value of the provided vendor extension- Parameters:
operation- Operation
-
processFixDuplicatedOperationId
protected void processFixDuplicatedOperationId(io.swagger.v3.oas.models.Operation operation)
-
processRemoveAnyOfOneOfAndKeepPropertiesOnly
protected void processRemoveAnyOfOneOfAndKeepPropertiesOnly(io.swagger.v3.oas.models.media.Schema schema)
If the schema contains anyOf/oneOf and properties, remove oneOf/anyOf as these serve as rules to ensure inter-dependency between properties. It's a workaround as such validation is not supported at the moment.- Parameters:
schema- Schema
-
processSimplifyAnyOfStringAndEnumString
protected io.swagger.v3.oas.models.media.Schema processSimplifyAnyOfStringAndEnumString(io.swagger.v3.oas.models.media.Schema schema)
If the schema is anyOf and the sub-schemas are either string or enum of string, then simplify it to just enum of string as many generators do not yet support anyOf.- Parameters:
schema- Schema- Returns:
- Schema
-
processSimplifyOneOf
protected io.swagger.v3.oas.models.media.Schema processSimplifyOneOf(io.swagger.v3.oas.models.media.Schema schema)
If the schema is oneOf and the sub-schemas is null, set `nullable: true` instead. If there's only one sub-schema, simply return the sub-schema directly.- Parameters:
schema- Schema- Returns:
- Schema
-
processSetArraytoNullable
protected io.swagger.v3.oas.models.media.Schema processSetArraytoNullable(io.swagger.v3.oas.models.media.Schema schema)
Set nullable to true in array/set if needed.- Parameters:
schema- Schema- Returns:
- Schema
-
processSetPrimitiveTypesToNullable
protected io.swagger.v3.oas.models.media.Schema processSetPrimitiveTypesToNullable(io.swagger.v3.oas.models.media.Schema schema)
Set nullable to true in primitive types (e.g. string) if needed.- Parameters:
schema- Schema- Returns:
- Schema
-
setNullable
protected io.swagger.v3.oas.models.media.Schema setNullable(io.swagger.v3.oas.models.media.Schema schema)
-
processSetMapToNullable
protected io.swagger.v3.oas.models.media.Schema processSetMapToNullable(io.swagger.v3.oas.models.media.Schema schema)
Set nullable to true in map if needed.- Parameters:
schema- Schema- Returns:
- Schema
-
processSimplifyAnyOf
protected io.swagger.v3.oas.models.media.Schema processSimplifyAnyOf(io.swagger.v3.oas.models.media.Schema schema)
If the schema is anyOf and the sub-schemas is null, set `nullable: true` instead. If there's only one sub-schema, simply return the sub-schema directly.- Parameters:
schema- Schema- Returns:
- Schema
-
processSimplifyBooleanEnum
protected void processSimplifyBooleanEnum(io.swagger.v3.oas.models.media.Schema schema)
If the schema is boolean and its enum is defined, then simply it to just boolean.- Parameters:
schema- Schema
-
processAddUnsignedToIntegerWithInvalidMaxValue
protected void processAddUnsignedToIntegerWithInvalidMaxValue(io.swagger.v3.oas.models.media.Schema schema)
If the schema is integer and the max value is invalid (out of bound) then add x-unsigned to use unsigned integer/long instead.- Parameters:
schema- Schema
-
processRefactorAllOfWithPropertiesOnly
protected io.swagger.v3.oas.models.media.Schema processRefactorAllOfWithPropertiesOnly(io.swagger.v3.oas.models.media.Schema schema)
When set to true, refactor schema with allOf and properties in the same level to a schema with allOf only and the allOf contains a new schema containing the properties in the top level.- Parameters:
schema- Schema- Returns:
- Schema
-
processNormalize31Spec
protected io.swagger.v3.oas.models.media.Schema processNormalize31Spec(io.swagger.v3.oas.models.media.Schema schema, Set<io.swagger.v3.oas.models.media.Schema> visitedSchemas)When set to true, normalize schema so that it works well with the generator.- Parameters:
schema- SchemavisitedSchemas- a set of visited schemas- Returns:
- Schema
-
-