Skip to content

Fix bug when parsing number typed enum #2261

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

Closed
Closed
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
2 changes: 2 additions & 0 deletions src/client/interfaces/Enum.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export interface Enum {
name: string;
value: string;
enumValue: string;
type: string;
title?: string | null;
description: string | null;
}
1 change: 1 addition & 0 deletions src/client/interfaces/Model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Model extends Schema {
template: string | null;
link: Model | null;
description: string | null;
title?: string | null;
deprecated?: boolean;
default?: string;
imports: string[];
Expand Down
1 change: 1 addition & 0 deletions src/client/interfaces/Operation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface Operation extends OperationParameters {
name: string;
summary: string | null;
description: string | null;
title?: string;
deprecated: boolean;
method: string;
path: string;
Expand Down
2 changes: 2 additions & 0 deletions src/openApi/v2/interfaces/Extensions/WithEnumExtension.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export interface WithEnumExtension {
'x-enum-values'?: string[];
'x-enum-varnames'?: string[];
'x-enum-titles'?: string[];
'x-enum-descriptions'?: string[];
}
4 changes: 4 additions & 0 deletions src/openApi/v2/parser/extendEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import type { WithEnumExtension } from '../interfaces/Extensions/WithEnumExtensi
* @param definition
*/
export const extendEnum = (enumerators: Enum[], definition: WithEnumExtension): Enum[] => {
const values = definition['x-enum-values'];
const names = definition['x-enum-varnames']?.filter(isString);
const titles = definition['x-enum-titles']?.filter(isString);
const descriptions = definition['x-enum-descriptions']?.filter(isString);

return enumerators.map((enumerator, index) => ({
name: names?.[index] || enumerator.name,
description: descriptions?.[index] || enumerator.description,
value: enumerator.value,
enumValue: values?.[index] || enumerator.value,
title: titles?.[index] || enumerator.title,
type: enumerator.type,
}));
};
4 changes: 3 additions & 1 deletion src/openApi/v2/parser/getEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ export const getEnum = (values?: (string | number)[]): Enum[] => {
.map(value => {
if (typeof value === 'number') {
return {
name: `'_${value}'`,
name: `_${value}`,
value: String(value),
enumValue: '',
type: 'number',
description: null,
};
Expand All @@ -25,6 +26,7 @@ export const getEnum = (values?: (string | number)[]): Enum[] => {
.replace(/([a-z])([A-Z]+)/g, '$1_$2')
.toUpperCase(),
value: `'${value.replace(/'/g, "\\'")}'`,
enumValue: '',
type: 'string',
description: null,
};
Expand Down
1 change: 1 addition & 0 deletions src/openApi/v2/parser/getModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const getModel = (
template: null,
link: null,
description: definition.description || null,
title: definition.title,
isDefinition,
isReadOnly: definition.readOnly === true,
isNullable: definition['x-nullable'] === true,
Expand Down
2 changes: 2 additions & 0 deletions src/openApi/v2/parser/getModelProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const getModelProperties = (openApi: OpenApi, definition: OpenApiSchema,
template: model.template,
link: null,
description: property.description || null,
title: property.title,
isDefinition: false,
isReadOnly: property.readOnly === true,
isRequired: propertyRequired,
Expand Down Expand Up @@ -58,6 +59,7 @@ export const getModelProperties = (openApi: OpenApi, definition: OpenApiSchema,
template: model.template,
link: model.link,
description: property.description || null,
title: property.title,
isDefinition: false,
isReadOnly: property.readOnly === true,
isRequired: propertyRequired,
Expand Down
2 changes: 2 additions & 0 deletions src/openApi/v3/interfaces/Extensions/WithEnumExtension.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export interface WithEnumExtension {
'x-enum-values'?: string[];
'x-enum-varnames'?: string[];
'x-enum-titles'?: string[];
'x-enum-descriptions'?: string[];
}
4 changes: 4 additions & 0 deletions src/openApi/v3/parser/extendEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import type { WithEnumExtension } from '../interfaces/Extensions/WithEnumExtensi
* @param definition
*/
export const extendEnum = (enumerators: Enum[], definition: WithEnumExtension): Enum[] => {
const values = definition['x-enum-values'];
const names = definition['x-enum-varnames']?.filter(isString);
const titles = definition['x-enum-titles']?.filter(isString);
const descriptions = definition['x-enum-descriptions']?.filter(isString);

return enumerators.map((enumerator, index) => ({
name: names?.[index] || enumerator.name,
description: descriptions?.[index] || enumerator.description,
value: enumerator.value,
enumValue: values?.[index] || enumerator.value,
title: titles?.[index] || enumerator.title,
type: enumerator.type,
}));
};
4 changes: 3 additions & 1 deletion src/openApi/v3/parser/getEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ export const getEnum = (values?: (string | number)[]): Enum[] => {
.map(value => {
if (typeof value === 'number') {
return {
name: `'_${value}'`,
name: `_${value}`,
value: String(value),
enumValue: '',
type: 'number',
description: null,
};
Expand All @@ -25,6 +26,7 @@ export const getEnum = (values?: (string | number)[]): Enum[] => {
.replace(/([a-z])([A-Z]+)/g, '$1_$2')
.toUpperCase(),
value: `'${value.replace(/'/g, "\\'")}'`,
enumValue: '',
type: 'string',
description: null,
};
Expand Down
1 change: 1 addition & 0 deletions src/openApi/v3/parser/getModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const getModel = (
template: null,
link: null,
description: definition.description || null,
title: definition.title,
deprecated: definition.deprecated === true,
isDefinition,
isReadOnly: definition.readOnly === true,
Expand Down
1 change: 1 addition & 0 deletions src/openApi/v3/parser/getModelProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const getModelProperties = (
> = {
name: escapeName(propertyName),
description: property.description || null,
title: property.title,
deprecated: property.deprecated === true,
isDefinition: false,
isReadOnly: property.readOnly === true,
Expand Down
18 changes: 18 additions & 0 deletions src/templates/core/fetch/getResponseBody.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,26 @@ export const getResponseBody = async (response: Response): Promise<any> => {
if (contentType) {
const jsonTypes = ['application/json', 'application/problem+json']
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
const isBlob = contentType.toLowerCase().startsWith('image/')
|| contentType.toLowerCase().startsWith('application/pdf')
|| contentType.toLowerCase().startsWith('application/zip')
|| contentType.toLowerCase().startsWith('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|| contentType.toLowerCase().startsWith('application/octet-stream');

if (isJSON) {
return await response.json();
} else if (isBlob) {
const blob = await response.blob();
const disposition = response.headers.get('Content-Disposition');
if (disposition && disposition.indexOf('attachment') !== -1) {
const filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
const matches = filenameRegex.exec(disposition);
if (matches !== null && matches[1]) {
const filename = decodeURIComponent(matches[1].replace(/['"]/g, ''));
return new File([blob],filename,{ type: contentType })
}
}
return blob;
} else {
return await response.text();
}
Expand Down
6 changes: 6 additions & 0 deletions src/templates/exportSchema.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{{>header}}

{{#equals export 'enum'}}
{{#unless @root.useUnionTypes}}
import { {{name}} } from '../models/{{{name}}}'
{{/unless}}
{{/equals}}

export const ${{{name}}} = {{>schema}} as const;
6 changes: 6 additions & 0 deletions src/templates/partials/schemaArray.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
{{#if title}}
title: '{{{title}}}',
{{/if}}
{{#if description}}
description: `{{{escapeDescription description}}}`,
{{/if}}
type: 'array',
{{#if link}}
contains: {{>schema link}},
Expand Down
5 changes: 4 additions & 1 deletion src/templates/partials/schemaComposition.hbs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
type: '{{export}}',
{{#if title}}
title: '{{{title}}}',
{{/if}}
{{#if description}}
description: `{{{escapeDescription description}}}`,
{{/if}}
type: '{{export}}',
contains: [{{#each properties}}{{>schema}}{{#unless @last}}, {{/unless}}{{/each}}],
{{#if isReadOnly}}
isReadOnly: {{{isReadOnly}}},
Expand Down
12 changes: 12 additions & 0 deletions src/templates/partials/schemaDictionary.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
{
{{#if title}}
title: '{{{title}}}',
{{/if}}
{{#if description}}
description: `{{{escapeDescription description}}}`,
{{/if}}
type: 'dictionary',
{{#if title}}
title: '{{{title}}}',
{{/if}}
{{#if description}}
description: '{{{description}}}',
{{/if}}
{{#if link}}
contains: {{>schema link}},
{{else}}
Expand Down
21 changes: 21 additions & 0 deletions src/templates/partials/schemaEnum.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
{{#if title}}
title: '{{{title}}}',
{{/if}}
{{#if description}}
description: `{{{escapeDescription description}}}`,
{{/if}}
type: 'Enum',
{{#if isReadOnly}}
isReadOnly: {{{isReadOnly}}},
Expand All @@ -9,4 +15,19 @@
{{#if isNullable}}
isNullable: {{{isNullable}}},
{{/if}}
items: [
{{#each enum}}
{
{{#if @root.useUnionTypes}}
key: {{{value}}},
{{else}}
key: {{{../name}}}.{{{name}}},
{{/if}}
name: '{{{name}}}',
value: {{{enumValue}}},
title: '{{{title}}}',
description: '{{{description}}}',
},
{{/each}}
],
}
7 changes: 5 additions & 2 deletions src/templates/partials/schemaGeneric.hbs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
{{#if type}}
type: '{{{type}}}',
{{#if title}}
title: '{{{title}}}',
{{/if}}
{{#if description}}
description: `{{{escapeDescription description}}}`,
{{/if}}
{{#if type}}
type: '{{{type}}}',
{{/if}}
{{#if isReadOnly}}
isReadOnly: {{{isReadOnly}}},
{{/if}}
Expand Down
3 changes: 3 additions & 0 deletions src/templates/partials/schemaInterface.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
{{#if title}}
title: '{{{title}}}',
{{/if}}
{{#if description}}
description: `{{{escapeDescription description}}}`,
{{/if}}
Expand Down