All files / src normalize.ts

62.96% Statements 102/162
56.6% Branches 90/159
80% Functions 8/10
63.33% Lines 95/150

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360                        22x   22x 5x   17x 1x     16x       22x 17x 17x   16x           5x 5x 5x 5x 5x 5x   5x                                 5x 5x 1x       5x 5x 2x       5x 5x 2x       5x       5x                 2x 2x     2x   2x 14x 14x   2x 14x 14x     14x     14x 1x 1x 1x                                                                                                               2x 2x 14x 2x 2x 2x     2x 1x           2x 1x   2x     2x                         2x                   2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x       2x 2x 1x   1x 1x                                                     1x 1x 1x         1x           1x 1x                                                                                           8x   8x     8x 1x 1x             8x 2x 2x 2x     8x 1x   8x     8x 24x         8x            
import type { NormalizedDocument } from './types.js';
import type { WarningCollector } from './warnings.js';
 
/**
 * Detects the OpenAPI version from a parsed document and normalizes it to a
 * 3.1-like shape. Swagger 2.0 and OpenAPI 3.0 documents are transformed
 * so that downstream code only needs to handle one schema dialect.
 *
 * Uses @scalar/openapi-parser's `upgrade()` for the heavy lifting when
 * available, with manual fallbacks for edge cases.
 */
export function normalize(doc: Record<string, unknown>, warnings: WarningCollector): NormalizedDocument {
    const version = detectVersion(doc);
 
    if (version === '2.0') {
        return normalizeSwagger2(doc, warnings);
    }
    if (version === '3.0') {
        return normalizeOas30(doc as unknown as NormalizedDocument, warnings);
    }
    // 3.1+ — already in target shape
    return doc as unknown as NormalizedDocument;
}
 
function detectVersion(doc: Record<string, unknown>): '2.0' | '3.0' | '3.1' {
    if (typeof doc.swagger === 'string' && doc.swagger.startsWith('2')) return '2.0';
    Eif (typeof doc.openapi === 'string') {
        if (doc.openapi.startsWith('3.0')) return '3.0';
    }
    return '3.1';
}
 
// ─── Swagger 2.0 → 3.1 ───────────────────────────────────────────────────
 
function normalizeSwagger2(doc: Record<string, unknown>, warnings: WarningCollector): NormalizedDocument {
    const info = (doc.info as Record<string, unknown>) ?? { title: 'Untitled', version: '0.0.0' };
    const basePath = (doc.basePath as string) ?? '';
    const schemes = (doc.schemes as string[]) ?? ['https'];
    const host = (doc.host as string) ?? 'localhost';
    const globalConsumes = (doc.consumes as string[]) ?? ['application/json'];
    const globalProduces = (doc.produces as string[]) ?? ['application/json'];
 
    const result: NormalizedDocument = {
        openapi: '3.1.0',
        info: {
            title: (info.title as string) ?? 'Untitled',
            version: (info.version as string) ?? '0.0.0',
            description: info.description as string | undefined,
        },
        servers: [{ url: `${schemes[0]}://${host}${basePath}` }],
        paths: {},
        components: {
            schemas: {},
            securitySchemes: {},
        },
        tags: (doc.tags as NormalizedDocument['tags']) ?? [],
    };
 
    // Convert definitions → components/schemas
    const definitions = (doc.definitions as Record<string, unknown>) ?? {};
    for (const [name, schema] of Object.entries(definitions)) {
        result.components!.schemas![name] = normalizeNullable30(schema as Record<string, unknown>);
    }
 
    // Convert securityDefinitions → components/securitySchemes
    const secDefs = (doc.securityDefinitions as Record<string, unknown>) ?? {};
    for (const [name, scheme] of Object.entries(secDefs)) {
        result.components!.securitySchemes![name] = convertSecurityScheme2(scheme as Record<string, unknown>);
    }
 
    // Convert paths
    const paths = (doc.paths as Record<string, Record<string, unknown>>) ?? {};
    for (const [path, pathItem] of Object.entries(paths)) {
        result.paths![path] = normalizePathItem2(pathItem, globalConsumes, globalProduces, warnings);
    }
 
    // Global security
    Iif (doc.security) {
        result.security = doc.security as Record<string, string[]>[];
    }
 
    return result;
}
 
