Skip to content

Commit

Permalink
[typescript-fetch] Use == to check property is null or undefined (#…
Browse files Browse the repository at this point in the history
…17972)

* use `==` operator to check null or undefined

* update the sample
  • Loading branch information
kota65535 authored Mar 9, 2024
1 parent 31cff44 commit 5d959ee
Show file tree
Hide file tree
Showing 208 changed files with 1,565 additions and 2,088 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class {{classname}} extends runtime.BaseAPI {
async {{nickname}}Raw({{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request, {{/allParams.0}}initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<{{{returnType}}}{{^returnType}}void{{/returnType}}>> {
{{#allParams}}
{{#required}}
if (!runtime.exists(requestParameters, '{{paramName}}')) {
if (requestParameters['{{paramName}}'] == null) {
throw new runtime.RequiredError(
'{{paramName}}',
'Required parameter "{{paramName}}" was null or undefined when calling {{nickname}}().'
Expand All @@ -120,7 +120,7 @@ export class {{classname}} extends runtime.BaseAPI {
{{#queryParams}}
{{#isArray}}
if (runtime.exists(requestParameters, '{{paramName}}')) {
if (requestParameters['{{paramName}}'] != null) {
{{#isCollectionFormatMulti}}
queryParameters['{{baseName}}'] = requestParameters['{{paramName}}'];
{{/isCollectionFormatMulti}}
Expand All @@ -131,7 +131,7 @@ export class {{classname}} extends runtime.BaseAPI {
{{/isArray}}
{{^isArray}}
if (runtime.exists(requestParameters, '{{paramName}}')) {
if (requestParameters['{{paramName}}'] != null) {
{{#isDateTimeType}}
queryParameters['{{baseName}}'] = (requestParameters['{{paramName}}'] as any).toISOString();
{{/isDateTimeType}}
Expand Down Expand Up @@ -161,13 +161,13 @@ export class {{classname}} extends runtime.BaseAPI {
{{/bodyParam}}
{{#headerParams}}
{{#isArray}}
if (runtime.exists(requestParameters, '{{paramName}}')) {
if (requestParameters['{{paramName}}'] != null) {
headerParameters['{{baseName}}'] = {{#uniqueItems}}Array.from({{/uniqueItems}}requestParameters['{{paramName}}']{{#uniqueItems}}){{/uniqueItems}}!.join(runtime.COLLECTION_FORMATS["{{collectionFormat}}"]);
}
{{/isArray}}
{{^isArray}}
if (runtime.exists(requestParameters, '{{paramName}}')) {
if (requestParameters['{{paramName}}'] != null) {
headerParameters['{{baseName}}'] = String(requestParameters['{{paramName}}']);
}
Expand Down Expand Up @@ -238,7 +238,7 @@ export class {{classname}} extends runtime.BaseAPI {
{{#formParams}}
{{#isArray}}
if (runtime.exists(requestParameters, '{{paramName}}')) {
if (requestParameters['{{paramName}}'] != null) {
{{#isCollectionFormatMulti}}
requestParameters['{{paramName}}'].forEach((element) => {
formParams.append('{{baseName}}', element as any);
Expand All @@ -251,7 +251,7 @@ export class {{classname}} extends runtime.BaseAPI {
{{/isArray}}
{{^isArray}}
if (runtime.exists(requestParameters, '{{paramName}}')) {
if (requestParameters['{{paramName}}'] != null) {
{{#isPrimitiveType}}
formParams.append('{{baseName}}', requestParameters['{{paramName}}'] as any);
{{/isPrimitiveType}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exists, mapValues } from '../runtime{{importFileExtension}}';
import { mapValues } from '../runtime{{importFileExtension}}';
{{#hasImports}}
{{#imports}}
import type { {{{.}}} } from './{{.}}{{importFileExtension}}';
Expand Down Expand Up @@ -38,7 +38,7 @@ export function {{classname}}FromJSON(json: any): {{classname}} {

export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boolean): {{classname}} {
{{#hasVars}}
if (json === undefined || json === null) {
if (json == null) {
return json;
}
{{#discriminator}}
Expand All @@ -58,36 +58,36 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole
{{#vars}}
{{#isPrimitiveType}}
{{#isDateType}}
'{{name}}': {{^required}}!exists(json, '{{baseName}}') ? undefined : {{/required}}({{#required}}{{#isNullable}}!exists(json, '{{baseName}}') ? null : {{/isNullable}}{{/required}}new Date(json['{{baseName}}'])),
'{{name}}': {{^required}}json['{{baseName}}'] == null ? undefined : {{/required}}({{#required}}{{#isNullable}}json['{{baseName}}'] == null ? null : {{/isNullable}}{{/required}}new Date(json['{{baseName}}'])),
{{/isDateType}}
{{#isDateTimeType}}
'{{name}}': {{^required}}!exists(json, '{{baseName}}') ? undefined : {{/required}}({{#required}}{{#isNullable}}!exists(json, '{{baseName}}') ? null : {{/isNullable}}{{/required}}new Date(json['{{baseName}}'])),
'{{name}}': {{^required}}json['{{baseName}}'] == null ? undefined : {{/required}}({{#required}}{{#isNullable}}json['{{baseName}}'] == null ? null : {{/isNullable}}{{/required}}new Date(json['{{baseName}}'])),
{{/isDateTimeType}}
{{^isDateType}}
{{^isDateTimeType}}
'{{name}}': {{^required}}!exists(json, '{{baseName}}') ? undefined : {{/required}}json['{{baseName}}'],
'{{name}}': {{^required}}json['{{baseName}}'] == null ? undefined : {{/required}}json['{{baseName}}'],
{{/isDateTimeType}}
{{/isDateType}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{#isArray}}
{{#uniqueItems}}
'{{name}}': {{^required}}!exists(json, '{{baseName}}') ? undefined : {{/required}}{{#required}}({{#isNullable}}!exists(json, '{{baseName}}') ? null : {{/isNullable}}{{/required}}new Set((json['{{baseName}}'] as Array<any>).map({{#items}}{{datatype}}{{/items}}FromJSON))),
'{{name}}': {{^required}}json['{{baseName}}'] == null ? undefined : {{/required}}{{#required}}({{#isNullable}}json['{{baseName}}'] == null ? null : {{/isNullable}}{{/required}}new Set((json['{{baseName}}'] as Array<any>).map({{#items}}{{datatype}}{{/items}}FromJSON))),
{{/uniqueItems}}
{{^uniqueItems}}
'{{name}}': {{^required}}!exists(json, '{{baseName}}') ? undefined : {{/required}}({{#required}}{{#isNullable}}!exists(json, '{{baseName}}') ? null : {{/isNullable}}{{/required}}(json['{{baseName}}'] as Array<any>).map({{#items}}{{datatype}}{{/items}}FromJSON)),
'{{name}}': {{^required}}json['{{baseName}}'] == null ? undefined : {{/required}}({{#required}}{{#isNullable}}json['{{baseName}}'] == null ? null : {{/isNullable}}{{/required}}(json['{{baseName}}'] as Array<any>).map({{#items}}{{datatype}}{{/items}}FromJSON)),
{{/uniqueItems}}
{{/isArray}}
{{#isMap}}
'{{name}}': {{^required}}!exists(json, '{{baseName}}') ? undefined : {{/required}}({{#required}}{{#isNullable}}!exists(json, '{{baseName}}') ? null : {{/isNullable}}{{/required}}mapValues(json['{{baseName}}'], {{#items}}{{datatype}}{{/items}}FromJSON)),
'{{name}}': {{^required}}json['{{baseName}}'] == null ? undefined : {{/required}}({{#required}}{{#isNullable}}json['{{baseName}}'] == null ? null : {{/isNullable}}{{/required}}mapValues(json['{{baseName}}'], {{#items}}{{datatype}}{{/items}}FromJSON)),
{{/isMap}}
{{^isArray}}
{{^isMap}}
{{^isFreeFormObject}}
'{{name}}': {{^required}}!exists(json, '{{baseName}}') ? undefined : {{/required}}{{datatype}}FromJSON(json['{{baseName}}']),
'{{name}}': {{^required}}json['{{baseName}}'] == null ? undefined : {{/required}}{{datatype}}FromJSON(json['{{baseName}}']),
{{/isFreeFormObject}}
{{#isFreeFormObject}}
'{{name}}': {{^required}}!exists(json, '{{baseName}}') ? undefined : {{/required}}json['{{baseName}}'],
'{{name}}': {{^required}}json['{{baseName}}'] == null ? undefined : {{/required}}json['{{baseName}}'],
{{/isFreeFormObject}}
{{/isMap}}
{{/isArray}}
Expand All @@ -102,11 +102,8 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole

export function {{classname}}ToJSON(value?: {{classname}} | null): any {
{{#hasVars}}
if (value === undefined) {
return undefined;
}
if (value === null) {
return null;
if (value == null) {
return value;
}
return {
{{#parent}}...{{{.}}}ToJSON(value),{{/parent}}
Expand All @@ -117,13 +114,13 @@ export function {{classname}}ToJSON(value?: {{classname}} | null): any {
{{^isReadOnly}}
{{#isPrimitiveType}}
{{#isDateType}}
'{{baseName}}': {{^required}}!exists(value, '{{name}}') ? undefined : {{/required}}({{#required}}{{#isNullable}}!exists(value, '{{name}}') ? null : {{/isNullable}}{{/required}}(value['{{name}}']{{#isNullable}} as any{{/isNullable}}).toISOString().substring(0,10)),
'{{baseName}}': {{^required}}value['{{name}}'] == null ? undefined : {{/required}}({{#required}}{{#isNullable}}value['{{name}}'] == null ? null : {{/isNullable}}{{/required}}(value['{{name}}']{{#isNullable}} as any{{/isNullable}}).toISOString().substring(0,10)),
{{/isDateType}}
{{#isDateTimeType}}
'{{baseName}}': {{^required}}!exists(value, '{{name}}') ? undefined : {{/required}}({{#required}}{{#isNullable}}!exists(value, '{{name}}') ? null : {{/isNullable}}{{/required}}(value['{{name}}']{{#isNullable}} as any{{/isNullable}}).toISOString()),
'{{baseName}}': {{^required}}value['{{name}}'] == null ? undefined : {{/required}}({{#required}}{{#isNullable}}value['{{name}}'] == null ? null : {{/isNullable}}{{/required}}(value['{{name}}']{{#isNullable}} as any{{/isNullable}}).toISOString()),
{{/isDateTimeType}}
{{#isArray}}
'{{baseName}}': {{#uniqueItems}}{{^required}}!exists(value, '{{name}}') ? undefined : {{/required}}{{#required}}{{#isNullable}}!exists(value, '{{name}}') ? null : {{/isNullable}}{{/required}}Array.from(value['{{name}}'] as Set<any>){{/uniqueItems}}{{^uniqueItems}}value['{{name}}']{{/uniqueItems}},
'{{baseName}}': {{#uniqueItems}}{{^required}}value['{{name}}'] == null ? undefined : {{/required}}{{#required}}{{#isNullable}}value['{{name}}'] == null ? null : {{/isNullable}}{{/required}}Array.from(value['{{name}}'] as Set<any>){{/uniqueItems}}{{^uniqueItems}}value['{{name}}']{{/uniqueItems}},
{{/isArray}}
{{^isDateType}}
{{^isDateTimeType}}
Expand All @@ -136,14 +133,14 @@ export function {{classname}}ToJSON(value?: {{classname}} | null): any {
{{^isPrimitiveType}}
{{#isArray}}
{{#uniqueItems}}
'{{baseName}}': {{^required}}!exists(value, '{{name}}') ? undefined : {{/required}}({{#required}}{{#isNullable}}!exists(value, '{{name}}') ? null : {{/isNullable}}{{/required}}Array.from(value['{{name}}'] as Set<any>).map({{#items}}{{datatype}}{{/items}}ToJSON)),
'{{baseName}}': {{^required}}value['{{name}}'] == null ? undefined : {{/required}}({{#required}}{{#isNullable}}value['{{name}}'] == null ? null : {{/isNullable}}{{/required}}Array.from(value['{{name}}'] as Set<any>).map({{#items}}{{datatype}}{{/items}}ToJSON)),
{{/uniqueItems}}
{{^uniqueItems}}
'{{baseName}}': {{^required}}!exists(value, '{{name}}') ? undefined : {{/required}}({{#required}}{{#isNullable}}!exists(value, '{{name}}') ? null : {{/isNullable}}{{/required}}(value['{{name}}'] as Array<any>).map({{#items}}{{datatype}}{{/items}}ToJSON)),
'{{baseName}}': {{^required}}value['{{name}}'] == null ? undefined : {{/required}}({{#required}}{{#isNullable}}value['{{name}}'] == null ? null : {{/isNullable}}{{/required}}(value['{{name}}'] as Array<any>).map({{#items}}{{datatype}}{{/items}}ToJSON)),
{{/uniqueItems}}
{{/isArray}}
{{#isMap}}
'{{baseName}}': {{^required}}!exists(value, '{{name}}') ? undefined : {{/required}}({{#required}}{{#isNullable}}!exists(value, '{{name}}') ? null : {{/isNullable}}{{/required}}mapValues(value['{{name}}'], {{#items}}{{datatype}}{{/items}}ToJSON)),
'{{baseName}}': {{^required}}value['{{name}}'] == null ? undefined : {{/required}}({{#required}}{{#isNullable}}value['{{name}}'] == null ? null : {{/isNullable}}{{/required}}mapValues(value['{{name}}'], {{#items}}{{datatype}}{{/items}}ToJSON)),
{{/isMap}}
{{^isArray}}
{{^isMap}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function {{classname}}FromJSON(json: any): {{classname}} {
}

export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boolean): {{classname}} {
if (json === undefined || json === null) {
if (json == null) {
return json;
}
{{#discriminator}}
Expand All @@ -36,11 +36,8 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole
}

export function {{classname}}ToJSON(value?: {{classname}} | null): any {
if (value === undefined) {
return undefined;
}
if (value === null) {
return null;
if (value == null) {
return value;
}
{{#discriminator}}
switch (value['{{discriminator.propertyName}}']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,6 @@ export interface RequestOpts {
body?: HTTPBody;
}

{{^withoutRuntimeChecks}}
export function exists(json: any, key: string): boolean {
const value = json[key];
return value !== undefined && value !== null;
}
{{/withoutRuntimeChecks}}

export function querystring(params: HTTPQuery, prefix: string = ''): string {
return Object.keys(params)
.map(key => querystringSingleKey(key, params[key], prefix))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class DefaultApi extends runtime.BaseAPI {
/**
*/
async listRaw(requestParameters: ListRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<Club>> {
if (!runtime.exists(requestParameters, 'personId')) {
if (requestParameters['personId'] == null) {
throw new runtime.RequiredError(
'personId',
'Required parameter "personId" was null or undefined when calling list().'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Do not edit the class manually.
*/

import { exists, mapValues } from '../runtime';
import { mapValues } from '../runtime';
import type { Owner } from './Owner';
import {
OwnerFromJSON,
Expand Down Expand Up @@ -46,21 +46,18 @@ export function ClubFromJSON(json: any): Club {
}

export function ClubFromJSONTyped(json: any, ignoreDiscriminator: boolean): Club {
if (json === undefined || json === null) {
if (json == null) {
return json;
}
return {

'owner': !exists(json, 'owner') ? undefined : OwnerFromJSON(json['owner']),
'owner': json['owner'] == null ? undefined : OwnerFromJSON(json['owner']),
};
}

export function ClubToJSON(value?: Club | null): any {
if (value === undefined) {
return undefined;
}
if (value === null) {
return null;
if (value == null) {
return value;
}
return {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Do not edit the class manually.
*/

import { exists, mapValues } from '../runtime';
import { mapValues } from '../runtime';
/**
*
* @export
Expand All @@ -39,21 +39,18 @@ export function OwnerFromJSON(json: any): Owner {
}

export function OwnerFromJSONTyped(json: any, ignoreDiscriminator: boolean): Owner {
if (json === undefined || json === null) {
if (json == null) {
return json;
}
return {

'name': !exists(json, 'name') ? undefined : json['name'],
'name': json['name'] == null ? undefined : json['name'],
};
}

export function OwnerToJSON(value?: Owner | null): any {
if (value === undefined) {
return undefined;
}
if (value === null) {
return null;
if (value == null) {
return value;
}
return {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,6 @@ export interface RequestOpts {
body?: HTTPBody;
}

export function exists(json: any, key: string): boolean {
const value = json[key];
return value !== undefined && value !== null;
}

export function querystring(params: HTTPQuery, prefix: string = ''): string {
return Object.keys(params)
.map(key => querystringSingleKey(key, params[key], prefix))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class DefaultApi extends runtime.BaseAPI {
/**
*/
async listRaw(requestParameters: ListRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<Club>> {
if (!runtime.exists(requestParameters, 'personId')) {
if (requestParameters['personId'] == null) {
throw new runtime.RequiredError(
'personId',
'Required parameter "personId" was null or undefined when calling list().'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Do not edit the class manually.
*/

import { exists, mapValues } from '../runtime';
import { mapValues } from '../runtime';
import type { Owner } from './Owner';
import {
OwnerFromJSON,
Expand Down Expand Up @@ -46,21 +46,18 @@ export function ClubFromJSON(json: any): Club {
}

export function ClubFromJSONTyped(json: any, ignoreDiscriminator: boolean): Club {
if (json === undefined || json === null) {
if (json == null) {
return json;
}
return {

'owner': !exists(json, 'owner') ? undefined : OwnerFromJSON(json['owner']),
'owner': json['owner'] == null ? undefined : OwnerFromJSON(json['owner']),
};
}

export function ClubToJSON(value?: Club | null): any {
if (value === undefined) {
return undefined;
}
if (value === null) {
return null;
if (value == null) {
return value;
}
return {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Do not edit the class manually.
*/

import { exists, mapValues } from '../runtime';
import { mapValues } from '../runtime';
/**
*
* @export
Expand All @@ -39,21 +39,18 @@ export function OwnerFromJSON(json: any): Owner {
}

export function OwnerFromJSONTyped(json: any, ignoreDiscriminator: boolean): Owner {
if (json === undefined || json === null) {
if (json == null) {
return json;
}
return {

'name': !exists(json, 'name') ? undefined : json['name'],
'name': json['name'] == null ? undefined : json['name'],
};
}

export function OwnerToJSON(value?: Owner | null): any {
if (value === undefined) {
return undefined;
}
if (value === null) {
return null;
if (value == null) {
return value;
}
return {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,6 @@ export interface RequestOpts {
body?: HTTPBody;
}

export function exists(json: any, key: string): boolean {
const value = json[key];
return value !== undefined && value !== null;
}

export function querystring(params: HTTPQuery, prefix: string = ''): string {
return Object.keys(params)
.map(key => querystringSingleKey(key, params[key], prefix))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class AnotherFakeApi extends runtime.BaseAPI {
* To test special tags
*/
async _123testSpecialTagsRaw(requestParameters: 123testSpecialTagsRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<Client>> {
if (!runtime.exists(requestParameters, 'client')) {
if (requestParameters['client'] == null) {
throw new runtime.RequiredError(
'client',
'Required parameter "client" was null or undefined when calling _123testSpecialTags().'
Expand Down
Loading

0 comments on commit 5d959ee

Please sign in to comment.