Skip to content

Commit b85f406

Browse files
authored
Merge pull request #2279 from hey-api/fix/typescript-property-names
fix(typescript): handle additionalProperties in propertyNames
2 parents 897cffa + f0549f6 commit b85f406

File tree

6 files changed

+30
-12
lines changed

6 files changed

+30
-12
lines changed

.changeset/honest-kings-juggle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hey-api/openapi-ts': patch
3+
---
4+
5+
fix(typescript): handle additionalProperties in propertyNames

packages/openapi-ts-tests/test/__snapshots__/3.1.x/object-property-names/types.gen.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ export type Bar = {
66
[key in Foo]?: string;
77
};
88

9+
export type Baz = {
10+
[key in Foo]?: number;
11+
};
12+
913
export type ClientOptions = {
1014
baseUrl: `${string}://${string}` | (string & {});
1115
};

packages/openapi-ts-tests/test/openapi-ts.config.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,7 @@ export default defineConfig(() => {
3737
// 'invalid',
3838
// 'servers-entry.yaml',
3939
// ),
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'),
4641
// path: path.resolve(__dirname, 'spec', 'v3-transforms.json'),
4742
// path: path.resolve(__dirname, 'spec', 'v3.json'),
4843
// path: 'http://localhost:4000/',
@@ -113,7 +108,7 @@ export default defineConfig(() => {
113108
// name: '{{name}}',
114109
},
115110
readWrite: {
116-
// enabled: false,
111+
enabled: false,
117112
requests: '{{name}}Writable',
118113
responses: '{{name}}',
119114
},

packages/openapi-ts-tests/test/spec/3.1.x/object-property-names.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ components:
1515
propertyNames:
1616
$ref: '#/components/schemas/Foo'
1717
type: object
18+
Baz:
19+
additionalProperties:
20+
type: integer
21+
propertyNames:
22+
$ref: '#/components/schemas/Foo'
23+
type: object

packages/openapi-ts/src/compiler/typedef.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ export const createTypeInterfaceNode = ({
129129
if (!properties.length && indexKey) {
130130
const indexSignature = createMappedTypeNode({
131131
questionToken: ts.factory.createToken(ts.SyntaxKind.QuestionToken),
132-
type: createKeywordTypeNode({ keyword: 'string' }),
132+
type:
133+
indexProperty.type ?? createKeywordTypeNode({ keyword: 'string' }),
133134
typeParameter: createTypeParameterDeclaration({
134135
constraint: createTypeReferenceNode({ typeName: indexKey }),
135136
name: createIdentifier({ text: String(indexProperty.name) }),

packages/openapi-ts/src/plugins/@hey-api/typescript/plugin.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { compiler } from '../../../compiler';
55
import { operationResponsesMap } from '../../../ir/operation';
66
import { deduplicateSchema } from '../../../ir/schema';
77
import type { IR } from '../../../ir/types';
8-
import { irRef, isRefOpenApiComponent, refToName } from '../../../utils/ref';
8+
import { irRef, isRefOpenApiComponent } from '../../../utils/ref';
99
import { numberRegExp } from '../../../utils/regexp';
1010
import { stringCase } from '../../../utils/stringCase';
1111
import { fieldName } from '../../shared/utils/case';
@@ -462,6 +462,8 @@ const objectTypeToIdentifier = ({
462462
schema: SchemaWithType<'object'>;
463463
state: State | undefined;
464464
}): ts.TypeNode | undefined => {
465+
const file = plugin.context.file({ id: typesId })!;
466+
465467
// TODO: parser - handle constants
466468
let indexKey: string | undefined;
467469
let indexProperty: Property | undefined;
@@ -533,9 +535,14 @@ const objectTypeToIdentifier = ({
533535
}),
534536
};
535537

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;
539546
}
540547
}
541548
}

0 commit comments

Comments
 (0)