Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle negative property names correctly in case and zod plugins #1919

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/popular-poems-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

"Fix handling of negative property names in TypeScript code generation"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Fix handling of negative property names in TypeScript code generation"
fix: correctly handle numeric property names with signs

7 changes: 7 additions & 0 deletions packages/openapi-ts-tests/test/3.1.x.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,13 @@ describe(`OpenAPI ${version}`, () => {
description:
'does not set oneOf composition ref model properties as required',
},
{
config: createConfig({
input: 'negative-property-names.json',
output: 'negative-property-names',
}),
description: 'handles negative property names correctly',
},
{
config: createConfig({
input: 'schema-const.yaml',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This file is auto-generated by @hey-api/openapi-ts
export * from './types.gen';
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// This file is auto-generated by @hey-api/openapi-ts

export type ReactionRollup = {
url: string;
total_count: number;
'+1': number;
"-1": number;
laugh: number;
confused: number;
heart: number;
hooray: number;
eyes: number;
rocket: number;
};

export type ClientOptions = {
baseUrl: `${string}://${string}` | (string & {});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"openapi": "3.1.1",
"info": {
"title": "OpenAPI 3.1.1 negative property names example",
"version": "1"
},
"components": {
"schemas": {
"ReactionRollup": {
"title": "Reaction Rollup",
"type": "object",
"properties": {
"url": {
"format": "uri",
"type": "string"
},
"total_count": {
"type": "integer"
},
"+1": {
"type": "integer"
},
"-1": {
"type": "integer"
},
"laugh": {
"type": "integer"
},
"confused": {
"type": "integer"
},
"heart": {
"type": "integer"
},
"hooray": {
"type": "integer"
},
"eyes": {
"type": "integer"
},
"rocket": {
"type": "integer"
}
},
"required": [
"url",
"total_count",
"+1",
"-1",
"laugh",
"confused",
"heart",
"hooray",
"eyes",
"rocket"
]
}
}
}
}
4 changes: 4 additions & 0 deletions packages/openapi-ts/src/plugins/shared/utils/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
}) => {
numberRegExp.lastIndex = 0;
if (numberRegExp.test(name)) {
// For negative numbers, use string literals instead
if (name.startsWith('-')) {
return ts.factory.createStringLiteral(name);
}

Check warning on line 23 in packages/openapi-ts/src/plugins/shared/utils/case.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/shared/utils/case.ts#L20-L23

Added lines #L20 - L23 were not covered by tests
return ts.factory.createNumericLiteral(name);
}

Expand Down
13 changes: 10 additions & 3 deletions packages/openapi-ts/src/plugins/zod/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,16 @@
});

numberRegExp.lastIndex = 0;
let propertyName = numberRegExp.test(name)
? ts.factory.createNumericLiteral(name)
: name;
let propertyName;
if (numberRegExp.test(name)) {
// For numeric literals, we'll handle negative numbers by using a string literal
// instead of trying to use a PrefixUnaryExpression
propertyName = name.startsWith('-')
? ts.factory.createStringLiteral(name)
: ts.factory.createNumericLiteral(name);
} else {
propertyName = name;
}

Check warning on line 372 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L363-L372

Added lines #L363 - L372 were not covered by tests
// TODO: parser - abstract safe property name logic
if (
((name.match(/^[0-9]/) && name.match(/\D+/g)) || name.match(/\W/g)) &&
Expand Down