Skip to content

Commit

Permalink
core: Fix handling of OAI 3.1 nullable schema references using anyOf
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
karlvr committed Aug 21, 2024
1 parent c3d8509 commit cbd8017
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 8 additions & 0 deletions .changeset/stupid-moons-eat.md
Original file line number Diff line number Diff line change
@@ -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.
14 changes: 10 additions & 4 deletions packages/core/src/process/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CodegenSchemaInfo> = {}

Expand All @@ -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)
Expand Down Expand Up @@ -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<CodegenSchemaInfo>, state: InternalCodegenState): OpenAPIX.SchemaObject {
function fixApiSchema(apiSchema: OpenAPIX.SchemaObject, $ref: string | undefined, usageInfo: Partial<CodegenSchemaInfo>, 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) {
Expand All @@ -295,6 +297,10 @@ function fixApiSchema(apiSchema: OpenAPIX.SchemaObject, usageInfo: Partial<Codeg
*/
const originalApiSchema = apiSchema

if (isOpenAPIReferenceObject(otherTypes[0])) {
/* As we're unwrapping this redundant anyOf we need to also update the $ref to point to our new target schema */
$ref = otherTypes[0].$ref
}
apiSchema = resolveReference(otherTypes[0], state)

/* Mark this usage as nullable */
Expand Down Expand Up @@ -361,7 +367,7 @@ function fixApiSchema(apiSchema: OpenAPIX.SchemaObject, usageInfo: Partial<Codeg
}
}

return apiSchema
return { apiSchema, $ref }
}

export type DiscoverSchemasTestFunc = (apiSchema: OpenAPIX.SchemaObject, state: InternalCodegenState) => boolean
Expand Down

0 comments on commit cbd8017

Please sign in to comment.