function normalizePathItem2(
    pathItem: Record<string, unknown>,
    globalConsumes: string[],
    globalProduces: string[],
    warnings: WarningCollector,
): Record<string, unknown> {
    const methods = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options'];
    const normalized: Record<string, unknown> = {};
 
    // Path-level parameters
    const pathParams = (pathItem.parameters as unknown[]) ?? [];
 
    for (const method of methods) {
        const op = pathItem[method] as Record<string, unknown> | undefined;
        if (!op) continue;
 
        const opConsumes = (op.consumes as string[]) ?? globalConsumes;
        const opProduces = (op.produces as string[]) ?? globalProduces;
        const params = [...pathParams, ...((op.parameters as unknown[]) ?? [])];
 
        // Separate body params from others
        const nonBodyParams: unknown[] = [];
        let requestBody: Record<string, unknown> | undefined;
 
        for (const param of params as Record<string, unknown>[]) {
            if (param.in === 'body') {
                const contentType = opConsumes[0] ?? 'application/json';
                requestBody = {
                    description: param.description,
                    required: param.required ?? true,
                    content: {
                        [contentType]: {
                            schema: normalizeNullable30((param.schema as Record<string, unknown>) ?? {}),
                        },
                    },
                };
            E} else if (param.in === 'formData') {
                warnings.info(`#/paths/${encodePathSegment(method)}`, 'formData parameters converted to multipart/form-data requestBody');
                // Collect formData params into a schema
                if (!requestBody) {
                    requestBody = {
                        content: {
                            'multipart/form-data': {
                                schema: { type: 'object', properties: {}, required: [] as string[] },
                            },
                        },
                    };
                }
                const formSchema = (requestBody.content as Record<string, Record<string, unknown>>)['multipart/form-data']!.schema as Record<
                    string,
                    unknown
                >;
                const props = formSchema.properties as Record<string, unknown>;
                props[param.name as string] = normalizeNullable30(param as Record<string, unknown>);
                if (param.required) {
                    (formSchema.required as string[]).push(param.name as string);
                }
            } else {
                // Convert param schema
                const normalizedParam = { ...param };
                if (param.type) {
                    normalizedParam.schema = normalizeNullable30({
                        type: param.type,
                        format: param.format,
                        enum: param.enum,
                        items: param.items,
                        default: param.default,
                        minimum: param.minimum,
                        maximum: param.maximum,
                        minLength: param.minLength,
                        maxLength: param.maxLength,
                        pattern: param.pattern,
                    } as Record<string, unknown>);
                    delete normalizedParam.type;
                    delete normalizedParam.format;
                    delete normalizedParam.enum;
                    delete normalizedParam.items;
                }
                nonBodyParams.push(normalizedParam);
            }
        }
 
        // Convert responses
        const responses: Record<string, unknown> = {};
        const opResponses = (op.responses as Record<string, Record<string, unknown>>) ?? {};
        for (const [code, resp] of Object.entries(opResponses)) {
            const contentType = opProduces[0] ?? 'application/json';
            const headers = convertResponseHeaders2(resp.headers as Record<string, Record<string, unknown>> | undefined);
            const responseEntry: Record<string, unknown> = {
                description: resp.description ?? '',
            };
            if (resp.schema) {
                responseEntry.content = {
                    [contentType]: {
                        schema: normalizeNullable30(resp.schema as Record<string, unknown>),
                    },
                };
            }
            if (headers) {
                responseEntry.headers = headers;
            }
            responses[code] = responseEntry;
        }
 
        normalized[method] = {
            operationId: op.operationId,
            summary: op.summary,
            description: op.description,
            tags: op.tags,
            parameters: nonBodyParams.length > 0 ? nonBodyParams : undefined,
            requestBody,
            responses,
            security: op.security,
            deprecated: op.deprecated,
        };
    }
 
    return normalized;
}
 
/**
 * Convert Swagger 2.0 response headers to a 3.x-shaped Header Object map.
 * 2.0 stores `type`/`format`/`items` inline on the header; 3.x wraps the same fields under `schema`.
 */
function convertResponseHeaders2(
    headers: Record<string, Record<string, unknown>> | undefined,
): Record<string, Record<string, unknown>> | undefined {
    if (!headers) return undefined;
    const out: Record<string, Record<string, unknown>> = {};
    for (const [name, header] of Object.entries(headers)) {
        Iif (!header || typeof header !== 'object') continue;
        const { description, type, format, items, ...rest } = header;
        const schema: Record<string, unknown> = { ...rest };
        Eif (type !== undefined) schema.type = type;
        Iif (format !== undefined) schema.format = format;
        Iif (items !== undefined) schema.items = items;
        const normalized: Record<string, unknown> = {};
        Eif (description !== undefined) normalized.description = description;
        Eif (Object.keys(schema).length > 0) normalized.schema = normalizeNullable30(schema);
        out[name] = normalized;
    }
    return Object.keys(out).length > 0 ? out : undefined;
}
 
