Skip to content

Commit

Permalink
Merge pull request #1326 from hey-api/fix/experimental-parser-enum-names
Browse files Browse the repository at this point in the history
fix: improve generated enum keys in experimental parser
  • Loading branch information
mrlubos authored Nov 22, 2024
2 parents 8976ccd + 01c3d69 commit a9cca25
Show file tree
Hide file tree
Showing 19 changed files with 226 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-houses-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: improve generated enum keys in experimental parser
15 changes: 5 additions & 10 deletions packages/openapi-ts/src/plugins/@hey-api/typescript/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type {
} from '../../../ir/ir';
import { operationResponsesMap } from '../../../ir/operation';
import { deduplicateSchema } from '../../../ir/schema';
import { ensureValidTypeScriptJavaScriptIdentifier } from '../../../openApi';
import { escapeComment } from '../../../utils/escape';
import { irRef, isRefOpenApiComponent } from '../../../utils/ref';
import type { PluginHandler } from '../../types';
Expand Down Expand Up @@ -94,23 +93,19 @@ const schemaToEnumObject = ({ schema }: { schema: IRSchemaObject }) => {
}

let key;

if (item.title) {
key = item.title;
} else if (typeOfItemConst === 'number') {
key = `_${item.const}`;
} else if (typeOfItemConst === 'boolean') {
const valid = typeOfItemConst ? 'true' : 'false';
key = valid.toLocaleUpperCase();
} else {
let valid = ensureValidTypeScriptJavaScriptIdentifier(
item.const as string,
);
if (!valid) {
// TODO: parser - abstract empty string handling
valid = 'empty_string';
}
key = valid.toLocaleUpperCase();
} else if (typeof item.const === 'string') {
key = item.const.replace(/(\p{Lowercase})(\p{Uppercase}+)/gu, '$1_$2');
key = key.toLocaleUpperCase();
}

