这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
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
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/debug/ir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const print = (ir: IR.Model, options: PrintOptions = {}) => {

const operation = item[method]!;
log(
`${colors.green(method.toUpperCase())} ${colors.cyan(key)} (${colors.magenta(operation.operationId)})`,
`${colors.green(method.toUpperCase())} ${colors.cyan(key)} (${colors.magenta(operation.operationId ?? operation.id)})`,
base,
);

Expand Down
59 changes: 59 additions & 0 deletions packages/openapi-ts/src/ir/__tests__/types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { describe, expect, it } from 'vitest';

import type { IR } from '../types';

describe('IR types', () => {
it('IR.Context should be properly typed (not any)', () => {
const mockContext = {
config: {} as any,
gen: {} as any,
graph: undefined,
ir: {},
logger: {} as any,
package: {} as any,
plugins: {},
spec: {},
} as IR.Context;

// If Context were 'any', TypeScript wouldn't catch type errors
// This test verifies the type is properly resolved
expect(mockContext.config).toBeDefined();
expect(mockContext.spec).toBeDefined();

// TypeScript should enforce the type structure
// @ts-expect-error - nonExistentProperty should not exist on Context
expect(mockContext.nonExistentProperty).toBeUndefined();
});

it('IR.ReferenceObject should be properly typed (not any)', () => {
const mockRef: IR.ReferenceObject = {
$ref: '#/components/schemas/Pet',
};

// Verify $ref property exists and has correct type
expect(mockRef.$ref).toBe('#/components/schemas/Pet');

// TypeScript should enforce the type structure
// @ts-expect-error - nonExistentProperty should not exist on ReferenceObject
expect(mockRef.nonExistentProperty).toBeUndefined();
});

it('IR.Context should support generic parameter', () => {
type CustomSpec = { title: string; version: string };
const mockContext = {
config: {} as any,
gen: {} as any,
graph: undefined,
ir: {},
logger: {} as any,
package: {} as any,
plugins: {},
spec: { title: 'API', version: '1.0' },
} as IR.Context<CustomSpec>;

// Verify the generic parameter is properly typed
const spec: CustomSpec = mockContext.spec;
expect(spec.title).toBe('API');
expect(spec.version).toBe('1.0');
});
});
9 changes: 7 additions & 2 deletions packages/openapi-ts/src/ir/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import type { JsonSchemaDraft2020_12 } from '~/openApi/3.1.x/types/json-schema-draft-2020-12';
import type {
ReferenceObject,
SecuritySchemeObject,
ServerObject,
} from '~/openApi/3.1.x/types/spec';

import type { Context as IRContext } from './context';
import type { Context } from './context';
import type { IRMediaType } from './mediaType';

type IRContext<Spec extends Record<string, any> = any> = Context<Spec>;

type IRReferenceObject = ReferenceObject;

interface IRBodyObject {
mediaType: string;
/**
Expand Down Expand Up @@ -228,7 +233,7 @@ export namespace IR {
export type ParametersObject = IRParametersObject;
export type PathItemObject = IRPathItemObject;
export type PathsObject = IRPathsObject;
export type ReferenceObject = ReferenceObject;
export type ReferenceObject = IRReferenceObject;
export type RequestBodyObject = IRRequestBodyObject;
export type ResponseObject = IRResponseObject;
export type ResponsesObject = IRResponsesObject;
Expand Down