Skip to content

Skip resolving in #/components/examples #977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2022
Merged
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
8 changes: 4 additions & 4 deletions examples/github-api-next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12306,7 +12306,7 @@ export interface components {
require_code_owner_reviews: boolean;
required_approving_review_count?: number;
/**
* @description Whether someone other than the person who last pushed to the branch must approve this pull request.
* @description Whether the most recent push must be approved by someone other than the person who pushed it.
* @default false
*/
require_last_push_approval?: boolean;
Expand Down Expand Up @@ -12592,7 +12592,7 @@ export interface components {
require_code_owner_reviews?: boolean;
required_approving_review_count?: number;
/**
* @description Whether someone other than the person who last pushed to the branch must approve this pull request.
* @description Whether the most recent push must be approved by someone other than the person who pushed it.
* @default false
*/
require_last_push_approval?: boolean;
Expand Down Expand Up @@ -90770,7 +90770,7 @@ export interface operations {
/** @description Specify the number of reviewers required to approve pull requests. Use a number between 1 and 6 or 0 to not require reviewers. */
required_approving_review_count?: number;
/**
* @description Whether someone other than the person who last pushed to the branch must approve this pull request. Default: `false`.
* @description Whether the most recent push must be approved by someone other than the person who pushed it. Default: `false`.
* @default false
*/
require_last_push_approval?: boolean;
Expand Down Expand Up @@ -90935,7 +90935,7 @@ export interface operations {
/** @description Specifies the number of reviewers required to approve pull requests. Use a number between 1 and 6 or 0 to not require reviewers. */
required_approving_review_count?: number;
/**
* @description Whether someone other than the person who last pushed to the branch must approve this pull request. Default: `false`
* @description Whether the most recent push must be approved by someone other than the person who pushed it. Default: `false`
* @default false
*/
require_last_push_approval?: boolean;
Expand Down
8 changes: 4 additions & 4 deletions examples/github-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15230,7 +15230,7 @@ export interface components {
/** @example 2 */
required_approving_review_count?: number;
/**
* @description Whether someone other than the person who last pushed to the branch must approve this pull request.
* @description Whether the most recent push must be approved by someone other than the person who pushed it.
* @default false
* @example true
*/
Expand Down Expand Up @@ -15598,7 +15598,7 @@ export interface components {
require_code_owner_reviews?: boolean;
required_approving_review_count?: number;
/**
* @description Whether someone other than the person who last pushed to the branch must approve this pull request.
* @description Whether the most recent push must be approved by someone other than the person who pushed it.
* @default false
*/
require_last_push_approval?: boolean;
Expand Down Expand Up @@ -95282,7 +95282,7 @@ export interface operations {
/** @description Specify the number of reviewers required to approve pull requests. Use a number between 1 and 6 or 0 to not require reviewers. */
required_approving_review_count?: number;
/**
* @description Whether someone other than the person who last pushed to the branch must approve this pull request. Default: `false`.
* @description Whether the most recent push must be approved by someone other than the person who pushed it. Default: `false`.
* @default false
*/
require_last_push_approval?: boolean;
Expand Down Expand Up @@ -95447,7 +95447,7 @@ export interface operations {
/** @description Specifies the number of reviewers required to approve pull requests. Use a number between 1 and 6 or 0 to not require reviewers. */
required_approving_review_count?: number;
/**
* @description Whether someone other than the person who last pushed to the branch must approve this pull request. Default: `false`
* @description Whether the most recent push must be approved by someone other than the person who pushed it. Default: `false`
* @default false
*/
require_last_push_approval?: boolean;
Expand Down
9,248 changes: 9,248 additions & 0 deletions examples/octokit-ghes-3.6-diff-to-api.ts

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions scripts/download-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const singleFile = {
"https://raw.githubusercontent.com/github/rest-api-description/main/descriptions/api.github.com/api.github.com.yaml",
"github-api-next":
"https://raw.githubusercontent.com/github/rest-api-description/main/descriptions-next/api.github.com/api.github.com.yaml",
"octokit-ghes-3.6-diff-to-api":
"https://raw.githubusercontent.com/octokit/octokit-next.js/main/cache/types-openapi/ghes-3.6-diff-to-api.github.com.json",
"stripe-api": "https://raw.githubusercontent.com/stripe/openapi/master/openapi/spec3.yaml",
};
export const multiFile = {
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ async function openapiTS(
// 1. load schema (and subschemas)
const allSchemas: { [id: string]: Subschema } = {};
const schemaURL: URL = typeof schema === "string" ? resolveSchema(schema) : (schema as URL);

await load(schemaURL, {
...ctx,
auth: options.auth,
Expand Down
16 changes: 12 additions & 4 deletions src/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,28 @@ export default async function load(
}

// 2. resolve $refs
const currentSchema = options.schemas[schemaID].schema;

// 2a. remove "components.examples" first
if (options.schemas[schemaID].hint === "OpenAPI3") {
if ("components" in currentSchema && currentSchema.components && "examples" in currentSchema.components)
delete currentSchema.components.examples;
}

const refPromises: Promise<any>[] = [];
walk(options.schemas[schemaID].schema, (rawNode, nodePath) => {
walk(currentSchema, (rawNode, nodePath) => {
// filter custom properties from allOf, anyOf, oneOf
for (const k of ["allOf", "anyOf", "oneOf"]) {
if (Array.isArray(rawNode[k])) {
rawNode[k] = (rawNode as any)[k].filter((o: SchemaObject | ReferenceObject) => {
if (!("$ref" in o)) return true;
if (!("$ref" in o) || typeof o.$ref !== "string") return true;
const ref = parseRef(o.$ref);
return !ref.path.some((i) => i.startsWith("x-")); // ignore all custom "x-*" properties
});
}
}

if (!("$ref" in rawNode)) return;
if (!("$ref" in rawNode) || typeof rawNode.$ref !== "string") return;
const node = rawNode as unknown as ReferenceObject;

const ref = parseRef(node.$ref);
Expand Down Expand Up @@ -253,7 +261,7 @@ export default async function load(
if (schemaID === ".") {
for (const subschemaID of Object.keys(options.schemas)) {
walk(options.schemas[subschemaID].schema, (rawNode) => {
if (!("$ref" in rawNode)) return;
if (!("$ref" in rawNode) || typeof rawNode.$ref !== "string") return;
const node = rawNode as unknown as ReferenceObject;

const ref = parseRef(node.$ref);
Expand Down
5 changes: 5 additions & 0 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ describe("CLI", () => {
const { stdout } = await execa(cmd, ["./test/fixtures/github-api-next.yaml"], { cwd });
expect(stdout).toBe(expected);
}, 30000);
test("Octokit GHES 3.6 Diff to API", async () => {
const expected = fs.readFileSync(new URL("./examples/octokit-ghes-3.6-diff-to-api.ts", cwd), "utf8").trim();
const { stdout } = await execa(cmd, ["./test/fixtures/octokit-ghes-3.6-diff-to-api.yaml"], { cwd });
expect(stdout).toBe(expected);
}, 30000);
test("Stripe API", async () => {
const expected = fs.readFileSync(new URL("./examples/stripe-api.ts", cwd), "utf8").trim();
const { stdout } = await execa(cmd, ["./test/fixtures/stripe-api.yaml"], { cwd });
Expand Down
9 changes: 9 additions & 0 deletions test/components-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ const basicSchema: ComponentsObject = {
},
},
},
// "examples" should just be ignored
examples: {
ExampleObject: {
value: {
name: "Example",
$ref: "foo.yml#/components/schemas/Bar",
},
},
},
headers: {
Auth: { schema: { type: "string" } },
},
Expand Down
54 changes: 54 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,54 @@ export interface components {

export type external = Record<string, never>;

export type operations = Record<string, never>;
`);
});

test("components.examples are skipped", async () => {
const generated = await openapiTS({
openapi: "3.0",
info: { title: "Test", version: "1.0" },
components: {
schemas: {
Example: {
type: "object",
properties: {
name: { type: "string" },
$ref: { type: "string" },
},
required: ["name", "$ref"],
},
},
examples: {
Example: {
value: {
name: "Test",
$ref: "fake.yml#/components/schemas/Example",
},
},
},
},
});
expect(generated).toBe(`${BOILERPLATE}
export type paths = Record<string, never>;

export interface components {
schemas: {
Example: {
name: string;
$ref: string;
};
};
responses: never;
parameters: never;
requestBodies: never;
headers: never;
pathItems: never;
}

export type external = Record<string, never>;

export type operations = Record<string, never>;
`);
});
Expand Down Expand Up @@ -417,6 +465,12 @@ export type operations = Record<string, never>;
expect(generated).toBe(fs.readFileSync(new URL("./github-api-next.ts", EXAMPLES_DIR), "utf8"));
}, 30000);
});
describe("Octokit GHES 3.6 Diff to API", () => {
test("default options", async () => {
const generated = await openapiTS(new URL("./octokit-ghes-3.6-diff-to-api.yaml", FIXTURES_DIR));
expect(generated).toBe(fs.readFileSync(new URL("./octokit-ghes-3.6-diff-to-api.ts", EXAMPLES_DIR), "utf8"));
}, 30000);
});
describe("Stripe", () => {
test("default options", async () => {
const generated = await openapiTS(new URL("./stripe-api.yaml", FIXTURES_DIR));
Expand Down