Skip to content

Commit

Permalink
fix(generators): sanitize schema name
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax committed Sep 23, 2022
1 parent 6b3f770 commit 969a5aa
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 13 deletions.
9 changes: 8 additions & 1 deletion src/core/generators/componentDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ContextSpecs } from '../../types';
import { GeneratorSchema } from '../../types/generator';
import { pascal } from '../../utils/case';
import { jsDoc } from '../../utils/doc';
import { sanitize } from '../../utils/string';
import { getResReqTypes } from '../getters/resReqTypes';

export const generateComponentDefinition = (
Expand Down Expand Up @@ -42,7 +43,13 @@ export const generateComponentDefinition = (

const type = allResponseTypes.map(({ value }) => value).join(' | ');

const modelName = `${pascal(name)}${suffix}`;
const modelName = sanitize(`${pascal(name)}${suffix}`, {
underscore: '_',
whitespace: '_',
dash: true,
es5keyword: true,
es5IdentifierName: true,
});
const doc = jsDoc(response as ResponseObject | RequestBodyObject);
const model = `${doc}export type ${modelName} = ${type || 'unknown'};\n`;

Expand Down
9 changes: 8 additions & 1 deletion src/core/generators/parameterDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ContextSpecs } from '../../types';
import { GeneratorSchema } from '../../types/generator';
import { pascal } from '../../utils/case';
import { jsDoc } from '../../utils/doc';
import { sanitize } from '../../utils/string';
import { resolveObject } from '../resolvers/object';
import { resolveRef } from '../resolvers/ref';

Expand All @@ -13,7 +14,13 @@ export const generateParameterDefinition = (
): GeneratorSchema[] => {
return Object.entries(parameters).reduce(
(acc, [parameterName, parameter]) => {
const modelName = `${pascal(parameterName)}${suffix}`;
const modelName = sanitize(`${pascal(parameterName)}${suffix}`, {
underscore: '_',
whitespace: '_',
dash: true,
es5keyword: true,
es5IdentifierName: true,
});
const { schema, imports } = resolveRef<ParameterObject>(
parameter,
context,
Expand Down
9 changes: 8 additions & 1 deletion src/core/generators/schemaDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { pascal } from '../../utils/case';
import { jsDoc } from '../../utils/doc';
import { isReference } from '../../utils/is';
import { getSpecName } from '../../utils/path';
import { sanitize } from '../../utils/string';
import { resolveDiscriminators } from '../getters/discriminators';
import { getEnum } from '../getters/enum';
import { resolveValue } from '../resolvers/value';
Expand All @@ -29,7 +30,13 @@ export const generateSchemasDefinition = (

const models = Object.entries(transformedSchemas).reduce(
(acc, [name, schema]) => {
const schemaName = pascal(name) + suffix;
const schemaName = sanitize(`${pascal(name)}${suffix}`, {
underscore: '_',
whitespace: '_',
dash: true,
es5keyword: true,
es5IdentifierName: true,
});
if (
(!schema.type || schema.type === 'object') &&
!schema.allOf &&
Expand Down
4 changes: 3 additions & 1 deletion src/core/getters/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export const getArray = ({
context,
});
return {
value: `(${resolvedObject.value})[]`,
value: resolvedObject.value.includes('|')
? `(${resolvedObject.value})[]`
: `${resolvedObject.value}[]`,
imports: resolvedObject.imports,
schemas: resolvedObject.schemas,
isEnum: false,
Expand Down
13 changes: 11 additions & 2 deletions src/core/getters/queryParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ContextSpecs } from '../../types';
import { GeneratorImport, GeneratorSchema } from '../../types/generator';
import { GetterParameters, GetterQueryParam } from '../../types/getters';
import { pascal } from '../../utils/case';
import { sanitize } from '../../utils/string';
import { resolveValue } from '../resolvers/value';
import { getEnum } from './enum';
import { getKey } from './keys';
Expand All @@ -26,10 +27,18 @@ const getQueryParamsTypes = (
content: ContentObject;
};

const queryName = sanitize(`${pascal(operationName)}${pascal(name)}`, {
underscore: '_',
whitespace: '_',
dash: true,
es5keyword: true,
es5IdentifierName: true,
});

const { value, imports, isEnum, type, schemas, isRef } = resolveValue({
schema: (schema || content['application/json'].schema)!,
context,
name: pascal(operationName) + pascal(name),
name: queryName,
});

const key = getKey(name);
Expand All @@ -45,7 +54,7 @@ const getQueryParamsTypes = (
}

if (isEnum && !isRef) {
const enumName = pascal(operationName) + pascal(name);
const enumName = queryName;
const enumValue = getEnum(value, type, enumName);

return {
Expand Down
4 changes: 2 additions & 2 deletions src/core/resolvers/object.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SchemaObject } from 'openapi3-ts';
import { ReferenceObject, SchemaObject } from 'openapi3-ts';
import { ContextSpecs } from '../../types';
import { ResolverValue } from '../../types/resolvers';
import { jsDoc } from '../../utils/doc';
Expand All @@ -11,7 +11,7 @@ export const resolveObject = ({
combined = false,
context,
}: {
schema: SchemaObject;
schema: SchemaObject | ReferenceObject;
propName?: string;
combined?: boolean;
context: ContextSpecs;
Expand Down
12 changes: 9 additions & 3 deletions src/core/resolvers/ref.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import get from 'lodash.get';
import {
ParameterObject,
ReferenceObject,
RequestBodyObject,
ResponseObject,
SchemaObject,
Expand All @@ -14,7 +15,8 @@ type ComponentObject =
| SchemaObject
| ResponseObject
| ParameterObject
| RequestBodyObject;
| RequestBodyObject
| ReferenceObject;
export const resolveRef = <Schema extends ComponentObject = ComponentObject>(
schema: ComponentObject,
context: ContextSpecs,
Expand All @@ -24,8 +26,12 @@ export const resolveRef = <Schema extends ComponentObject = ComponentObject>(
imports: GeneratorImport[];
} => {
// the schema is refering to another object
if (schema?.schema?.$ref) {
const resolvedRef = resolveRef<Schema>(schema?.schema, context, imports);
if ((schema as any)?.schema?.$ref) {
const resolvedRef = resolveRef<Schema>(
(schema as any)?.schema,
context,
imports,
);
return {
schema: {
...schema,
Expand Down
4 changes: 2 additions & 2 deletions src/core/resolvers/value.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SchemaObject } from 'openapi3-ts';
import { ReferenceObject, SchemaObject } from 'openapi3-ts';
import { ContextSpecs } from '../../types';
import { ResolverValue } from '../../types/resolvers';
import { isReference } from '../../utils/is';
Expand All @@ -10,7 +10,7 @@ export const resolveValue = ({
name,
context,
}: {
schema: SchemaObject;
schema: SchemaObject | ReferenceObject;
name?: string;
context: ContextSpecs;
}): ResolverValue => {
Expand Down
12 changes: 12 additions & 0 deletions src/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const sanitize = (
dot?: string | true;
dash?: string | true;
es5keyword?: boolean;
es5IdentifierName?: boolean;
special?: boolean;
},
) => {
Expand All @@ -63,6 +64,7 @@ export const sanitize = (
dot = '',
dash = '',
es5keyword = false,
es5IdentifierName = false,
special = false,
} = options ?? {};
let newValue = value;
Expand Down Expand Up @@ -94,6 +96,16 @@ export const sanitize = (
newValue = keyword.isKeywordES5(newValue, true) ? `_${newValue}` : newValue;
}

if (es5IdentifierName) {
if (newValue.match(/^[0-9]/)) {
newValue = `N${newValue}`;
} else {
newValue = keyword.isIdentifierNameES5(newValue)
? newValue
: `_${newValue}`;
}
}

return newValue;
};

Expand Down

0 comments on commit 969a5aa

Please sign in to comment.