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
1 change: 0 additions & 1 deletion packages/openapi-code-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"@typespec/versioning": "0.77.0",
"@typespec/xml": "0.77.0",
"joi": "^18.0.2",
"piscina": "^5.1.4",
"tsx": "^4.21.0"
},
"dependencies": {
Expand Down
6 changes: 5 additions & 1 deletion packages/openapi-code-generator/src/core/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ export type InputConfig = {
enumExtensibility: "open" | "closed"
}

export class Input {
export interface ISchemaProvider {
schema(maybeRef: MaybeIRModel): IRModel
}

export class Input implements ISchemaProvider {
constructor(
private loader: OpenapiLoader,
readonly config: InputConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import type {
IRModelBoolean,
IRModelIntersection,
IRModelNever,
IRModelNull,
IRModelNumeric,
IRModelObject,
IRModelRecord,
IRModelString,
IRModelUnion,
IRRef,
} from "../core/openapi-types-normalized"

const base = {
Expand Down Expand Up @@ -110,9 +112,20 @@ const extension = {
default: undefined,
"x-internal-preprocess": undefined,
} satisfies IRModelUnion,
null: {
isIRModel: true,
type: "null",
nullable: false,
} satisfies IRModelNull,
}

export const irFixture = {
ref(path: string, file = ""): IRRef {
return {
$ref: `${file}#${path}`,
"x-internal-preprocess": undefined,
}
},
any(partial: Partial<IRModelAny> = {}): IRModelAny {
return {...base.any, ...partial}
},
Expand Down Expand Up @@ -147,4 +160,7 @@ export const irFixture = {
union(partial: Partial<IRModelUnion> = {}): IRModelUnion {
return {...extension.union, ...partial}
},
null(partial: Partial<IRModelNull> = {}): IRModelNull {
return {...extension.null, ...partial}
},
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {describe, expect, it} from "@jest/globals"
import {TestOutputTypeChecker} from "./typescript-compiler.test-utils"

describe("test/typescript-compiler", () => {
const typechecker = new TestOutputTypeChecker()

it("should throw is provided code doesn't typecheck successfully", async () => {
expect(() =>
typechecker.typecheck([
{
filename: "unit-test.ts",
content: `
const x = "foo"
const y: number = x
`,
},
]),
).toThrow(/TS2322: Type 'string' is not assignable to type 'number'/)
})

it("should not throw if the provided code is valid", async () => {
expect(() =>
typechecker.typecheck([
{
filename: "unit-test.ts",
content: `
const x = "foo"
const y: string = x
`,
},
]),
).not.toThrow()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import fs from "node:fs"
import path from "node:path"
import util from "node:util"
import ts from "typescript"

export class TestOutputTypeChecker {
private files: Record<string, string> = {}
private readonly sourceFileCache: Record<string, ts.SourceFile | undefined> =
{}

private readonly compilerHost: ts.CompilerHost
private readonly options: ts.CompilerOptions = {
noEmit: true,
strict: true,
target: ts.ScriptTarget.ESNext,
module: ts.ModuleKind.CommonJS,
skipLibCheck: true,
types: [],
}
private program: ts.Program

constructor() {
const defaultHost = ts.createCompilerHost(this.options, true)

const compilerHost = {
...defaultHost,
fileExists: (fileName: string) => {
return fileName in this.files
},
readFile: (fileName: string): string => {
const file = this.files[fileName]

if (file === undefined && fileName.startsWith("/")) {
return fs.readFileSync(fileName, "utf-8").toString()
}

if (file === undefined) {
throw new Error(`file '${fileName}' not found`)
}

return file
},
writeFile: () => {
/* noop */
},
getSourceFile: (fileName, languageVersion) => {
if (this.sourceFileCache[fileName]) {
return this.sourceFileCache[fileName]
}

const file = compilerHost.readFile(fileName)
const result = ts.createSourceFile(fileName, file, languageVersion)
this.sourceFileCache[fileName] = result

return result
},
} satisfies ts.CompilerHost

this.compilerHost = compilerHost
this.program = ts.createProgram({
rootNames: [],
options: this.options,
host: this.compilerHost,
})
}

typecheck(compilationUnits: {filename: string; content: string}[]) {
const rootNames: string[] = []

for (const filename in this.files) {
this.sourceFileCache[filename] = undefined
}

this.files = {}

for (const unit of compilationUnits) {
const filename = path.resolve(unit.filename)
this.files[filename] = unit.content
this.sourceFileCache[filename] = undefined
rootNames.push(filename)
}

this.program = ts.createProgram({
rootNames,
options: this.options,
host: this.compilerHost,
oldProgram: this.program,
})

const diagnostics = ts.getPreEmitDiagnostics(this.program)

if (diagnostics.length > 0) {
const formatted = ts.formatDiagnosticsWithColorAndContext(diagnostics, {
getCurrentDirectory: () => "",
getCanonicalFileName: (fileName) => fileName,
getNewLine: () => "\n",
})
throw new Error(
`TypeScript compilation failed:\n\n${util.stripVTControlCharacters(formatted)}`,
)
}
}
}
4 changes: 0 additions & 4 deletions packages/openapi-code-generator/src/test/workerWrapper.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {IROperation} from "../../core/openapi-types-normalized"
import {CompilationUnit, type ICompilable} from "../common/compilation-units"
import type {ImportBuilder} from "../common/import-builder"
import type {SchemaBuilder} from "../common/schema-builders/schema-builder"
import type {TypeBuilder} from "../common/type-builder"
import type {TypeBuilder} from "../common/type-builder/type-builder"
import {union} from "../common/type-utils"
import {ClientOperationBuilder} from "./client-operation-builder"
import {ClientServersBuilder} from "./client-servers-builder"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
import {extractPlaceholders} from "../../core/openapi-utils"
import {camelCase, isDefined} from "../../core/utils"
import type {SchemaBuilder} from "../common/schema-builders/schema-builder"
import type {TypeBuilder} from "../common/type-builder"
import type {TypeBuilder} from "../common/type-builder/type-builder"
import {object, quotedStringLiteral} from "../common/type-utils"
import {
combineParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {titleCase} from "../../../core/utils"
import type {OpenapiTypescriptGeneratorConfig} from "../../../templates.types"
import {ImportBuilder} from "../../common/import-builder"
import {schemaBuilderFactory} from "../../common/schema-builders/schema-builder"
import {TypeBuilder} from "../../common/type-builder"
import {TypeBuilder} from "../../common/type-builder/type-builder"
import {AngularModuleBuilder} from "./angular-module-builder"
import {AngularServiceBuilder} from "./angular-service-builder"

Expand All @@ -17,7 +17,7 @@ export async function generateTypescriptAngular(
const moduleImports = new ImportBuilder(importBuilderConfig)
const serviceImports = new ImportBuilder(importBuilderConfig)

const rootTypeBuilder = await TypeBuilder.fromInput(
const rootTypeBuilder = await TypeBuilder.fromSchemaProvider(
"./models.ts",
input,
config.compilerOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {titleCase} from "../../../core/utils"
import type {OpenapiTypescriptGeneratorConfig} from "../../../templates.types"
import {ImportBuilder} from "../../common/import-builder"
import {schemaBuilderFactory} from "../../common/schema-builders/schema-builder"
import {TypeBuilder} from "../../common/type-builder"
import {TypeBuilder} from "../../common/type-builder/type-builder"
import {TypescriptAxiosClientBuilder} from "./typescript-axios-client-builder"

export async function generateTypescriptAxios(
Expand All @@ -15,7 +15,7 @@ export async function generateTypescriptAxios(
const schemaBuilderImports = new ImportBuilder(importBuilderConfig)
const clientImports = new ImportBuilder(importBuilderConfig)

const rootTypeBuilder = await TypeBuilder.fromInput(
const rootTypeBuilder = await TypeBuilder.fromSchemaProvider(
"./models.ts",
input,
config.compilerOptions,
Expand Down
Loading