diff --git a/.changeset/lazy-cheetahs-grow.md b/.changeset/lazy-cheetahs-grow.md new file mode 100644 index 000000000..7bc0782e4 --- /dev/null +++ b/.changeset/lazy-cheetahs-grow.md @@ -0,0 +1,5 @@ +--- +'@hey-api/openapi-ts': patch +--- + +feat: add initial implementation of prefixItems diff --git a/packages/openapi-ts/src/compiler/typedef.ts b/packages/openapi-ts/src/compiler/typedef.ts index c67fbe4dd..4a63656d9 100644 --- a/packages/openapi-ts/src/compiler/typedef.ts +++ b/packages/openapi-ts/src/compiler/typedef.ts @@ -7,6 +7,8 @@ import { tsNodeToString, } from './utils'; +const nullNode = ts.factory.createTypeReferenceNode('null'); + export const createTypeNode = ( base: any | ts.TypeNode, args?: (any | ts.TypeNode)[], @@ -62,6 +64,23 @@ export type Property = { comment?: Comments; }; +/** + * Returns a union of provided node with null if marked as nullable, + * otherwise returns the provided node unmodified. + */ +const maybeNullable = ({ + isNullable, + node, +}: { + node: ts.TypeNode; + isNullable: boolean; +}) => { + if (!isNullable) { + return node; + } + return ts.factory.createUnionTypeNode([node, nullNode]); +}; + /** * Create a interface type node. Example `{ readonly x: string, y?: number }` * @param properties - the properties of the interface. @@ -90,13 +109,7 @@ export const createTypeInterfaceNode = ( return signature; }), ); - if (!isNullable) { - return node; - } - return ts.factory.createUnionTypeNode([ - node, - ts.factory.createTypeReferenceNode('null'), - ]); + return maybeNullable({ isNullable, node }); }; /** @@ -110,10 +123,10 @@ export const createTypeUnionNode = ( isNullable: boolean = false, ) => { const nodes = types.map((type) => createTypeNode(type)); - if (isNullable) { - nodes.push(ts.factory.createTypeReferenceNode('null')); + if (!isNullable) { + return ts.factory.createUnionTypeNode(nodes); } - return ts.factory.createUnionTypeNode(nodes); + return ts.factory.createUnionTypeNode([...nodes, nullNode]); }; /** @@ -126,15 +139,9 @@ export const createTypeIntersectNode = ( types: (any | ts.TypeNode)[], isNullable: boolean = false, ) => { - const nodes = types.map((t) => createTypeNode(t)); - const intersect = ts.factory.createIntersectionTypeNode(nodes); - if (isNullable) { - return ts.factory.createUnionTypeNode([ - intersect, - ts.factory.createTypeReferenceNode('null'), - ]); - } - return intersect; + const nodes = types.map((type) => createTypeNode(type)); + const node = ts.factory.createIntersectionTypeNode(nodes); + return maybeNullable({ isNullable, node }); }; /** @@ -151,15 +158,8 @@ export const createTypeTupleNode = ({ types: Array; }) => { const nodes = types.map((type) => createTypeNode(type)); - const tupleNode = ts.factory.createTupleTypeNode(nodes); - if (isNullable) { - const unionNode = ts.factory.createUnionTypeNode([ - tupleNode, - ts.factory.createTypeReferenceNode('null'), - ]); - return unionNode; - } - return tupleNode; + const node = ts.factory.createTupleTypeNode(nodes); + return maybeNullable({ isNullable, node }); }; /** @@ -186,13 +186,7 @@ export const createTypeRecordNode = ( type: valueNode, }, ]); - if (!isNullable) { - return node; - } - return ts.factory.createUnionTypeNode([ - node, - ts.factory.createTypeReferenceNode('null'), - ]); + return maybeNullable({ isNullable, node }); }; /** @@ -208,11 +202,5 @@ export const createTypeArrayNode = ( const node = ts.factory.createTypeReferenceNode('Array', [ createTypeUnionNode(types), ]); - if (!isNullable) { - return node; - } - return ts.factory.createUnionTypeNode([ - node, - ts.factory.createTypeReferenceNode('null'), - ]); + return maybeNullable({ isNullable, node }); }; diff --git a/packages/openapi-ts/src/openApi/common/interfaces/client.ts b/packages/openapi-ts/src/openApi/common/interfaces/client.ts index 2e0a3e774..799aa8cc7 100644 --- a/packages/openapi-ts/src/openApi/common/interfaces/client.ts +++ b/packages/openapi-ts/src/openApi/common/interfaces/client.ts @@ -145,7 +145,7 @@ export interface Model extends Schema { | OpenApiParameter['in'] | OperationResponse['in'] | ''; - link: Model | null; + link: Model | Model[] | null; meta?: ModelMeta; /** * @deprecated use `meta.name` instead diff --git a/packages/openapi-ts/src/openApi/common/parser/operation.ts b/packages/openapi-ts/src/openApi/common/parser/operation.ts index 8cdc0ee96..2db5a061b 100644 --- a/packages/openapi-ts/src/openApi/common/parser/operation.ts +++ b/packages/openapi-ts/src/openApi/common/parser/operation.ts @@ -8,7 +8,20 @@ const areEqual = (a: Model, b: Model): boolean => { const equal = a.type === b.type && a.base === b.base && a.template === b.template; if (equal && a.link && b.link) { - return areEqual(a.link, b.link); + if (!Array.isArray(a.link) && !Array.isArray(b.link)) { + return areEqual(a.link, b.link); + } + + if ( + Array.isArray(a.link) && + Array.isArray(b.link) && + a.link.length === b.link.length + ) { + const bLinks = b.link; + return a.link.every((model, index) => areEqual(model, bLinks[index]!)); + } + + return false; } return equal; }; diff --git a/packages/openapi-ts/src/openApi/v3/interfaces/OpenApiSchema.ts b/packages/openapi-ts/src/openApi/v3/interfaces/OpenApiSchema.ts index 1da13559e..271bd0b79 100644 --- a/packages/openapi-ts/src/openApi/v3/interfaces/OpenApiSchema.ts +++ b/packages/openapi-ts/src/openApi/v3/interfaces/OpenApiSchema.ts @@ -48,6 +48,7 @@ export interface OpenApiSchema extends OpenApiReference, WithEnumExtension { nullable?: boolean; oneOf?: OpenApiSchema[]; pattern?: string; + prefixItems?: OpenApiSchema[]; properties?: Dictionary; readOnly?: boolean; required?: string[]; diff --git a/packages/openapi-ts/src/openApi/v3/parser/getModel.ts b/packages/openapi-ts/src/openApi/v3/parser/getModel.ts index 2e69ce6b4..32b24b69c 100644 --- a/packages/openapi-ts/src/openApi/v3/parser/getModel.ts +++ b/packages/openapi-ts/src/openApi/v3/parser/getModel.ts @@ -107,7 +107,44 @@ export const getModel = ({ } } - if (definitionTypes.includes('array') && definition.items) { + if ( + definitionTypes.includes('array') && + (definition.items || definition.prefixItems) + ) { + if (definition.prefixItems) { + const arrayItems = definition.prefixItems.map((item) => + getModel({ + definition: item, + openApi, + parentDefinition: definition, + types, + }), + ); + + model.export = 'array'; + model.$refs = [ + ...model.$refs, + ...arrayItems.reduce( + (acc, m) => [...acc, ...m.$refs], + [] as Model['$refs'], + ), + ]; + model.imports = [ + ...model.imports, + ...arrayItems.reduce( + (acc, m) => [...acc, ...m.imports], + [] as Model['imports'], + ), + ]; + model.link = arrayItems; + model.default = getDefault(definition, model); + return model; + } + + if (!definition.items) { + return model; + } + if (definition.items.$ref) { const arrayItems = getType({ type: definition.items.$ref }); model.$refs = [...model.$refs, definition.items.$ref]; diff --git a/packages/openapi-ts/src/utils/write/type.ts b/packages/openapi-ts/src/utils/write/type.ts index 97ef6e48b..5b94d125a 100644 --- a/packages/openapi-ts/src/utils/write/type.ts +++ b/packages/openapi-ts/src/utils/write/type.ts @@ -32,24 +32,33 @@ const typeReference = (model: Model) => { }; const typeArray = (model: Model) => { - // Special case where we use tuple to define constant size array. - if ( - model.export === 'array' && - model.link && - model.maxItems && - model.minItems && - model.maxItems === model.minItems && - model.maxItems <= 100 - ) { - const types = Array(model.maxItems).fill(toType(model.link)); - const tuple = compiler.typedef.tuple({ - isNullable: model.isNullable, - types, - }); - return tuple; - } - if (model.link) { + // We treat an array of `model.link` as constant size array definition. + if (Array.isArray(model.link)) { + const types = model.link.map((m) => toType(m)); + const tuple = compiler.typedef.tuple({ + isNullable: model.isNullable, + types, + }); + return tuple; + } + + // Special case where we use tuple to define constant size array. + if ( + model.export === 'array' && + model.maxItems && + model.minItems && + model.maxItems === model.minItems && + model.maxItems <= 100 + ) { + const types = Array(model.maxItems).fill(toType(model.link)); + const tuple = compiler.typedef.tuple({ + isNullable: model.isNullable, + types, + }); + return tuple; + } + return compiler.typedef.array([toType(model.link)], model.isNullable); } @@ -62,7 +71,8 @@ const typeEnum = (model: Model) => { }; const typeDict = (model: Model) => { - const type = model.link ? toType(model.link) : base(model); + const type = + model.link && !Array.isArray(model.link) ? toType(model.link) : base(model); return compiler.typedef.record(['string'], [type], model.isNullable); }; @@ -137,6 +147,8 @@ export const toType = (model: Model): TypeNode => { return typeEnum(model); case 'interface': return typeInterface(model); + case 'const': + case 'generic': case 'reference': default: return typeReference(model); diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3/schemas.gen.ts.snap index 2c8c2f435..e5c0ba16f 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3/schemas.gen.ts.snap @@ -1590,6 +1590,28 @@ export const $ModelWithAnyOfConstantSizeArray = { maxItems: 3 } as const; +export const $ModelWithPrefixItemsConstantSizeArray = { + type: 'array', + prefixItems: [ + { + '$ref': '#/components/schemas/ModelWithInteger' + }, + { + oneOf: [ + { + type: 'number' + }, + { + type: 'string' + } + ] + }, + { + type: 'string' + } + ] +} as const; + export const $ModelWithAnyOfConstantSizeArrayNullable = { type: ['array'], items: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3/types.gen.ts.snap index 2c0f942e6..1838dcae9 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3/types.gen.ts.snap @@ -875,6 +875,12 @@ export type ModelWithAnyOfConstantSizeArray = [ number | string ]; +export type ModelWithPrefixItemsConstantSizeArray = [ + ModelWithInteger, + number | string, + string +]; + export type ModelWithAnyOfConstantSizeArrayNullable = [ number | null | string, number | null | string, diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/schemas.gen.ts.snap index 2c8c2f435..e5c0ba16f 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/schemas.gen.ts.snap @@ -1590,6 +1590,28 @@ export const $ModelWithAnyOfConstantSizeArray = { maxItems: 3 } as const; +export const $ModelWithPrefixItemsConstantSizeArray = { + type: 'array', + prefixItems: [ + { + '$ref': '#/components/schemas/ModelWithInteger' + }, + { + oneOf: [ + { + type: 'number' + }, + { + type: 'string' + } + ] + }, + { + type: 'string' + } + ] +} as const; + export const $ModelWithAnyOfConstantSizeArrayNullable = { type: ['array'], items: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/types.gen.ts.snap index f3c3fdece..16f6f9e6c 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/types.gen.ts.snap @@ -765,6 +765,12 @@ export type ModelWithAnyOfConstantSizeArray = [ number | string ]; +export type ModelWithPrefixItemsConstantSizeArray = [ + ModelWithInteger, + number | string, + string +]; + export type ModelWithAnyOfConstantSizeArrayNullable = [ number | null | string, number | null | string, diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_axios/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_axios/schemas.gen.ts.snap index 2c8c2f435..e5c0ba16f 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_axios/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_axios/schemas.gen.ts.snap @@ -1590,6 +1590,28 @@ export const $ModelWithAnyOfConstantSizeArray = { maxItems: 3 } as const; +export const $ModelWithPrefixItemsConstantSizeArray = { + type: 'array', + prefixItems: [ + { + '$ref': '#/components/schemas/ModelWithInteger' + }, + { + oneOf: [ + { + type: 'number' + }, + { + type: 'string' + } + ] + }, + { + type: 'string' + } + ] +} as const; + export const $ModelWithAnyOfConstantSizeArrayNullable = { type: ['array'], items: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_axios/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_axios/types.gen.ts.snap index 2c0f942e6..1838dcae9 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_axios/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_axios/types.gen.ts.snap @@ -875,6 +875,12 @@ export type ModelWithAnyOfConstantSizeArray = [ number | string ]; +export type ModelWithPrefixItemsConstantSizeArray = [ + ModelWithInteger, + number | string, + string +]; + export type ModelWithAnyOfConstantSizeArrayNullable = [ number | null | string, number | null | string, diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/types.gen.ts.snap index 453abba61..17503cb72 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/types.gen.ts.snap @@ -765,6 +765,12 @@ export type ModelWithAnyOfConstantSizeArray = [ number | string ]; +export type ModelWithPrefixItemsConstantSizeArray = [ + ModelWithInteger, + number | string, + string +]; + export type ModelWithAnyOfConstantSizeArrayNullable = [ number | null | string, number | null | string, diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/types.gen.ts.snap index 0e75172a9..940bd3a1e 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/types.gen.ts.snap @@ -836,6 +836,12 @@ export type ModelWithAnyOfConstantSizeArray = [ number | string ]; +export type ModelWithPrefixItemsConstantSizeArray = [ + ModelWithInteger, + number | string, + string +]; + export type ModelWithAnyOfConstantSizeArrayNullable = [ number | null | string, number | null | string, diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios/schemas.gen.ts.snap index 2c8c2f435..e5c0ba16f 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios/schemas.gen.ts.snap @@ -1590,6 +1590,28 @@ export const $ModelWithAnyOfConstantSizeArray = { maxItems: 3 } as const; +export const $ModelWithPrefixItemsConstantSizeArray = { + type: 'array', + prefixItems: [ + { + '$ref': '#/components/schemas/ModelWithInteger' + }, + { + oneOf: [ + { + type: 'number' + }, + { + type: 'string' + } + ] + }, + { + type: 'string' + } + ] +} as const; + export const $ModelWithAnyOfConstantSizeArrayNullable = { type: ['array'], items: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios/types.gen.ts.snap index d54889f6a..bab6683e4 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios/types.gen.ts.snap @@ -875,6 +875,12 @@ export type ModelWithAnyOfConstantSizeArray = [ number | string ]; +export type ModelWithPrefixItemsConstantSizeArray = [ + ModelWithInteger, + number | string, + string +]; + export type ModelWithAnyOfConstantSizeArrayNullable = [ number | null | string, number | null | string, diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch/schemas.gen.ts.snap index 2c8c2f435..e5c0ba16f 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch/schemas.gen.ts.snap @@ -1590,6 +1590,28 @@ export const $ModelWithAnyOfConstantSizeArray = { maxItems: 3 } as const; +export const $ModelWithPrefixItemsConstantSizeArray = { + type: 'array', + prefixItems: [ + { + '$ref': '#/components/schemas/ModelWithInteger' + }, + { + oneOf: [ + { + type: 'number' + }, + { + type: 'string' + } + ] + }, + { + type: 'string' + } + ] +} as const; + export const $ModelWithAnyOfConstantSizeArrayNullable = { type: ['array'], items: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch/types.gen.ts.snap index d54889f6a..bab6683e4 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch/types.gen.ts.snap @@ -875,6 +875,12 @@ export type ModelWithAnyOfConstantSizeArray = [ number | string ]; +export type ModelWithPrefixItemsConstantSizeArray = [ + ModelWithInteger, + number | string, + string +]; + export type ModelWithAnyOfConstantSizeArrayNullable = [ number | null | string, number | null | string, diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_models/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_models/types.gen.ts.snap index dcfdce99d..0b59bff27 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_models/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_models/types.gen.ts.snap @@ -765,6 +765,12 @@ export type ModelWithAnyOfConstantSizeArray = [ number | string ]; +export type ModelWithPrefixItemsConstantSizeArray = [ + ModelWithInteger, + number | string, + string +]; + export type ModelWithAnyOfConstantSizeArrayNullable = [ number | null | string, number | null | string, diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_node/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_node/schemas.gen.ts.snap index 2c8c2f435..e5c0ba16f 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_node/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_node/schemas.gen.ts.snap @@ -1590,6 +1590,28 @@ export const $ModelWithAnyOfConstantSizeArray = { maxItems: 3 } as const; +export const $ModelWithPrefixItemsConstantSizeArray = { + type: 'array', + prefixItems: [ + { + '$ref': '#/components/schemas/ModelWithInteger' + }, + { + oneOf: [ + { + type: 'number' + }, + { + type: 'string' + } + ] + }, + { + type: 'string' + } + ] +} as const; + export const $ModelWithAnyOfConstantSizeArrayNullable = { type: ['array'], items: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_node/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_node/types.gen.ts.snap index 2c0f942e6..1838dcae9 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_node/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_node/types.gen.ts.snap @@ -875,6 +875,12 @@ export type ModelWithAnyOfConstantSizeArray = [ number | string ]; +export type ModelWithPrefixItemsConstantSizeArray = [ + ModelWithInteger, + number | string, + string +]; + export type ModelWithAnyOfConstantSizeArrayNullable = [ number | null | string, number | null | string, diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_form/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_form/schemas.gen.ts.snap index a7a4f18c1..42877c8e2 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_form/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_form/schemas.gen.ts.snap @@ -1471,6 +1471,28 @@ export const $ModelWithAnyOfConstantSizeArray = { maxItems: 3 } as const; +export const $ModelWithPrefixItemsConstantSizeArray = { + type: 'array', + prefixItems: [ + { + '$ref': '#/components/schemas/ModelWithInteger' + }, + { + oneOf: [ + { + type: 'number' + }, + { + type: 'string' + } + ] + }, + { + type: 'string' + } + ] +} as const; + export const $ModelWithAnyOfConstantSizeArrayNullable = { type: ['array'], items: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_json/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_json/schemas.gen.ts.snap index 2c8c2f435..e5c0ba16f 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_json/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_json/schemas.gen.ts.snap @@ -1590,6 +1590,28 @@ export const $ModelWithAnyOfConstantSizeArray = { maxItems: 3 } as const; +export const $ModelWithPrefixItemsConstantSizeArray = { + type: 'array', + prefixItems: [ + { + '$ref': '#/components/schemas/ModelWithInteger' + }, + { + oneOf: [ + { + type: 'number' + }, + { + type: 'string' + } + ] + }, + { + type: 'string' + } + ] +} as const; + export const $ModelWithAnyOfConstantSizeArrayNullable = { type: ['array'], items: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_tree_shakeable/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_tree_shakeable/types.gen.ts.snap index 2b90449b0..ae5711905 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_tree_shakeable/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_tree_shakeable/types.gen.ts.snap @@ -875,6 +875,12 @@ export type ModelWithAnyOfConstantSizeArray = [ number | string ]; +export type ModelWithPrefixItemsConstantSizeArray = [ + ModelWithInteger, + number | string, + string +]; + export type ModelWithAnyOfConstantSizeArrayNullable = [ number | null | string, number | null | string, diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr/schemas.gen.ts.snap index 2c8c2f435..e5c0ba16f 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr/schemas.gen.ts.snap @@ -1590,6 +1590,28 @@ export const $ModelWithAnyOfConstantSizeArray = { maxItems: 3 } as const; +export const $ModelWithPrefixItemsConstantSizeArray = { + type: 'array', + prefixItems: [ + { + '$ref': '#/components/schemas/ModelWithInteger' + }, + { + oneOf: [ + { + type: 'number' + }, + { + type: 'string' + } + ] + }, + { + type: 'string' + } + ] +} as const; + export const $ModelWithAnyOfConstantSizeArrayNullable = { type: ['array'], items: { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr/types.gen.ts.snap index 2c0f942e6..1838dcae9 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr/types.gen.ts.snap @@ -875,6 +875,12 @@ export type ModelWithAnyOfConstantSizeArray = [ number | string ]; +export type ModelWithPrefixItemsConstantSizeArray = [ + ModelWithInteger, + number | string, + string +]; + export type ModelWithAnyOfConstantSizeArrayNullable = [ number | null | string, number | null | string, diff --git a/packages/openapi-ts/test/sample.cjs b/packages/openapi-ts/test/sample.cjs index 82577fc69..b01ad0731 100644 --- a/packages/openapi-ts/test/sample.cjs +++ b/packages/openapi-ts/test/sample.cjs @@ -7,19 +7,20 @@ const main = async () => { input: './test/spec/v3.json', // input: 'https://mongodb-mms-prod-build-server.s3.amazonaws.com/openapi/2caffd88277a4e27c95dcefc7e3b6a63a3b03297-v2-2023-11-15.json', output: { - path: './test/generated/v3/', + path: './test/generated/sample/', }, schemas: { export: false, }, services: { asClass: true, - // export: false, + export: false, // name: '^Parameters', }, types: { enums: 'typescript', - // include: '^CloudProvider', + include: + '^ModelWithPrefixItemsConstantSizeArray|ModelWithAnyOfConstantSizeArray', // name: 'PascalCase', }, // useOptions: false, diff --git a/packages/openapi-ts/test/spec/v3.json b/packages/openapi-ts/test/spec/v3.json index 44c06dcff..351284d21 100644 --- a/packages/openapi-ts/test/spec/v3.json +++ b/packages/openapi-ts/test/spec/v3.json @@ -3184,6 +3184,27 @@ "minItems": 3, "maxItems": 3 }, + "ModelWithPrefixItemsConstantSizeArray": { + "type": "array", + "prefixItems": [ + { + "$ref": "#/components/schemas/ModelWithInteger" + }, + { + "oneOf": [ + { + "type": "number" + }, + { + "type": "string" + } + ] + }, + { + "type": "string" + } + ] + }, "ModelWithAnyOfConstantSizeArrayNullable": { "type": ["array"], "items": {