Skip to content

Commit f45ba8e

Browse files
committed
fix: avoid including underscore for appended types (e.g. data, error, response) when preserving identifier case
1 parent 0144c5f commit f45ba8e

File tree

16 files changed

+92
-43
lines changed

16 files changed

+92
-43
lines changed

.changeset/eighty-rivers-enjoy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"@hey-api/openapi-ts": patch
33
---
44

5-
fix: make descriminator field required when used with `oneOf` keyword
5+
fix: make discriminator field required when used with `oneOf` keyword

.changeset/nervous-months-brake.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: avoid including underscore for appended types (e.g. data, error, response) when preserving identifier case

packages/openapi-ts-tests/test/3.0.x.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ describe(`OpenAPI ${version}`, () => {
8989
{
9090
config: createConfig({
9191
input: 'case.json',
92-
output: 'case',
92+
output: 'case-preserve',
9393
plugins: [
9494
{
95-
identifierCase: undefined,
95+
identifierCase: 'preserve',
9696
name: '@hey-api/typescript',
9797
},
9898
],

packages/openapi-ts-tests/test/3.1.x.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ describe(`OpenAPI ${version}`, () => {
8989
{
9090
config: createConfig({
9191
input: 'case.json',
92-
output: 'case',
92+
output: 'case-preserve',
9393
plugins: [
9494
{
95-
identifierCase: undefined,
95+
identifierCase: 'preserve',
9696
name: '@hey-api/typescript',
9797
},
9898
],

packages/openapi-ts-tests/test/__snapshots__/3.0.x/case/types.gen.ts renamed to packages/openapi-ts-tests/test/__snapshots__/3.0.x/case-preserve/types.gen.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export type fooBar = number;
5151
*/
5252
export type FooBar = string;
5353

54-
export type GetFoo_Data = {
54+
export type GetFooData = {
5555
body: Foo;
5656
path?: never;
5757
query: {
@@ -71,7 +71,7 @@ export type GetFoo_Data = {
7171
url: '/foo';
7272
};
7373

74-
export type GetFoo_Responses = {
74+
export type GetFooResponses = {
7575
/**
7676
* OK
7777
*/
@@ -82,7 +82,7 @@ export type GetFoo_Responses = {
8282
201: _201;
8383
};
8484

85-
export type GetFoo_Response = GetFoo_Responses[keyof GetFoo_Responses];
85+
export type GetFooResponse = GetFooResponses[keyof GetFooResponses];
8686

8787
export type ClientOptions = {
8888
baseUrl: `${string}://${string}` | (string & {});

packages/openapi-ts-tests/test/__snapshots__/3.1.x/case/types.gen.ts renamed to packages/openapi-ts-tests/test/__snapshots__/3.1.x/case-preserve/types.gen.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export type fooBar = number;
5151
*/
5252
export type FooBar = string;
5353

54-
export type GetFoo_Data = {
54+
export type GetFooData = {
5555
body: Foo;
5656
path?: never;
5757
query: {
@@ -71,7 +71,7 @@ export type GetFoo_Data = {
7171
url: '/foo';
7272
};
7373

74-
export type GetFoo_Responses = {
74+
export type GetFooResponses = {
7575
/**
7676
* OK
7777
*/
@@ -82,7 +82,7 @@ export type GetFoo_Responses = {
8282
201: _201;
8383
};
8484

85-
export type GetFoo_Response = GetFoo_Responses[keyof GetFoo_Responses];
85+
export type GetFooResponse = GetFooResponses[keyof GetFooResponses];
8686

8787
export type ClientOptions = {
8888
baseUrl: `${string}://${string}` | (string & {});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export default defineConfig(() => {
103103
// enums: 'javascript',
104104
// enumsCase: 'camelCase',
105105
// exportInlineEnums: true,
106-
// identifierCase: 'preserve',
106+
identifierCase: 'preserve',
107107
name: '@hey-api/typescript',
108108
// readOnlyWriteOnlyBehavior: 'off',
109109
// readableNameBuilder: 'Readable{{name}}',

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ const operationStatements = ({
398398
const identifierSchema = context.file({ id: zodId })!.identifier({
399399
$ref: operationIrRef({
400400
case: 'camelCase',
401+
config: context.config,
401402
id: operation.id,
402403
type: 'response',
403404
}),

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,11 @@ export const handler: Plugin.Handler<Config> = ({ context, plugin }) => {
469469
}
470470

471471
const identifierResponse = context.file({ id: typesId })!.identifier({
472-
$ref: operationIrRef({ id: operation.id, type: 'response' }),
472+
$ref: operationIrRef({
473+
config: context.config,
474+
id: operation.id,
475+
type: 'response',
476+
}),
473477
namespace: 'type',
474478
});
475479
if (!identifierResponse.name) {

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,11 @@ const operationToDataType = ({
904904
data.required = dataRequired;
905905

906906
const identifier = file.identifier({
907-
$ref: operationIrRef({ id: operation.id, type: 'data' }),
907+
$ref: operationIrRef({
908+
config: context.config,
909+
id: operation.id,
910+
type: 'data',
911+
}),
908912
create: true,
909913
namespace: 'type',
910914
});
@@ -952,7 +956,11 @@ const operationToType = ({
952956

953957
if (errors) {
954958
const identifierErrors = file.identifier({
955-
$ref: operationIrRef({ id: operation.id, type: 'errors' }),
959+
$ref: operationIrRef({
960+
config: context.config,
961+
id: operation.id,
962+
type: 'errors',
963+
}),
956964
create: true,
957965
namespace: 'type',
958966
});
@@ -980,7 +988,11 @@ const operationToType = ({
980988

981989
if (error) {
982990
const identifierError = file.identifier({
983-
$ref: operationIrRef({ id: operation.id, type: 'error' }),
991+
$ref: operationIrRef({
992+
config: context.config,
993+
id: operation.id,
994+
type: 'error',
995+
}),
984996
create: true,
985997
namespace: 'type',
986998
});
@@ -1008,7 +1020,11 @@ const operationToType = ({
10081020

10091021
if (responses) {
10101022
const identifierResponses = file.identifier({
1011-
$ref: operationIrRef({ id: operation.id, type: 'responses' }),
1023+
$ref: operationIrRef({
1024+
config: context.config,
1025+
id: operation.id,
1026+
type: 'responses',
1027+
}),
10121028
create: true,
10131029
namespace: 'type',
10141030
});
@@ -1036,7 +1052,11 @@ const operationToType = ({
10361052

10371053
if (response) {
10381054
const identifierResponse = file.identifier({
1039-
$ref: operationIrRef({ id: operation.id, type: 'response' }),
1055+
$ref: operationIrRef({
1056+
config: context.config,
1057+
id: operation.id,
1058+
type: 'response',
1059+
}),
10401060
create: true,
10411061
namespace: 'type',
10421062
});

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ export const importIdentifierData = ({
2727
operation: IR.OperationObject;
2828
}): Identifier => {
2929
const identifierData = context.file({ id: typesId })!.identifier({
30-
$ref: operationIrRef({ id: operation.id, type: 'data' }),
30+
$ref: operationIrRef({
31+
config: context.config,
32+
id: operation.id,
33+
type: 'data',
34+
}),
3135
namespace: 'type',
3236
});
3337
return refIdentifier(identifierData, (identifier) => {
@@ -51,7 +55,11 @@ export const importIdentifierError = ({
5155
operation: IR.OperationObject;
5256
}): Identifier => {
5357
const identifierError = context.file({ id: typesId })!.identifier({
54-
$ref: operationIrRef({ id: operation.id, type: 'error' }),
58+
$ref: operationIrRef({
59+
config: context.config,
60+
id: operation.id,
61+
type: 'error',
62+
}),
5563
namespace: 'type',
5664
});
5765
return refIdentifier(identifierError, (identifier) => {
@@ -75,7 +83,11 @@ export const importIdentifierResponse = ({
7583
operation: IR.OperationObject;
7684
}): Identifier => {
7785
const identifierResponse = context.file({ id: typesId })!.identifier({
78-
$ref: operationIrRef({ id: operation.id, type: 'response' }),
86+
$ref: operationIrRef({
87+
config: context.config,
88+
id: operation.id,
89+
type: 'response',
90+
}),
7991
namespace: 'type',
8092
});
8193
return refIdentifier(identifierResponse, (identifier) => {

packages/openapi-ts/src/plugins/fastify/plugin.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ const operationToRouteHandler = ({
2424
const properties: Array<Property> = [];
2525

2626
const identifierData = fileTypes.identifier({
27-
$ref: operationIrRef({ id: operation.id, type: 'data' }),
27+
$ref: operationIrRef({
28+
config: context.config,
29+
id: operation.id,
30+
type: 'data',
31+
}),
2832
namespace: 'type',
2933
});
3034
if (identifierData.name) {
@@ -93,7 +97,11 @@ const operationToRouteHandler = ({
9397

9498
let errorsTypeReference: ts.TypeReferenceNode | undefined = undefined;
9599
const identifierErrors = fileTypes.identifier({
96-
$ref: operationIrRef({ id: operation.id, type: 'errors' }),
100+
$ref: operationIrRef({
101+
config: context.config,
102+
id: operation.id,
103+
type: 'errors',
104+
}),
97105
namespace: 'type',
98106
});
99107
if (identifierErrors.name && errors && errors.properties) {
@@ -131,7 +139,11 @@ const operationToRouteHandler = ({
131139

132140
let responsesTypeReference: ts.TypeReferenceNode | undefined = undefined;
133141
const identifierResponses = fileTypes.identifier({
134-
$ref: operationIrRef({ id: operation.id, type: 'responses' }),
142+
$ref: operationIrRef({
143+
config: context.config,
144+
id: operation.id,
145+
type: 'responses',
146+
}),
135147
namespace: 'type',
136148
});
137149
if (identifierResponses.name && responses && responses.properties) {
Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { StringCase } from '../../../types/config';
1+
import type { Config, StringCase } from '../../../types/config';
22
import { irRef } from '../../../utils/ref';
33
import { stringCase } from '../../../utils/stringCase';
44

@@ -11,36 +11,30 @@ interface OperationIRRef {
1111

1212
export const operationIrRef = ({
1313
case: _case = 'PascalCase',
14+
config,
1415
id,
1516
type,
1617
}: OperationIRRef & {
1718
readonly case?: StringCase;
19+
config: Pick<Config, 'plugins'>;
1820
type: 'data' | 'error' | 'errors' | 'response' | 'responses';
1921
}): string => {
2022
let affix = '';
2123
switch (type) {
2224
case 'data':
23-
affix = 'Data';
24-
break;
25-
case 'error':
26-
// error union
27-
affix = 'Error';
28-
break;
29-
case 'errors':
30-
// errors map
31-
affix = 'Errors';
32-
break;
33-
case 'response':
34-
// response union
35-
affix = 'Response';
36-
break;
37-
case 'responses':
38-
// responses map
39-
affix = 'Responses';
25+
case 'error': // error union
26+
case 'errors': // errors map
27+
case 'response': // response union
28+
case 'responses': // responses map
29+
affix = `${(type[0] ?? '').toLocaleUpperCase()}${type.slice(1)}`;
4030
break;
4131
}
32+
let separator = true;
33+
if (config.plugins['@hey-api/typescript']?.identifierCase === 'preserve') {
34+
separator = false;
35+
}
4236
return `${irRef}${stringCase({
4337
case: _case,
4438
value: id,
45-
})}-${affix}`;
39+
})}${separator ? '-' : ''}${affix}`;
4640
};

packages/openapi-ts/src/plugins/zod/plugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,7 @@ const operationToZodSchema = ({
809809
schemaToZodSchema({
810810
$ref: operationIrRef({
811811
case: 'camelCase',
812+
config: context.config,
812813
id: operation.id,
813814
type: 'response',
814815
}),

0 commit comments

Comments
 (0)