function convertSecurityScheme2(scheme: Record<string, unknown>): unknown {
    const type = scheme.type as string;
    if (type === 'basic') {
        return { type: 'http', scheme: 'basic' };
    }
    Eif (type === 'apiKey') {
        return { type: 'apiKey', name: scheme.name, in: scheme.in };
    }
    if (type === 'oauth2') {
        const flow = scheme.flow as string;
        const flows: Record<string, unknown> = {};
        if (flow === 'implicit') {
            flows.implicit = { authorizationUrl: scheme.authorizationUrl, scopes: scheme.scopes ?? {} };
        } else if (flow === 'password') {
            flows.password = { tokenUrl: scheme.tokenUrl, scopes: scheme.scopes ?? {} };
        } else if (flow === 'application') {
            flows.clientCredentials = { tokenUrl: scheme.tokenUrl, scopes: scheme.scopes ?? {} };
        } else if (flow === 'accessCode') {
            flows.authorizationCode = {
                authorizationUrl: scheme.authorizationUrl,
                tokenUrl: scheme.tokenUrl,
                scopes: scheme.scopes ?? {},
            };
        }
        return { type: 'oauth2', flows };
    }
    return scheme;
}
 
// ─── OpenAPI 3.0 → 3.1 ───────────────────────────────────────────────────
 
function normalizeOas30(doc: NormalizedDocument, _warnings: WarningCollector): NormalizedDocument {
    // Walk all schemas and convert `nullable: true` to type arrays
    Eif (doc.components?.schemas) {
        for (const [name, schema] of Object.entries(doc.components.schemas)) {
            doc.components.schemas[name] = normalizeNullable30(schema as Record<string, unknown>);
        }
    }
 
    // Walk paths and normalize inline schemas
    Iif (doc.paths) {
        for (const pathItem of Object.values(doc.paths)) {
            normalizePathItemSchemas(pathItem as Record<string, unknown>);
        }
    }
 
    doc.openapi = '3.1.0';
    return doc;
}
 
function normalizePathItemSchemas(pathItem: Record<string, unknown>): void {
    const methods = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options'];
    for (const method of methods) {
        const op = pathItem[method] as Record<string, unknown> | undefined;
        if (!op) continue;
 
        // Normalize parameter schemas
        const params = (op.parameters as Record<string, unknown>[]) ?? [];
        for (const param of params) {
            if (param.schema) {
                param.schema = normalizeNullable30(param.schema as Record<string, unknown>);
            }
        }
 
        // Normalize requestBody schemas
        const reqBody = op.requestBody as Record<string, unknown> | undefined;
        if (reqBody?.content) {
            for (const mediaType of Object.values(reqBody.content as Record<string, Record<string, unknown>>)) {
                if (mediaType.schema) {
                    mediaType.schema = normalizeNullable30(mediaType.schema as Record<string, unknown>);
                }
            }
        }
 
        // Normalize response schemas
        const responses = (op.responses as Record<string, Record<string, unknown>>) ?? {};
        for (const resp of Object.values(responses)) {
            if (resp.content) {
                for (const mediaType of Object.values(resp.content as Record<string, Record<string, unknown>>)) {
                    if (mediaType.schema) {
                        mediaType.schema = normalizeNullable30(mediaType.schema as Record<string, unknown>);
                    }
                }
            }
        }
    }
}
 
/**
 * Recursively converts OAS 3.0 `nullable: true` to OAS 3.1 `type: [T, "null"]`.
 * Also normalizes nested schemas (properties, items, allOf, etc.).
 */
function normalizeNullable30(schema: Record<string, unknown>): Record<string, unknown> {
    Iif (!schema || typeof schema !== 'object') return schema;
 
    const result = { ...schema };
 
    // Convert nullable: true → type array with null
    if (result.nullable === true && typeof result.type === 'string') {
        result.type = [result.type, 'null'];
        delete result.nullable;
    }
 
    // Convert $ref alongside other properties (OAS 3.0 $ref with siblings was invalid,
    // but OAS 3.1 allows it — no conversion needed, just keep walking)
 
    // Recurse into nested schemas
    if (result.properties && typeof result.properties === 'object') {
        const props = result.properties as Record<string, Record<string, unknown>>;
        for (const [key, val] of Object.entries(props)) {
            props[key] = normalizeNullable30(val);
        }
    }
    if (result.items && typeof result.items === 'object' && !Array.isArray(result.items)) {
        result.items = normalizeNullable30(result.items as Record<string, unknown>);
    }
    Iif (result.additionalProperties && typeof result.additionalProperties === 'object') {
        result.additionalProperties = normalizeNullable30(result.additionalProperties as Record<string, unknown>);
    }
    for (const combiner of ['allOf', 'oneOf', 'anyOf'] as const) {
        Iif (Array.isArray(result[combiner])) {
            result[combiner] = (result[combiner] as Record<string, unknown>[]).map(normalizeNullable30);
        }
    }
 
    return result;
}
 
function encodePathSegment(s: string): string {
    return s.replace(/~/g, '~0').replace(/\//g, '~1');
}