Skip to content

Commit

Permalink
chore(parser): move getting default value function to common parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanshatford committed Apr 1, 2024
1 parent ad7a84d commit b332182
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import type { Model } from '../../common/interfaces/client';
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
import type { OpenApiParameter } from '../../v2/interfaces/OpenApiParameter';
import type { OpenApiSchema } from '../../v3/interfaces/OpenApiSchema';
import type { OperationParameter } from '../interfaces/client';

export const getModelDefault = (definition: OpenApiSchema, model?: Model): string | undefined => {
export const getDefault = (
definition: OpenApiSchema | OpenApiParameter,
model?: Model | OperationParameter
): string | undefined => {
if (definition.default === undefined) {
return undefined;
}
Expand All @@ -16,8 +21,8 @@ export const getModelDefault = (definition: OpenApiSchema, model?: Model): strin
case 'int':
case 'integer':
case 'number':
if (model?.export === 'enum' && model.enum?.[definition.default]) {
const { value } = model.enum[definition.default];
if (model?.export === 'enum' && model.enum?.[definition.default as number]) {
const { value } = model.enum[definition.default as number];
return typeof value === 'string' ? `'${value}'` : String(value);
}
return String(definition.default);
Expand All @@ -32,6 +37,5 @@ export const getModelDefault = (definition: OpenApiSchema, model?: Model): strin
// Ignore
}
}

return undefined;
};
2 changes: 1 addition & 1 deletion src/openApi/v2/interfaces/OpenApiParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface OpenApiParameter extends OpenApiReference, WithEnumExtension, W
allowEmptyValue?: boolean;
items?: OpenApiItems;
collectionFormat?: 'csv' | 'ssv' | 'tsv' | 'pipes' | 'multi';
default?: number;
default?: unknown;
maximum?: number;
exclusiveMaximum?: boolean;
minimum?: number;
Expand Down
16 changes: 8 additions & 8 deletions src/openApi/v2/parser/getOperationParameter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { OperationParameter } from '../../common/interfaces/client';
import { getDefault } from '../../common/parser/getDefault';
import { getEnums } from '../../common/parser/getEnums';
import { getPattern } from '../../common/parser/getPattern';
import { getRef } from '../../common/parser/getRef';
Expand All @@ -8,7 +9,6 @@ import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiParameter } from '../interfaces/OpenApiParameter';
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
import { getModel } from './getModel';
import { getOperationParameterDefault } from './getOperationParameterDefault';

export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParameter): OperationParameter => {
const operationParameter: OperationParameter = {
Expand Down Expand Up @@ -52,7 +52,7 @@ export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParame
operationParameter.base = definitionRef.base;
operationParameter.template = definitionRef.template;
operationParameter.imports.push(...definitionRef.imports);
operationParameter.default = getOperationParameterDefault(parameter, operationParameter);
operationParameter.default = getDefault(parameter, operationParameter);

Check warning on line 55 in src/openApi/v2/parser/getOperationParameter.ts

View check run for this annotation

Codecov / codecov/patch

src/openApi/v2/parser/getOperationParameter.ts#L55

Added line #L55 was not covered by tests
return operationParameter;
}

Expand All @@ -63,7 +63,7 @@ export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParame
operationParameter.enum.push(...enums);
operationParameter.export = 'enum';
operationParameter.type = 'string';
operationParameter.default = getOperationParameterDefault(parameter, operationParameter);
operationParameter.default = getDefault(parameter, operationParameter);

Check warning on line 66 in src/openApi/v2/parser/getOperationParameter.ts

View check run for this annotation

Codecov / codecov/patch

src/openApi/v2/parser/getOperationParameter.ts#L66

Added line #L66 was not covered by tests
return operationParameter;
}
}
Expand All @@ -75,7 +75,7 @@ export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParame
operationParameter.base = items.base;
operationParameter.template = items.template;
operationParameter.imports.push(...items.imports);
operationParameter.default = getOperationParameterDefault(parameter, operationParameter);
operationParameter.default = getDefault(parameter, operationParameter);
return operationParameter;
}

Expand All @@ -86,7 +86,7 @@ export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParame
operationParameter.base = items.base;
operationParameter.template = items.template;
operationParameter.imports.push(...items.imports);
operationParameter.default = getOperationParameterDefault(parameter, operationParameter);
operationParameter.default = getDefault(parameter, operationParameter);
return operationParameter;
}

Expand All @@ -102,7 +102,7 @@ export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParame
operationParameter.base = model.base;
operationParameter.template = model.template;
operationParameter.imports.push(...model.imports);
operationParameter.default = getOperationParameterDefault(parameter, operationParameter);
operationParameter.default = getDefault(parameter, operationParameter);
return operationParameter;
} else {
const model = getModel(openApi, schema);
Expand All @@ -115,7 +115,7 @@ export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParame
operationParameter.enum.push(...model.enum);
operationParameter.enums.push(...model.enums);
operationParameter.properties.push(...model.properties);
operationParameter.default = getOperationParameterDefault(parameter, operationParameter);
operationParameter.default = getDefault(parameter, operationParameter);
return operationParameter;
}
}
Expand All @@ -128,7 +128,7 @@ export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParame
operationParameter.base = definitionType.base;
operationParameter.template = definitionType.template;
operationParameter.imports.push(...definitionType.imports);
operationParameter.default = getOperationParameterDefault(parameter, operationParameter);
operationParameter.default = getDefault(parameter, operationParameter);
return operationParameter;
}