return {
comments: parseSchemaJsDoc({ schema: item }),
key,
Expand Down
33 changes: 33 additions & 0 deletions packages/openapi-ts/test/3.0.x.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,39 @@ describe(`OpenAPI ${VERSION}`, () => {
}),
description: 'escapes enum values',
},
{
config: createConfig({
input: 'enum-names.json',
output: 'enum-names',
}),
description: 'handles enum names',
},
{
config: createConfig({
input: 'enum-names.json',
output: 'enum-names-javascript',
plugins: [
{
enums: 'javascript',
name: '@hey-api/typescript',
},
],
}),
description: 'handles enum names (JavaScript)',
},
{
config: createConfig({
input: 'enum-names.json',
output: 'enum-names-typescript',
plugins: [
{
enums: 'typescript',
name: '@hey-api/typescript',
},
],
}),
description: 'handles enum names (TypeScript)',
},
{
config: createConfig({
input: 'enum-null.json',
Expand Down
33 changes: 33 additions & 0 deletions packages/openapi-ts/test/3.1.x.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,39 @@ describe(`OpenAPI ${VERSION}`, () => {
}),
description: 'escapes enum values',
},
{
config: createConfig({
input: 'enum-names.json',
output: 'enum-names',
}),
description: 'handles enum names',
},
{
config: createConfig({
input: 'enum-names.json',
output: 'enum-names-javascript',
plugins: [
{
enums: 'javascript',
name: '@hey-api/typescript',
},
],
}),
description: 'handles enum names (JavaScript)',
},
{
config: createConfig({
input: 'enum-names.json',
output: 'enum-names-typescript',
plugins: [
{
enums: 'typescript',
name: '@hey-api/typescript',
},
],
}),
description: 'handles enum names (TypeScript)',
},
{
config: createConfig({
input: 'enum-null.json',
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,22 @@
// This file is auto-generated by @hey-api/openapi-ts

export type _1_10 = '1-10' | '11-20';

export const _1_10 = {
'1-10': '1-10',
'11-20': '11-20'
} as const;

export type myFoo = 'myFoo' | 'myBar';

export const myFoo = {
MY_FOO: 'myFoo',
MY_BAR: 'myBar'
} as const;

export type MyFoo = 'MyFoo' | 'MyBar';

export const MyFoo = {
MY_FOO: 'MyFoo',
MY_BAR: 'MyBar'
} as const;
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,16 @@
// This file is auto-generated by @hey-api/openapi-ts

export enum _1_10 {
'1-10' = '1-10',
'11-20' = '11-20'
}

export enum myFoo {
MY_FOO = 'myFoo',
MY_BAR = 'myBar'
}

export enum MyFoo {
MY_FOO = 'MyFoo',
MY_BAR = 'MyBar'
}
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,7 @@
// This file is auto-generated by @hey-api/openapi-ts

export type _1_10 = '1-10' | '11-20';

export type myFoo = 'myFoo' | 'myBar';

export type MyFoo = 'MyFoo' | 'MyBar';
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,22 @@
// This file is auto-generated by @hey-api/openapi-ts

export type _1_10 = '1-10' | '11-20';

export const _1_10 = {
'1-10': '1-10',
'11-20': '11-20'
} as const;

export type myFoo = 'myFoo' | 'myBar';

export const myFoo = {
MY_FOO: 'myFoo',
MY_BAR: 'myBar'
} as const;

export type MyFoo = 'MyFoo' | 'MyBar';

export const MyFoo = {
MY_FOO: 'MyFoo',
MY_BAR: 'MyBar'
} as const;
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,16 @@
// This file is auto-generated by @hey-api/openapi-ts

export enum _1_10 {
'1-10' = '1-10',
'11-20' = '11-20'
}

export enum myFoo {
MY_FOO = 'myFoo',
MY_BAR = 'myBar'
}

export enum MyFoo {
MY_FOO = 'MyFoo',
MY_BAR = 'MyBar'
}
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,7 @@
// This file is auto-generated by @hey-api/openapi-ts

export type _1_10 = '1-10' | '11-20';

export type myFoo = 'myFoo' | 'myBar';

export type MyFoo = 'MyFoo' | 'MyBar';
4 changes: 2 additions & 2 deletions packages/openapi-ts/test/sample.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const main = async () => {
exclude: '^#/components/schemas/ModelWithCircularReference$',
// include:
// '^(#/components/schemas/import|#/paths/api/v{api-version}/simple/options)$',
path: './test/spec/3.1.x/full.json',
path: './test/spec/3.1.x/enum-names.json',
// path: 'https://mongodb-mms-prod-build-server.s3.amazonaws.com/openapi/2caffd88277a4e27c95dcefc7e3b6a63a3b03297-v2-2023-11-15.json',
},
// name: 'foo',
Expand Down Expand Up @@ -42,7 +42,7 @@ const main = async () => {
{
// enums: 'typescript',
// enums: 'typescript+namespace',
// enums: 'javascript',
enums: 'javascript',
name: '@hey-api/typescript',
// style: 'PascalCase',
// tree: true,
Expand Down
23 changes: 23 additions & 0 deletions packages/openapi-ts/test/spec/3.0.x/enum-names.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"openapi": "3.0.2",
"info": {
"title": "OpenAPI 3.0.2 enum names example",
"version": "1"
},
"components": {
"schemas": {
"1-10": {
"enum": ["1-10", "11-20"],
"type": "string"
},
"myFoo": {
"enum": ["myFoo", "myBar"],
"type": "string"
},
"MyFoo": {
"enum": ["MyFoo", "MyBar"],
"type": "string"
}
}
}
}
23 changes: 23 additions & 0 deletions packages/openapi-ts/test/spec/3.1.x/enum-names.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"openapi": "3.1.1",
"info": {
"title": "OpenAPI 3.1.1 enum names example",
"version": "1"
},
"components": {
"schemas": {
"1-10": {
"enum": ["1-10", "11-20"],
"type": "string"
},
"myFoo": {
"enum": ["myFoo", "myBar"],
"type": "string"
},
"MyFoo": {
"enum": ["MyFoo", "MyBar"],
"type": "string"
}
}
}
}

0 comments on commit a9cca25

Please sign in to comment.