diff --git a/.changeset/stupid-moons-eat.md b/.changeset/stupid-moons-eat.md new file mode 100644 index 0000000..fcbb4e9 --- /dev/null +++ b/.changeset/stupid-moons-eat.md @@ -0,0 +1,8 @@ +--- +"@openapi-generator-plus/core": patch +--- + +Fix handling of OAI 3.1 nullable schema references using `anyOf` + +The generator did not properly use the referenced schema so it would not be generated as a top-level schema +and its name would not be used. diff --git a/packages/core/src/process/schema/index.ts b/packages/core/src/process/schema/index.ts index 75c9ee9..1641287 100644 --- a/packages/core/src/process/schema/index.ts +++ b/packages/core/src/process/schema/index.ts @@ -59,7 +59,7 @@ export interface SchemaUsageOptions { } export function toCodegenSchemaUsage(apiSchema: OpenAPIX.SchemaObject | OpenAPIX.ReferenceObject, state: InternalCodegenState, options: SchemaUsageOptions): CodegenSchemaUsage { - const $ref = isOpenAPIReferenceObject(apiSchema) ? apiSchema.$ref : undefined + let $ref = isOpenAPIReferenceObject(apiSchema) ? apiSchema.$ref : undefined const usageInfo: Partial = {} @@ -75,7 +75,9 @@ export function toCodegenSchemaUsage(apiSchema: OpenAPIX.SchemaObject | OpenAPIX } apiSchema = resolveReference(apiSchema, state) - apiSchema = fixApiSchema(apiSchema, usageInfo, state) + const fixed = fixApiSchema(apiSchema, $ref, usageInfo, state) + apiSchema = fixed.apiSchema + $ref = fixed.$ref /* Check if we've already generated this schema, and return it */ const existing = findKnownSchema(apiSchema, $ref, state) @@ -284,7 +286,7 @@ function supportedNamedSchema(schemaType: CodegenSchemaType, referenced: boolean * fixed schema but NOT the fixed usageInfo. * @param apiSchema */ -function fixApiSchema(apiSchema: OpenAPIX.SchemaObject, usageInfo: Partial, state: InternalCodegenState): OpenAPIX.SchemaObject { +function fixApiSchema(apiSchema: OpenAPIX.SchemaObject, $ref: string | undefined, usageInfo: Partial, state: InternalCodegenState): { apiSchema: OpenAPIX.SchemaObject; $ref: string | undefined } { if (apiSchema.anyOf && apiSchema.anyOf.length > 1) { const nullIndex = (apiSchema.anyOf as OpenAPIX.SchemaObject[]).findIndex(s => s.type === 'null') if (nullIndex !== -1) { @@ -295,6 +297,10 @@ function fixApiSchema(apiSchema: OpenAPIX.SchemaObject, usageInfo: Partial boolean