fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #2279 from hey-api/fix/typescript-property-names

fix(typescript): handle additionalProperties in propertyNames

authored by

Lubos and committed by
GitHub
b85f406e 897cffa4

+30 -12
+5
.changeset/honest-kings-juggle.md
··· 1 + --- 2 + '@hey-api/openapi-ts': patch 3 + --- 4 + 5 + fix(typescript): handle additionalProperties in propertyNames
+4
packages/openapi-ts-tests/test/__snapshots__/3.1.x/object-property-names/types.gen.ts
··· 6 6 [key in Foo]?: string; 7 7 }; 8 8 9 + export type Baz = { 10 + [key in Foo]?: number; 11 + }; 12 + 9 13 export type ClientOptions = { 10 14 baseUrl: `${string}://${string}` | (string & {}); 11 15 };
+2 -7
packages/openapi-ts-tests/test/openapi-ts.config.ts
··· 37 37 // 'invalid', 38 38 // 'servers-entry.yaml', 39 39 // ), 40 - path: path.resolve( 41 - __dirname, 42 - 'spec', 43 - '3.1.x', 44 - 'array-items-one-of-length-1.yaml', 45 - ), 40 + path: path.resolve(__dirname, 'spec', '3.1.x', 'full.yaml'), 46 41 // path: path.resolve(__dirname, 'spec', 'v3-transforms.json'), 47 42 // path: path.resolve(__dirname, 'spec', 'v3.json'), 48 43 // path: 'http://localhost:4000/', ··· 113 108 // name: '{{name}}', 114 109 }, 115 110 readWrite: { 116 - // enabled: false, 111 + enabled: false, 117 112 requests: '{{name}}Writable', 118 113 responses: '{{name}}', 119 114 },
+6
packages/openapi-ts-tests/test/spec/3.1.x/object-property-names.yaml
··· 15 15 propertyNames: 16 16 $ref: '#/components/schemas/Foo' 17 17 type: object 18 + Baz: 19 + additionalProperties: 20 + type: integer 21 + propertyNames: 22 + $ref: '#/components/schemas/Foo' 23 + type: object
+2 -1
packages/openapi-ts/src/compiler/typedef.ts
··· 129 129 if (!properties.length && indexKey) { 130 130 const indexSignature = createMappedTypeNode({ 131 131 questionToken: ts.factory.createToken(ts.SyntaxKind.QuestionToken), 132 - type: createKeywordTypeNode({ keyword: 'string' }), 132 + type: 133 + indexProperty.type ?? createKeywordTypeNode({ keyword: 'string' }), 133 134 typeParameter: createTypeParameterDeclaration({ 134 135 constraint: createTypeReferenceNode({ typeName: indexKey }), 135 136 name: createIdentifier({ text: String(indexProperty.name) }),
+11 -4
packages/openapi-ts/src/plugins/@hey-api/typescript/plugin.ts
··· 5 5 import { operationResponsesMap } from '../../../ir/operation'; 6 6 import { deduplicateSchema } from '../../../ir/schema'; 7 7 import type { IR } from '../../../ir/types'; 8 - import { irRef, isRefOpenApiComponent, refToName } from '../../../utils/ref'; 8 + import { irRef, isRefOpenApiComponent } from '../../../utils/ref'; 9 9 import { numberRegExp } from '../../../utils/regexp'; 10 10 import { stringCase } from '../../../utils/stringCase'; 11 11 import { fieldName } from '../../shared/utils/case'; ··· 462 462 schema: SchemaWithType<'object'>; 463 463 state: State | undefined; 464 464 }): ts.TypeNode | undefined => { 465 + const file = plugin.context.file({ id: typesId })!; 466 + 465 467 // TODO: parser - handle constants 466 468 let indexKey: string | undefined; 467 469 let indexProperty: Property | undefined; ··· 533 535 }), 534 536 }; 535 537 536 - if (schema.propertyNames) { 537 - if (schema.propertyNames.$ref) { 538 - indexKey = refToName(schema.propertyNames.$ref); 538 + if (schema.propertyNames?.$ref) { 539 + const identifier = file.identifier({ 540 + $ref: schema.propertyNames.$ref, 541 + create: true, 542 + namespace: 'type', 543 + }); 544 + if (identifier.name) { 545 + indexKey = identifier.name; 539 546 } 540 547 } 541 548 }