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: escape keys in schemas starting with digit but and containing non-digit characters #502

Merged
merged 1 commit into from
Apr 27, 2024
Merged
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/few-chefs-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix: escape keys in schemas starting with digit but containing non-digit characters
3 changes: 2 additions & 1 deletion examples/openapi-ts-axios/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"dev": "vite",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"openapi-ts": "openapi-ts",
"preview": "vite preview"
"preview": "vite preview",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@hey-api/client-axios": "workspace:*",
Expand Down
3 changes: 2 additions & 1 deletion examples/openapi-ts-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"dev": "vite",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"openapi-ts": "openapi-ts",
"preview": "vite preview"
"preview": "vite preview",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@hey-api/client-fetch": "workspace:*",
Expand Down
8 changes: 8 additions & 0 deletions packages/openapi-ts/src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@
}
// Check key value equality before possibly modifying it
const hasShorthandSupport = key === value;
if (
key.match(/^[0-9]/) &&
key.match(/\D+/g) &&
!key.startsWith("'") &&
!key.endsWith("'")

Check warning on line 118 in packages/openapi-ts/src/compiler/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/types.ts#L118

Added line #L118 was not covered by tests
) {
key = `'${key}'`;
}

Check warning on line 121 in packages/openapi-ts/src/compiler/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/types.ts#L120-L121

Added lines #L120 - L121 were not covered by tests
if (key.match(/\W/g) && !key.startsWith("'") && !key.endsWith("'")) {
key = `'${key}'`;
}
Expand Down
37 changes: 19 additions & 18 deletions packages/openapi-ts/src/utils/write/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import { ensureValidTypeScriptJavaScriptIdentifier } from '../../openApi/common/parser/sanitize';
import { getConfig } from '../config';

const schemaToFormSchema = (schema: unknown): object => {
const ensureValidSchemaOutput = (schema: unknown): object => {
const config = getConfig();

if (Array.isArray(schema)) {
return schema.map((item) => schemaToFormSchema(item));
return schema.map((item) => ensureValidSchemaOutput(item));

Check warning on line 10 in packages/openapi-ts/src/utils/write/schemas.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/schemas.ts#L10

Added line #L10 was not covered by tests
}

if (typeof schema !== 'object' || schema === null) {
Expand All @@ -14,22 +16,24 @@

const result = { ...schema };
Object.entries(result).forEach(([key, value]) => {
if (
[
'description',
'x-enum-descriptions',
'x-enum-varnames',
'x-enumNames',
].includes(key)
) {
// @ts-ignore
delete result[key];
return;
if (config.schemas.type === 'form') {
if (
[
'description',
'x-enum-descriptions',
'x-enum-varnames',
'x-enumNames',
].includes(key)
) {
// @ts-ignore
delete result[key];
return;
}

Check warning on line 31 in packages/openapi-ts/src/utils/write/schemas.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/schemas.ts#L20-L31

Added lines #L20 - L31 were not covered by tests
}

if (value && typeof value === 'object') {
// @ts-ignore
result[key] = schemaToFormSchema(value);
result[key] = ensureValidSchemaOutput(value);

Check warning on line 36 in packages/openapi-ts/src/utils/write/schemas.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/schemas.ts#L36

Added line #L36 was not covered by tests
}
});
return result;
Expand All @@ -46,12 +50,9 @@
return;
}

const config = getConfig();

const addSchema = (name: string, schema: object) => {
const validName = `$${ensureValidTypeScriptJavaScriptIdentifier(name)}`;
const obj =
config.schemas.type === 'form' ? schemaToFormSchema(schema) : schema;
const obj = ensureValidSchemaOutput(schema);
const expression = compiler.types.object({ obj });
const statement = compiler.export.asConst(validName, expression);
file.add(statement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export const $ArrayWithProperties = {
items: {
type: 'object',
properties: {
foo: {
'16x16': {
'$ref': '#/components/schemas/camelCaseCommentWithBreaks'
},
bar: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export type ArrayWithArray = Array<Array<ModelWithString>>;
* This is a simple array with properties
*/
export type ArrayWithProperties = Array<{
foo?: camelCaseCommentWithBreaks;
'16x16'?: camelCaseCommentWithBreaks;
bar?: string;
}>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export type ArrayWithArray = Array<Array<ModelWithString>>;
* This is a simple array with properties
*/
export type ArrayWithProperties = Array<{
foo?: camelCaseCommentWithBreaks;
'16x16'?: camelCaseCommentWithBreaks;
bar?: string;
}>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export type ArrayWithArray = Array<Array<ModelWithString>>;
* This is a simple array with properties
*/
export type ArrayWithProperties = Array<{
foo?: camelCaseCommentWithBreaks;
'16x16'?: camelCaseCommentWithBreaks;
bar?: string;
}>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export type ArrayWithArray = Array<Array<ModelWithString>>;
* This is a simple array with properties
*/
export type ArrayWithProperties = Array<{
foo?: camelCaseCommentWithBreaks;
'16x16'?: camelCaseCommentWithBreaks;
bar?: string;
}>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export type ArrayWithArray = Array<Array<ModelWithString>>;
* This is a simple array with properties
*/
export type ArrayWithProperties = Array<{
foo?: camelCaseCommentWithBreaks;
'16x16'?: camelCaseCommentWithBreaks;
bar?: string;
}>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export type CamelCaseCommentWithBreaks = number;
* This is a simple array with properties
*/
export type ArrayWithProperties = Array<{
foo?: CamelCaseCommentWithBreaks;
'16x16'?: CamelCaseCommentWithBreaks;
bar?: string;
}>;
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export const $ArrayWithProperties = {
items: {
type: 'object',
properties: {
foo: {
'16x16': {
'$ref': '#/components/schemas/camelCaseCommentWithBreaks'
},
bar: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export const $ArrayWithProperties = {
items: {
type: 'object',
properties: {
foo: {
'16x16': {
'$ref': '#/components/schemas/camelCaseCommentWithBreaks'
},
bar: {
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/test/sample.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const main = async () => {
input: './test/spec/v3.json',
output: './test/generated/v3/',
schemas: {
export: false,
// export: false,
},
services: {
// export: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/test/spec/v3.json
Original file line number Diff line number Diff line change
Expand Up @@ -1797,7 +1797,7 @@
"items": {
"type": "object",
"properties": {
"foo": {
"16x16": {
"$ref": "#/components/schemas/camelCaseCommentWithBreaks"
},
"bar": {
Expand Down