Skip to content
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
173 changes: 173 additions & 0 deletions plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import {
} from "./schemaToTypeAliasDeclaration";

describe("schemaToTypeAliasDeclaration", () => {
type SchemaObjectWithConst = SchemaObject & {
const?: string | number | boolean | null;
};

it("should generate null", () => {
const schema: SchemaObject = {
type: "null",
Expand Down Expand Up @@ -52,6 +56,52 @@ describe("schemaToTypeAliasDeclaration", () => {
expect(printSchema(schema)).toBe("export type Test = number | null;");
});

it("should generate string const as a literal", () => {
const schema: SchemaObjectWithConst = {
type: "string",
const: "foo",
};

expect(printSchema(schema)).toBe(`export type Test = "foo";`);
});

it("should generate number const as a literal", () => {
const schema: SchemaObjectWithConst = {
type: "integer",
const: 42,
};

expect(printSchema(schema)).toBe(`export type Test = 42;`);
});

it("should generate boolean const as a literal", () => {
const schema: SchemaObjectWithConst = {
type: "boolean",
const: true,
};

expect(printSchema(schema)).toBe(`export type Test = true;`);
});

it("should generate null const as a literal", () => {
const schema: SchemaObjectWithConst = {
type: "null",
const: null,
};

expect(printSchema(schema)).toBe("export type Test = null;");
});

it("should preserve nullable semantics for non-null const", () => {
const schema: SchemaObjectWithConst = {
type: "string",
const: "foo",
nullable: true,
};

expect(printSchema(schema)).toBe(`export type Test = "foo" | null;`);
});

it("should generate an array of numbers", () => {
const schema: SchemaObject = {
type: "array",
Expand Down Expand Up @@ -682,6 +732,106 @@ describe("schemaToTypeAliasDeclaration", () => {
);
});

it("should generate a oneOf with const branches", () => {
const schema: SchemaObjectWithConst = {
oneOf: [
{ type: "string", const: "foo" } as SchemaObjectWithConst,
{ type: "number", const: 42 } as SchemaObjectWithConst,
],
};

expect(printSchema(schema)).toMatchInlineSnapshot(
`"export type Test = "foo" | 42;"`
);
});

it("should preserve const discriminators inside object oneOf branches", () => {
const schema: SchemaObject = {
oneOf: [
{
type: "object",
properties: {
status: {
type: "string",
const: "READY",
} as SchemaObjectWithConst,
value: {
type: "string",
},
},
required: ["status", "value"],
},
{
type: "object",
properties: {
status: {
type: "string",
const: "COUNT",
} as SchemaObjectWithConst,
value: {
$ref: "#/components/schemas/CountToken",
},
},
required: ["status", "value"],
},
{
type: "object",
properties: {
status: {
type: "string",
const: "RATIO",
} as SchemaObjectWithConst,
value: {
type: "number",
format: "double",
},
},
required: ["status", "value"],
},
{
type: "object",
properties: {
status: {
type: "string",
const: "ENABLED",
} as SchemaObjectWithConst,
value: {
type: "boolean",
},
},
required: ["status", "value"],
},
],
};

expect(
printSchema(schema, "Test", "schemas", {
schemas: {
CountToken: {
type: "string",
},
},
})
).toMatchInlineSnapshot(`
"export type Test = {
status: "READY";
value: string;
} | {
status: "COUNT";
value: CountToken;
} | {
status: "RATIO";
/**
* @format double
*/
value: number;
} | {
status: "ENABLED";
value: boolean;
};"
`);
});

describe("discrimination", () => {
const schema: SchemaObject = {
oneOf: [
Expand Down Expand Up @@ -904,6 +1054,16 @@ describe("schemaToTypeAliasDeclaration", () => {
`);
});

it("should preserve const when merged with compatible keywords", () => {
const schema: SchemaObjectWithConst = {
allOf: [{ type: "string" }, { const: "foo" } as SchemaObjectWithConst],
};

expect(printSchema(schema)).toMatchInlineSnapshot(
`"export type Test = "foo";"`
);
});

it("should combine ref and inline type", () => {
const schema: SchemaObject = {
allOf: [
Expand Down Expand Up @@ -1063,6 +1223,19 @@ describe("schemaToTypeAliasDeclaration", () => {
);
});

it("should generate a union with const branches", () => {
const schema: SchemaObjectWithConst = {
anyOf: [
{ type: "boolean", const: false } as SchemaObjectWithConst,
{ type: "string", const: "foo" } as SchemaObjectWithConst,
],
};

expect(printSchema(schema)).toMatchInlineSnapshot(
`"export type Test = false | "foo";"`
);
});

it("should combine required & properties", () => {
// from github api - operationId: gists/update
const schema: SchemaObject = {
Expand Down
36 changes: 36 additions & 0 deletions plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export type Context = {
currentComponent: OpenAPIComponentType | null;
};

type SupportedConstValue = string | number | boolean | null;
type SchemaObjectWithConst = SchemaObject & {
const?: SupportedConstValue;
};

let useEnumsConfigBase: boolean | undefined;

/**
Expand Down Expand Up @@ -148,6 +153,15 @@ export const getType = (
return f.createKeywordTypeNode(ts.SyntaxKind.NeverKeyword);
}

const literalConstType = getConstType(schema);
const constValue = (schema as SchemaObjectWithConst).const;
if (literalConstType) {
return withNullable(
literalConstType,
constValue !== null ? schema.nullable : false
);
}

if (schema.oneOf) {
return f.createUnionTypeNode([
...schema.oneOf.map((i) =>
Expand Down Expand Up @@ -349,6 +363,28 @@ export const getType = (
}
};

const getConstType = (schema: SchemaObject): ts.LiteralTypeNode | undefined => {
const constValue = (schema as SchemaObjectWithConst).const;

if (typeof constValue === "string") {
return f.createLiteralTypeNode(f.createStringLiteral(constValue));
}

if (typeof constValue === "number") {
return f.createLiteralTypeNode(f.createNumericLiteral(constValue));
}

if (typeof constValue === "boolean") {
return f.createLiteralTypeNode(
constValue ? f.createTrue() : f.createFalse()
);
}

if (constValue === null) {
return f.createLiteralTypeNode(f.createNull());
}
};

/**
* Add nullable option if needed.
*
Expand Down
50 changes: 50 additions & 0 deletions plugins/typescript/src/generators/generateSchemaTypes.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it, vi } from "vitest";
import { OpenAPIObject } from "openapi3-ts/oas30";
import { petstore } from "../fixtures/petstore";
import { generateSchemaTypes } from "./generateSchemaTypes";
import { createWriteFileMock } from "../testUtils";
Expand Down Expand Up @@ -324,6 +325,55 @@ describe("generateSchemaTypes", () => {
`);
});

it("should preserve const literals without changing adjacent schemas", async () => {
const writeFile = createWriteFileMock();
const readFile = vi.fn(() => Promise.resolve(""));
const openAPIDocument = {
openapi: "3.0.3",
info: {
title: "Const API",
version: "1.0.0",
},
paths: {},
components: {
schemas: {
Status: {
type: "string",
const: "ready",
},
Counter: {
type: "integer",
},
},
},
} as OpenAPIObject;

await generateSchemaTypes(
{
openAPIDocument,
writeFile,
readFile,
existsFile: () => true,
},
{
filenameCase: "camel",
}
);

expect(writeFile.mock.calls[0][0]).toBe("constApiSchemas.ts");
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
"/**
* Generated by @openapi-codegen
*
* @version 1.0.0
*/
export type Status = "ready";

export type Counter = number;
"
`);
});

it("should generate the responses file", async () => {
const writeFile = createWriteFileMock();
const readFile = vi.fn(() => Promise.resolve(""));
Expand Down
Loading