- Added options to handle reserved property names

This commit is contained in:
Ferdi Koomen 2021-03-12 22:34:16 +01:00
parent 8368025520
commit 405df66faf
6 changed files with 56 additions and 3 deletions

View File

@ -1,6 +1,6 @@
{
"name": "openapi-typescript-codegen",
"version": "0.9.1",
"version": "0.9.2",
"description": "Library that generates Typescript clients based on the OpenAPI specification.",
"author": "Ferdi Koomen",
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",

View File

@ -1,5 +1,7 @@
import camelCase from 'camelcase';
const reservedWords = /^(arguments|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|eval|export|extends|false|finally|for|function|if|implements|import|in|instanceof|interface|let|new|null|package|private|protected|public|return|static|super|switch|this|throw|true|try|typeof|var|void|while|with|yield)$/g;
/**
* Replaces any invalid characters from a parameter name.
* For example: 'filter.someProperty' becomes 'filterSomeProperty'.
@ -9,5 +11,5 @@ export function getOperationParameterName(value: string): string {
.replace(/^[^a-zA-Z]+/g, '')
.replace(/[^\w\-]+/g, '-')
.trim();
return camelCase(clean);
return camelCase(clean).replace(reservedWords, '_$1');
}

View File

@ -1,5 +1,7 @@
import camelCase from 'camelcase';
const reservedWords = /^(arguments|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|eval|export|extends|false|finally|for|function|if|implements|import|in|instanceof|interface|let|new|null|package|private|protected|public|return|static|super|switch|this|throw|true|try|typeof|var|void|while|with|yield)$/g;
/**
* Replaces any invalid characters from a parameter name.
* For example: 'filter.someProperty' becomes 'filterSomeProperty'.
@ -9,5 +11,5 @@ export function getOperationParameterName(value: string): string {
.replace(/^[^a-zA-Z]+/g, '')
.replace(/[^\w\-]+/g, '-')
.trim();
return camelCase(clean);
return camelCase(clean).replace(reservedWords, '_$1');
}

View File

@ -951,6 +951,8 @@ export type ModelWithProperties = {
boolean?: boolean;
reference?: ModelWithString;
'property with space'?: string;
default?: string;
try?: string;
readonly '@namespace.string'?: string;
readonly '@namespace.integer'?: number;
}
@ -1630,6 +1632,12 @@ export const $ModelWithProperties = {
'property with space': {
type: 'string',
},
default: {
type: 'string',
},
try: {
type: 'string',
},
'@namespace.string': {
type: 'string',
isReadOnly: true,
@ -2059,6 +2067,7 @@ export class ParametersService {
* @param parameterPath1 This is the parameter that goes into the path
* @param parameterPath2 This is the parameter that goes into the path
* @param parameterPath3 This is the parameter that goes into the path
* @param _default This is the parameter with a reserved keyword
* @throws ApiError
*/
public static async callWithWeirdParameterNames(
@ -2069,6 +2078,7 @@ export class ParametersService {
parameterPath1?: string,
parameterPath2?: string,
parameterPath3?: string,
_default?: string,
): Promise<void> {
const result = await __request({
method: 'GET',
@ -3363,6 +3373,8 @@ export type ModelWithProperties = {
boolean?: boolean;
reference?: ModelWithString;
'property with space'?: string;
default?: string;
try?: string;
readonly '@namespace.string'?: string;
readonly '@namespace.integer'?: number;
}
@ -4197,6 +4209,12 @@ export const $ModelWithProperties = {
'property with space': {
type: 'string',
},
default: {
type: 'string',
},
try: {
type: 'string',
},
'@namespace.string': {
type: 'string',
isReadOnly: true,
@ -4697,6 +4715,7 @@ export class ParametersService {
* @param parameterPath1 This is the parameter that goes into the path
* @param parameterPath2 This is the parameter that goes into the path
* @param parameterPath3 This is the parameter that goes into the path
* @param _default This is the parameter with a reserved keyword
* @throws ApiError
*/
public static async callWithWeirdParameterNames(
@ -4708,6 +4727,7 @@ export class ParametersService {
parameterPath1?: string,
parameterPath2?: string,
parameterPath3?: string,
_default?: string,
): Promise<void> {
const result = await __request({
method: 'GET',

View File

@ -133,6 +133,13 @@
"type": "string",
"required": false
},
{
"description": "This is the parameter with a reserved keyword",
"name": "default",
"in": "path",
"type": "string",
"required": false
},
{
"description": "This is the parameter that goes into the request header",
"name": "parameter.header",
@ -1141,6 +1148,12 @@
"property with space": {
"type": "string"
},
"default": {
"type": "string"
},
"try": {
"type": "string"
},
"@namespace.string": {
"type": "string",
"readOnly": true

View File

@ -173,6 +173,16 @@
"type": "string"
}
},
{
"description": "This is the parameter with a reserved keyword",
"name": "default",
"in": "path",
"required": false,
"nullable": false,
"schema": {
"type": "string"
}
},
{
"description": "This is the parameter that goes into the request header",
"name": "parameter.header",
@ -1804,6 +1814,12 @@
"property with space": {
"type": "string"
},
"default": {
"type": "string"
},
"try": {
"type": "string"
},
"@namespace.string": {
"type": "string",
"readOnly": true