|
1 | 1 | import { parseRef } from "@redocly/openapi-core/lib/ref-utils.js";
|
| 2 | +import { Referenced, OasRef} from "@redocly/openapi-core"; |
2 | 3 | import ts, { type LiteralTypeNode, type TypeLiteralNode } from "typescript";
|
| 4 | +import { ParameterObject, ReferenceObject } from "../types.js"; |
3 | 5 |
|
4 | 6 | export const JS_PROPERTY_INDEX_RE = /^[A-Za-z_$][A-Za-z_$0-9]*$/;
|
5 | 7 | export const JS_ENUM_INVALID_CHARS_RE = /[^A-Za-z_$0-9]+(.)?/g;
|
@@ -115,33 +117,77 @@ export function addJSDocComment(schemaObject: AnnotatedSchemaObject, node: ts.Pr
|
115 | 117 | }
|
116 | 118 | }
|
117 | 119 |
|
118 |
| -/** Convert OpenAPI ref into TS indexed access node (ex: `components["schemas"]["Foo"]`) */ |
119 |
| -export function oapiRef(path: string): ts.TypeNode { |
| 120 | +function isOasRef<T>(obj: Referenced<T>): obj is OasRef { |
| 121 | + return Boolean((obj as OasRef).$ref); |
| 122 | +} |
| 123 | +type OapiRefResolved = ParameterObject | ReferenceObject; |
| 124 | + |
| 125 | +function isParameterObject(obj: OapiRefResolved | undefined): obj is ParameterObject { |
| 126 | + return Boolean(obj && !isOasRef(obj) && obj.in); |
| 127 | +} |
| 128 | + |
| 129 | +function addIndexedAccess(node: ts.TypeReferenceNode | ts.IndexedAccessTypeNode, ...segments: readonly string[]) { |
| 130 | + return segments.reduce((acc, segment) => { |
| 131 | + return ts.factory.createIndexedAccessTypeNode( |
| 132 | + acc, |
| 133 | + ts.factory.createLiteralTypeNode( |
| 134 | + typeof segment === "number" |
| 135 | + ? ts.factory.createNumericLiteral(segment) |
| 136 | + : ts.factory.createStringLiteral(segment), |
| 137 | + ), |
| 138 | + ); |
| 139 | + }, node); |
| 140 | +} |
| 141 | + |
| 142 | +/** |
| 143 | + * Convert OpenAPI ref into TS indexed access node (ex: `components["schemas"]["Foo"]`) |
| 144 | + * `path` is a JSON Pointer to a location within an OpenAPI document. |
| 145 | + * Transform it into a TypeScript type reference into the generated types. |
| 146 | + * |
| 147 | + * In most cases the structures of the openapi-typescript generated types and the |
| 148 | + * JSON Pointer paths into the OpenAPI document are the same. However, in some cases |
| 149 | + * special transformations are necessary to account for the ways they differ. |
| 150 | + * * Object schemas |
| 151 | + * $refs into the `properties` of object schemas are valid, but openapi-typescript |
| 152 | + * flattens these objects, so we omit so the index into the schema skips ["properties"] |
| 153 | + * * Parameters |
| 154 | + * $refs into the `parameters` of paths are valid, but openapi-ts represents |
| 155 | + * them according to their type; path, query, header, etc… so in these cases we |
| 156 | + * must check the parameter definition to determine the how to index into |
| 157 | + * the openapi-typescript type. |
| 158 | + **/ |
| 159 | +export function oapiRef(path: string, resolved?: OapiRefResolved): ts.TypeNode { |
120 | 160 | const { pointer } = parseRef(path);
|
121 | 161 | if (pointer.length === 0) {
|
122 | 162 | throw new Error(`Error parsing $ref: ${path}. Is this a valid $ref?`);
|
123 | 163 | }
|
124 |
| - let t: ts.TypeReferenceNode | ts.IndexedAccessTypeNode = ts.factory.createTypeReferenceNode( |
125 |
| - ts.factory.createIdentifier(String(pointer[0])), |
| 164 | + |
| 165 | + const parametersObject = isParameterObject(resolved); |
| 166 | + |
| 167 | + // Initial segments are handled in a fixed , then remaining segments are treated |
| 168 | + // according to heuristics based on the initial segments |
| 169 | + const initialSegment = pointer[0]; |
| 170 | + const leadingSegments = pointer.slice(1, 3); |
| 171 | + const restSegments = pointer.slice(3); |
| 172 | + |
| 173 | + const leadingType = addIndexedAccess( |
| 174 | + ts.factory.createTypeReferenceNode(ts.factory.createIdentifier(String(initialSegment))), |
| 175 | + ...leadingSegments, |
126 | 176 | );
|
127 |
| - if (pointer.length > 1) { |
128 |
| - for (let i = 1; i < pointer.length; i++) { |
129 |
| - // Skip `properties` items when in the middle of the pointer |
130 |
| - // See: https://github.com/openapi-ts/openapi-typescript/issues/1742 |
131 |
| - if (i > 2 && i < pointer.length - 1 && pointer[i] === "properties") { |
132 |
| - continue; |
133 |
| - } |
134 |
| - t = ts.factory.createIndexedAccessTypeNode( |
135 |
| - t, |
136 |
| - ts.factory.createLiteralTypeNode( |
137 |
| - typeof pointer[i] === "number" |
138 |
| - ? ts.factory.createNumericLiteral(pointer[i]) |
139 |
| - : ts.factory.createStringLiteral(pointer[i] as string), |
140 |
| - ), |
141 |
| - ); |
| 177 | + |
| 178 | + return restSegments.reduce<ts.TypeReferenceNode | ts.IndexedAccessTypeNode>((acc, segment, index, original) => { |
| 179 | + // Skip `properties` items when in the middle of the pointer |
| 180 | + // See: https://github.com/openapi-ts/openapi-typescript/issues/1742 |
| 181 | + if (segment === "properties") { |
| 182 | + return acc; |
142 | 183 | }
|
143 |
| - } |
144 |
| - return t; |
| 184 | + |
| 185 | + if (parametersObject && index === original.length - 1) { |
| 186 | + return addIndexedAccess(acc, resolved.in, resolved.name); |
| 187 | + } |
| 188 | + |
| 189 | + return addIndexedAccess(acc, segment); |
| 190 | + }, leadingType); |
145 | 191 | }
|
146 | 192 |
|
147 | 193 | export interface AstToStringOptions {
|
|
0 commit comments