|
| 1 | +import type {OpenAPIV3} from "openapi-types" |
| 2 | +import {resolveLocalRef} from "./validator-artifacts.js" |
| 3 | + |
| 4 | +function isObjectRecord(value: unknown): value is Record<string, unknown> { |
| 5 | + return value !== null && typeof value === "object" && !Array.isArray(value) |
| 6 | +} |
| 7 | + |
| 8 | +function isReferenceObject(value: unknown): value is OpenAPIV3.ReferenceObject { |
| 9 | + return isObjectRecord(value) && typeof value.$ref === "string" |
| 10 | +} |
| 11 | + |
| 12 | +function extractStringLiteralValues( |
| 13 | + doc: OpenAPIV3.Document, |
| 14 | + schema: unknown, |
| 15 | +): string[] { |
| 16 | + if (!isObjectRecord(schema)) { |
| 17 | + return [] |
| 18 | + } |
| 19 | + |
| 20 | + const resolved = isReferenceObject(schema) |
| 21 | + ? resolveLocalRef<Record<string, unknown>>(doc, schema.$ref) |
| 22 | + : schema |
| 23 | + |
| 24 | + if (typeof resolved.const === "string") { |
| 25 | + return [resolved.const] |
| 26 | + } |
| 27 | + |
| 28 | + if (Array.isArray(resolved.enum) && resolved.enum.every((value) => typeof value === "string")) { |
| 29 | + return [...resolved.enum] |
| 30 | + } |
| 31 | + |
| 32 | + return [] |
| 33 | +} |
| 34 | + |
| 35 | +function inferDiscriminatorValues( |
| 36 | + doc: OpenAPIV3.Document, |
| 37 | + schema: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject, |
| 38 | + propertyName: string, |
| 39 | +): string[] { |
| 40 | + const resolved = isReferenceObject(schema) |
| 41 | + ? resolveLocalRef<Record<string, unknown>>(doc, schema.$ref) |
| 42 | + : schema as Record<string, unknown> |
| 43 | + |
| 44 | + const properties = isObjectRecord(resolved.properties) ? resolved.properties : null |
| 45 | + const directValues = extractStringLiteralValues(doc, properties?.[propertyName]) |
| 46 | + if (directValues.length > 0) { |
| 47 | + return directValues |
| 48 | + } |
| 49 | + |
| 50 | + if (!isReferenceObject(schema)) { |
| 51 | + return [] |
| 52 | + } |
| 53 | + |
| 54 | + const segments = schema.$ref.split("/") |
| 55 | + const schemaName = segments.at(-1) |
| 56 | + return schemaName ? [schemaName] : [] |
| 57 | +} |
| 58 | + |
| 59 | +type DiscriminatorBranch = { |
| 60 | + tagValues: string[] |
| 61 | + schema: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject |
| 62 | +} |
| 63 | + |
| 64 | +function getDiscriminatorBranches( |
| 65 | + doc: OpenAPIV3.Document, |
| 66 | + unionMembers: Array<OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject>, |
| 67 | + discriminator: OpenAPIV3.DiscriminatorObject, |
| 68 | +): DiscriminatorBranch[] { |
| 69 | + const mappingEntries = Object.entries(discriminator.mapping ?? {}) |
| 70 | + const branches: DiscriminatorBranch[] = [] |
| 71 | + const seenTagValues = new Set<string>() |
| 72 | + |
| 73 | + for (const member of unionMembers) { |
| 74 | + const tagValues = new Set<string>() |
| 75 | + |
| 76 | + if (isReferenceObject(member)) { |
| 77 | + for (const [tagValue, mappedRef] of mappingEntries) { |
| 78 | + if (mappedRef === member.$ref) { |
| 79 | + tagValues.add(tagValue) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if (tagValues.size === 0) { |
| 85 | + for (const tagValue of inferDiscriminatorValues(doc, member, discriminator.propertyName)) { |
| 86 | + tagValues.add(tagValue) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (tagValues.size === 0) { |
| 91 | + return [] |
| 92 | + } |
| 93 | + |
| 94 | + for (const tagValue of tagValues) { |
| 95 | + if (seenTagValues.has(tagValue)) { |
| 96 | + return [] |
| 97 | + } |
| 98 | + |
| 99 | + seenTagValues.add(tagValue) |
| 100 | + } |
| 101 | + |
| 102 | + branches.push({ |
| 103 | + tagValues: [...tagValues], |
| 104 | + schema: member, |
| 105 | + }) |
| 106 | + } |
| 107 | + |
| 108 | + return branches |
| 109 | +} |
| 110 | + |
| 111 | +function createDiscriminatorMatcher( |
| 112 | + propertyName: string, |
| 113 | + tagValues: string[], |
| 114 | +): Record<string, unknown> { |
| 115 | + return { |
| 116 | + type: "object", |
| 117 | + required: [propertyName], |
| 118 | + properties: { |
| 119 | + [propertyName]: tagValues.length === 1 ? {const: tagValues[0]} : {enum: tagValues}, |
| 120 | + }, |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +function rewriteDiscriminatorSchema( |
| 125 | + doc: OpenAPIV3.Document, |
| 126 | + schema: Record<string, unknown>, |
| 127 | +): void { |
| 128 | + const discriminator = schema.discriminator |
| 129 | + if (!isObjectRecord(discriminator) || typeof discriminator.propertyName !== "string") { |
| 130 | + return |
| 131 | + } |
| 132 | + |
| 133 | + const propertyName = discriminator.propertyName |
| 134 | + |
| 135 | + const oneOf = Array.isArray(schema.oneOf) ? schema.oneOf : null |
| 136 | + const anyOf = Array.isArray(schema.anyOf) ? schema.anyOf : null |
| 137 | + const unionMembers = oneOf ?? anyOf |
| 138 | + |
| 139 | + if (!unionMembers) { |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + const branches = getDiscriminatorBranches( |
| 144 | + doc, |
| 145 | + unionMembers as Array<OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject>, |
| 146 | + discriminator as unknown as OpenAPIV3.DiscriminatorObject, |
| 147 | + ) |
| 148 | + |
| 149 | + if (branches.length === 0) { |
| 150 | + return |
| 151 | + } |
| 152 | + |
| 153 | + const existingAllOf = Array.isArray(schema.allOf) ? [...schema.allOf] : [] |
| 154 | + const allowedTagValues = branches.flatMap((branch) => branch.tagValues) |
| 155 | + |
| 156 | + schema.allOf = [ |
| 157 | + ...existingAllOf, |
| 158 | + createDiscriminatorMatcher(propertyName, allowedTagValues), |
| 159 | + ...branches.map((branch) => ({ |
| 160 | + if: createDiscriminatorMatcher(propertyName, branch.tagValues), |
| 161 | + then: branch.schema, |
| 162 | + })), |
| 163 | + ] as Array<OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject> |
| 164 | + |
| 165 | + delete schema.discriminator |
| 166 | + delete schema.oneOf |
| 167 | + delete schema.anyOf |
| 168 | +} |
| 169 | + |
| 170 | +export function rewriteOpenApiDiscriminators(doc: OpenAPIV3.Document): void { |
| 171 | + const visit = (value: unknown) => { |
| 172 | + if (Array.isArray(value)) { |
| 173 | + for (const item of value) { |
| 174 | + visit(item) |
| 175 | + } |
| 176 | + return |
| 177 | + } |
| 178 | + |
| 179 | + if (!isObjectRecord(value)) { |
| 180 | + return |
| 181 | + } |
| 182 | + |
| 183 | + for (const child of Object.values(value)) { |
| 184 | + visit(child) |
| 185 | + } |
| 186 | + |
| 187 | + rewriteDiscriminatorSchema(doc, value) |
| 188 | + } |
| 189 | + |
| 190 | + visit(doc) |
| 191 | +} |
0 commit comments