Skip to content

Commit

Permalink
Merge pull request #502 from hey-api/fix/schemas-key-escape
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos authored Apr 27, 2024
2 parents ffd237d + 8112507 commit 13ebddc
Show file tree
Hide file tree
Showing 16 changed files with 47 additions and 31 deletions.
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 @@ export const createObjectType = <T extends object>({
}
// 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("'")
) {
key = `'${key}'`;
}
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 type { OpenApi } from '../../openApi';
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));
}

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

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;
}
}

if (value && typeof value === 'object') {
// @ts-ignore
result[key] = schemaToFormSchema(value);
result[key] = ensureValidSchemaOutput(value);
}
});
return result;
Expand All @@ -46,12 +50,9 @@ export const processSchemas = async ({
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

0 comments on commit 13ebddc

Please sign in to comment.