Expand Down
43 changes: 0 additions & 43 deletions src/openApi/v2/parser/getOperationParameterDefault.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/openApi/v3/interfaces/OpenApiSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface OpenApiSchema extends OpenApiReference, WithEnumExtension {
allOf?: OpenApiSchema[];
anyOf?: OpenApiSchema[];
const?: string | number | boolean | null;
default?: number;
default?: unknown;
deprecated?: boolean;
description?: string;
discriminator?: OpenApiDiscriminator;
Expand Down Expand Up @@ -52,7 +52,7 @@ export interface OpenApiSchema extends OpenApiReference, WithEnumExtension {
readOnly?: boolean;
required?: string[];
title?: string;
type?: string | string[];
type?: string;
uniqueItems?: boolean;
writeOnly?: boolean;
xml?: OpenApiXml;
Expand Down
14 changes: 7 additions & 7 deletions src/openApi/v3/parser/getModel.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Model } from '../../common/interfaces/client';
import { getDefault } from '../../common/parser/getDefault';
import { getEnums } from '../../common/parser/getEnums';
import { getPattern } from '../../common/parser/getPattern';
import { getType } from '../../common/parser/type';
import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
import { findModelComposition, getModelComposition } from './getModelComposition';
import { getModelDefault } from './getModelDefault';
import { getAdditionalPropertiesModel, getModelProperties } from './getModelProperties';
import { inferType } from './inferType';

Expand Down Expand Up @@ -59,7 +59,7 @@ export const getModel = (
model.imports = [...model.imports, ...definitionRef.imports];
model.template = definitionRef.template;
model.type = definitionRef.type;
model.default = getModelDefault(definition, model);
model.default = getDefault(definition, model);
return model;
}

Expand All @@ -70,7 +70,7 @@ export const getModel = (
model.enum = [...model.enum, ...enums];
model.export = 'enum';
model.type = 'string';
model.default = getModelDefault(definition, model);
model.default = getDefault(definition, model);
return model;
}
}
Expand All @@ -84,7 +84,7 @@ export const getModel = (
model.imports = [...model.imports, ...arrayItems.imports];
model.template = arrayItems.template;
model.type = arrayItems.type;
model.default = getModelDefault(definition, model);
model.default = getDefault(definition, model);
return model;
}

Expand Down Expand Up @@ -112,7 +112,7 @@ export const getModel = (
model.link = arrayItems;
model.template = arrayItems.template;
model.type = arrayItems.type;
model.default = getModelDefault(definition, model);
model.default = getDefault(definition, model);
return model;
}

Expand All @@ -133,7 +133,7 @@ export const getModel = (
model.base = 'any';
model.export = 'interface';
model.type = 'any';
model.default = getModelDefault(definition, model);
model.default = getDefault(definition, model);

const modelProperties = getModelProperties(openApi, definition, getModel, model);
modelProperties.forEach(modelProperty => {
Expand Down Expand Up @@ -176,7 +176,7 @@ export const getModel = (
model.isNullable = definitionType.isNullable || model.isNullable;
model.template = definitionType.template;
model.type = definitionType.type;
model.default = getModelDefault(definition, model);
model.default = getDefault(definition, model);
return model;
}

Expand Down
8 changes: 4 additions & 4 deletions src/openApi/v3/parser/getModelProperties.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { escapeName } from '../../../utils/escapeName';
import type { Model } from '../../common/interfaces/client';
import { getDefault } from '../../common/parser/getDefault';
import { getPattern } from '../../common/parser/getPattern';
import { getType } from '../../common/parser/type';
import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
import { findOneOfParentDiscriminator, mapPropertyValue } from './discriminator';
import type { getModel } from './getModel';
import { getModelDefault } from './getModelDefault';

// Fix for circular dependency
export type GetModelFn = typeof getModel;
Expand All @@ -21,7 +21,7 @@ export const getAdditionalPropertiesModel = (
const apModel = getModel(openApi, ap);

if (definition.additionalProperties === true && definition.properties) {
apModel.default = getModelDefault(definition, model);
apModel.default = getDefault(definition, model);
apModel.export = 'generic';
apModel.isRequired = true;
apModel.name = '[key: string]';
Expand All @@ -31,7 +31,7 @@ export const getAdditionalPropertiesModel = (
if (ap.$ref) {
const apType = getType(ap.$ref);
model.base = apType.base;
model.default = getModelDefault(definition, model);
model.default = getDefault(definition, model);
model.export = 'dictionary';
model.imports.push(...apType.imports);
model.template = apType.template;
Expand All @@ -40,7 +40,7 @@ export const getAdditionalPropertiesModel = (
}

model.base = apModel.base;
model.default = getModelDefault(definition, model);
model.default = getDefault(definition, model);
model.export = 'dictionary';
model.imports.push(...apModel.imports);
model.link = apModel;
Expand Down
4 changes: 2 additions & 2 deletions src/openApi/v3/parser/getOperationParameter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { OperationParameter } from '../../common/interfaces/client';
import { getDefault } from '../../common/parser/getDefault';
import { getPattern } from '../../common/parser/getPattern';
import { getRef } from '../../common/parser/getRef';
import { getOperationParameterName } from '../../common/parser/operation';
Expand All @@ -7,7 +8,6 @@ import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiParameter } from '../interfaces/OpenApiParameter';
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
import { getModel } from './getModel';
import { getModelDefault } from './getModelDefault';

export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParameter): OperationParameter => {
const operationParameter: OperationParameter = {
Expand Down Expand Up @@ -57,7 +57,7 @@ export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParame
operationParameter.template = model.template;
operationParameter.$refs = [...operationParameter.$refs, ...model.$refs];
operationParameter.imports = [...operationParameter.imports, ...model.imports];
operationParameter.default = getModelDefault(schema);
operationParameter.default = getDefault(schema);
return operationParameter;
} else {
const model = getModel(openApi, schema);
Expand Down

0 comments on commit b332182

Please sign in to comment.