Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/salty-results-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": major
---

⚠️ Breaking change: Update Redocly CLI to 2.x. See https://redocly.com/docs/cli/guides/migrate-to-v2 for migration guide.
2 changes: 1 addition & 1 deletion packages/openapi-typescript/biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"$schema": "https://biomejs.dev/schemas/2.2.4/schema.json",
"extends": "//",
"files": {
"includes": ["bin/**", "!examples/**", "src/**", "test/**", "!**/fixtures/**/*"]
"includes": ["bin/**", "!examples/**", "src/**", "test/**", "!**/fixtures/**/*", "!src/lib/redocly.ts"]
}
}
3 changes: 2 additions & 1 deletion packages/openapi-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
"typescript": "^5.x"
},
"dependencies": {
"@redocly/openapi-core": "^1.34.5",
"@redocly/openapi-core": "^2.1.2",
"ajv": "^8.17.1",
"ansi-colors": "^4.1.3",
"change-case": "^5.4.4",
"parse-json": "^8.3.0",
Expand Down
231 changes: 224 additions & 7 deletions packages/openapi-typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { performance } from "node:perf_hooks";
import type { Readable } from "node:stream";
import { createConfig } from "@redocly/openapi-core";
import type { RawGovernanceConfig } from "@redocly/openapi-core/lib/config/config";
import type ts from "typescript";
import { validateAndBundle } from "./lib/redoc.js";
import { debug, resolveRef, scanDiscriminators } from "./lib/utils.js";
Expand Down Expand Up @@ -51,14 +52,13 @@ export default async function openapiTS(

const redoc =
options.redocly ??
(await createConfig(
{
rules: {
"operation-operationId-unique": { severity: "error" }, // throw error on duplicate operationIDs
},
(await createConfig({
...REDOCLY_MINIMAL_CONFIG,
rules: {
...REDOCLY_MINIMAL_CONFIG.rules,
"operation-operationId-unique": { severity: "error" }, // throw error on duplicate operationIDs
},
{ extends: ["minimal"] },
));
}));

const schema = await validateAndBundle(source, {
redoc,
Expand Down Expand Up @@ -102,3 +102,220 @@ export default async function openapiTS(

return result;
}

export const REDOCLY_MINIMAL_CONFIG: RawGovernanceConfig<"built-in"> = {
rules: {
struct: "error",
"no-unresolved-refs": "error",
},
oas2Rules: {
"boolean-parameter-prefixes": "off",
"info-contact": "off",
"info-license": "off",
"info-license-strict": "off",
"no-path-trailing-slash": "warn",
"no-identical-paths": "warn",
"no-ambiguous-paths": "warn",
"no-invalid-schema-examples": "off",
"no-invalid-parameter-examples": "off",
"no-http-verbs-in-paths": "off",
"no-enum-type-mismatch": "warn",
"no-required-schema-properties-undefined": "warn",
"no-schema-type-mismatch": "warn",
"operation-summary": "warn",
"operation-operationId": "warn",
"operation-operationId-unique": "warn",
"operation-operationId-url-safe": "warn",
"operation-description": "off",
"operation-2xx-response": "warn",
"operation-4xx-response": "off",
"operation-parameters-unique": "warn",
"operation-tag-defined": "off",
"operation-singular-tag": "off",
"parameter-description": "off",
"path-declaration-must-exist": "warn",
"path-not-include-query": "warn",
"path-parameters-defined": "warn",
"paths-kebab-case": "off",
"path-http-verbs-order": "off",
"path-params-defined": "off",
"path-segment-plural": "off",
"required-string-property-missing-min-length": "off",
"response-contains-header": "off",
"request-mime-type": "off",
"response-contains-property": "off",
"response-mime-type": "off",
"security-defined": "warn",
"spec-strict-refs": "off",
"scalar-property-missing-example": "off",
"tag-description": "warn",
"tags-alphabetical": "off",
"no-duplicated-tag-names": "off",
},
oas3_0Rules: {
"array-parameter-serialization": "off",
"boolean-parameter-prefixes": "off",
"component-name-unique": "off",
"info-contact": "off",
"info-license": "off",
"info-license-strict": "off",
"no-ambiguous-paths": "warn",
"no-path-trailing-slash": "warn",
"no-identical-paths": "warn",
"no-invalid-schema-examples": "off",
"no-invalid-parameter-examples": "off",
"no-http-verbs-in-paths": "off",
"no-enum-type-mismatch": "warn",
"no-required-schema-properties-undefined": "warn",
"no-schema-type-mismatch": "warn",
"no-invalid-media-type-examples": {
severity: "warn",
allowAdditionalProperties: false,
},
"no-server-example.com": "warn",
"no-server-trailing-slash": "error",
"no-empty-servers": "warn",
"no-example-value-and-externalValue": "warn",
"no-unused-components": "warn",
"no-undefined-server-variable": "warn",
"no-server-variables-empty-enum": "error",
"nullable-type-sibling": "warn",
"operation-summary": "warn",
"operation-operationId": "warn",
"operation-operationId-unique": "warn",
"operation-operationId-url-safe": "warn",
"operation-description": "off",
"operation-2xx-response": "warn",
"operation-4xx-response": "off",
"operation-4xx-problem-details-rfc7807": "off",
"operation-parameters-unique": "warn",
"operation-tag-defined": "off",
"operation-singular-tag": "off",
"parameter-description": "off",
"path-declaration-must-exist": "warn",
"path-not-include-query": "warn",
"path-parameters-defined": "warn",
"paths-kebab-case": "off",
"path-http-verbs-order": "off",
"path-params-defined": "off",
"path-segment-plural": "off",
"required-string-property-missing-min-length": "off",
"response-contains-header": "off",
"request-mime-type": "off",
"response-contains-property": "off",
"response-mime-type": "off",
"security-defined": "warn",
"spec-strict-refs": "off",
"scalar-property-missing-example": "off",
"spec-components-invalid-map-name": "warn",
"tag-description": "warn",
"tags-alphabetical": "off",
"no-duplicated-tag-names": "off",
},
oas3_1Rules: {
"array-parameter-serialization": "off",
"boolean-parameter-prefixes": "off",
"component-name-unique": "off",
"info-contact": "off",
"info-license": "off",
"info-license-strict": "off",
"no-path-trailing-slash": "warn",
"no-identical-paths": "warn",
"no-ambiguous-paths": "warn",
"no-invalid-schema-examples": "off",
"no-invalid-parameter-examples": "off",
"no-http-verbs-in-paths": "off",
"no-enum-type-mismatch": "warn",
"no-required-schema-properties-undefined": "warn",
"no-schema-type-mismatch": "warn",
"no-invalid-media-type-examples": "warn",
"no-server-example.com": "warn",
"no-server-trailing-slash": "error",
"no-empty-servers": "warn",
"no-example-value-and-externalValue": "warn",
"no-unused-components": "warn",
"no-undefined-server-variable": "warn",
"no-server-variables-empty-enum": "error",
"operation-summary": "warn",
"operation-operationId": "warn",
"operation-operationId-unique": "warn",
"operation-operationId-url-safe": "warn",
"operation-description": "off",
"operation-2xx-response": "warn",
"operation-4xx-response": "off",
"operation-4xx-problem-details-rfc7807": "off",
"operation-parameters-unique": "warn",
"operation-tag-defined": "off",
"operation-singular-tag": "off",
"parameter-description": "off",
"path-declaration-must-exist": "warn",
"path-not-include-query": "warn",
"path-parameters-defined": "warn",
"paths-kebab-case": "off",
"path-http-verbs-order": "off",
"path-params-defined": "off",
"path-segment-plural": "off",
"required-string-property-missing-min-length": "off",
"response-contains-header": "off",
"request-mime-type": "off",
"response-contains-property": "off",
"response-mime-type": "off",
"security-defined": "warn",
"spec-strict-refs": "off",
"scalar-property-missing-example": "off",
"spec-components-invalid-map-name": "warn",
"tag-description": "warn",
"tags-alphabetical": "off",
"no-duplicated-tag-names": "off",
},
async2Rules: {
"channels-kebab-case": "off",
"info-contact": "off",
"info-license-strict": "off",
"no-channel-trailing-slash": "off",
"operation-operationId": "warn",
"tag-description": "warn",
"tags-alphabetical": "off",
"no-duplicated-tag-names": "off",
"no-required-schema-properties-undefined": "warn",
"no-enum-type-mismatch": "warn",
"no-schema-type-mismatch": "warn",
},
async3Rules: {
"channels-kebab-case": "off",
"info-contact": "off",
"info-license-strict": "off",
"no-channel-trailing-slash": "off",
"operation-operationId": "warn",
"tag-description": "warn",
"tags-alphabetical": "off",
"no-duplicated-tag-names": "off",
"no-required-schema-properties-undefined": "warn",
"no-enum-type-mismatch": "warn",
"no-schema-type-mismatch": "warn",
},
arazzo1Rules: {
"criteria-unique": "off",
"no-criteria-xpath": "off",
"parameters-unique": "off",
"requestBody-replacements-unique": "off",
"sourceDescription-type": "off",
"sourceDescriptions-not-empty": "off",
"step-onSuccess-unique": "off",
"step-onFailure-unique": "off",
"stepId-unique": "error",
"sourceDescription-name-unique": "off",
"respect-supported-versions": "off",
"workflowId-unique": "error",
"workflow-dependsOn": "off",
"no-x-security-scheme-name-without-openapi": "off",
"x-security-scheme-required-values": "off",
"no-x-security-scheme-name-in-workflow": "off",
"no-required-schema-properties-undefined": "warn",
"no-enum-type-mismatch": "warn",
"no-schema-type-mismatch": "warn",
},
overlay1Rules: {
"info-contact": "off",
},
};
14 changes: 7 additions & 7 deletions packages/openapi-typescript/src/lib/redoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function _processProblems(problems: NormalizedProblem[], options: { silent: bool
export async function validateAndBundle(
source: string | URL | OpenAPI3 | Readable | Buffer,
options: ValidateAndBundleOptions,
) {
): Promise<OpenAPI3> {
const redocConfigT = performance.now();
debug("Loaded Redoc config", "redoc", performance.now() - redocConfigT);
const redocParseT = performance.now();
Expand All @@ -116,22 +116,22 @@ export async function validateAndBundle(
absoluteRef = source.protocol === "file:" ? fileURLToPath(source) : source.href;
}
const resolver = new BaseResolver(options.redoc.resolve);
const document = await parseSchema(source, {
const document = (await parseSchema(source, {
absoluteRef,
resolver,
});
})) as Document<OpenAPI3>;
debug("Parsed schema", "redoc", performance.now() - redocParseT);

// 1. check for OpenAPI 3 or greater
const openapiVersion = Number.parseFloat(document.parsed.openapi);
if (
document.parsed.swagger ||
(document.parsed as any).swagger ||
!document.parsed.openapi ||
Number.isNaN(openapiVersion) ||
openapiVersion < 3 ||
openapiVersion >= 4
) {
if (document.parsed.swagger) {
if ((document.parsed as any).swagger) {
throw new Error("Unsupported Swagger version: 2.x. Use OpenAPI 3.x instead.");
}
if (document.parsed.openapi || openapiVersion < 3 || openapiVersion >= 4) {
Expand All @@ -144,7 +144,7 @@ export async function validateAndBundle(
const redocLintT = performance.now();
const problems = await lintDocument({
document,
config: options.redoc.styleguide,
config: options.redoc,
externalRefResolver: resolver,
});
_processProblems(problems, options);
Expand All @@ -160,5 +160,5 @@ export async function validateAndBundle(
_processProblems(bundled.problems, options);
debug("Bundled schema", "bundle", performance.now() - redocBundleT);

return bundled.bundle.parsed;
return bundled.bundle.parsed as OpenAPI3;
}
33 changes: 33 additions & 0 deletions packages/openapi-typescript/src/lib/redocly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2019 Redocly Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
export type Falsy = undefined | null | false | '' | 0;

export function isTruthy<Truthy>(value: Truthy | Falsy): value is Truthy {
return !!value;
}

export function parseRef(ref: string): { uri: string | null; pointer: string[] } {
const [uri, pointer = ''] = ref.split('#/');
return {
uri: (uri.endsWith('#') ? uri.slice(0, -1) : uri) || null,
pointer: parsePointer(pointer),
};
}

export function parsePointer(pointer: string) {
return pointer.split('/').map(unescapePointer).filter(isTruthy);
}

export function unescapePointer(fragment: string): string {
return decodeURIComponent(fragment.replace(/~1/g, '/').replace(/~0/g, '~'));
}

export function escapePointer<T extends string | number>(fragment: T): T {
if (typeof fragment === 'number') return fragment;
return (fragment as string).replace(/~/g, '~0').replace(/\//g, '~1') as T;
}
2 changes: 1 addition & 1 deletion packages/openapi-typescript/src/lib/ts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { OasRef, Referenced } from "@redocly/openapi-core";
import { parseRef } from "@redocly/openapi-core/lib/ref-utils.js";
import ts, { type LiteralTypeNode, type TypeLiteralNode } from "typescript";
import type { ParameterObject } from "../types.js";
import { parseRef } from "./redocly.js";

export const JS_PROPERTY_INDEX_RE = /^[A-Za-z_$][A-Za-z_$0-9]*$/;
export const JS_ENUM_INVALID_CHARS_RE = /[^A-Za-z_$0-9]+(.)?/g;
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-typescript/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { escapePointer, parseRef } from "@redocly/openapi-core/lib/ref-utils.js";
import c from "ansi-colors";
import supportsColor from "supports-color";
import ts from "typescript";
import { escapePointer, parseRef } from "../lib/redocly.js";
import type { DiscriminatorObject, OpenAPI3, OpenAPITSOptions, ReferenceObject, SchemaObject } from "../types.js";
import { tsLiteral, tsModifiers, tsPropertyIndex } from "./ts.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { escapePointer } from "@redocly/openapi-core/lib/ref-utils.js";
import ts from "typescript";
import { escapePointer } from "../lib/redocly.js";
import { addJSDocComment, tsModifiers, tsPropertyIndex, UNKNOWN } from "../lib/ts.js";
import { getEntries } from "../lib/utils.js";
import type { HeaderObject, TransformNodeOptions } from "../types.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseRef } from "@redocly/openapi-core/lib/ref-utils.js";
import ts from "typescript";
import { parseRef } from "../lib/redocly.js";
import {
addJSDocComment,
BOOLEAN,
Expand Down
Loading
Loading