Skip to content

Commit

Permalink
Merge pull request #672 from WoH/master
Browse files Browse the repository at this point in the history
Schema Naming: Avoid tilde, escape double quotes properly
  • Loading branch information
WoH committed Apr 29, 2020
2 parents cbadc84 + c795004 commit af70226
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 32 deletions.
11 changes: 6 additions & 5 deletions src/metadataGeneration/typeResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,14 +555,15 @@ export class TypeResolver {
.replace(/<|>/g, '_')
.replace(/\s+/g, '')
.replace(/,/g, '.')
.replace(/\'([^']*)\'|\"(^"*)\"/g, '$1')
.replace(/&/g, '~AND~')
.replace(/\|/g, '~OR~')
.replace(/\[\]/g, 'Array')
.replace(/\'([^']*)\'/g, '$1')
.replace(/\"([^"]*)\"/g, '$1')
.replace(/&/g, '-and-')
.replace(/\|/g, '-or-')
.replace(/\[\]/g, '-Array')
.replace(/{|}/g, '_') // SuccessResponse_{indexesCreated-number}_ -> SuccessResponse__indexesCreated-number__
.replace(/([a-z]+):([a-z]+)/gi, '$1-$2') // SuccessResponse_indexesCreated:number_ -> SuccessResponse_indexesCreated-number_
.replace(/;/g, '--')
.replace(/([a-z]+)\[([a-z]+)\]/gi, '$1~$2~'), // Partial_SerializedDatasourceWithVersion[format]_ -> Partial_SerializedDatasourceWithVersion~format~_,
.replace(/([a-z]+)\[([a-z]+)\]/gi, '$1-at-$2'), // Partial_SerializedDatasourceWithVersion[format]_ -> Partial_SerializedDatasourceWithVersion~format~_,
);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/testModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ export interface TestModel extends Model {
partial?: Partial<Account>;
excludeToEnum?: Exclude<EnumUnion, EnumNumberValue>;
excludeToAlias?: Exclude<ThreeOrFour, TypeAliasModel3>;
excludeLiteral?: Exclude<keyof TestClassModel, 'account' | 'defaultValue2' | 'indexedTypeToInterface' | 'indexedTypeToClass' | 'indexedTypeToAlias'>;
// prettier-ignore
excludeLiteral?: Exclude<keyof TestClassModel, 'account' | "defaultValue2" | "indexedTypeToInterface" | 'indexedTypeToClass' | 'indexedTypeToAlias'>;
excludeToInterface?: Exclude<OneOrTwo, TypeAliasModel1>;
excludeTypeToPrimitive?: NonNullable<number | null>;

Expand Down
34 changes: 17 additions & 17 deletions tests/unit/swagger/definitionsGeneration/definitions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,11 @@ describe('Definition generation', () => {
expect(propertySchema.$ref).to.eq('#/definitions/Generic__foo-string--bar-boolean__');
},
indexed: (propertyName, propertySchema) => {
expect(propertySchema.$ref).to.eq('#/definitions/Partial_Indexed~foo~_');
expect(propertySchema.$ref).to.eq('#/definitions/Partial_Indexed-at-foo_');
},
record: (propertyName, propertySchema) => {
expect(propertySchema.$ref).to.eq('#/definitions/Record_record-foo~OR~record-bar._data-string__');
const schema = getValidatedDefinition('Record_record-foo~OR~record-bar._data-string__', currentSpec);
expect(propertySchema.$ref).to.eq('#/definitions/Record_record-foo-or-record-bar._data-string__');
const schema = getValidatedDefinition('Record_record-foo-or-record-bar._data-string__', currentSpec);
expect(schema).to.be.deep.eq({
properties: {
'record-foo': {
Expand Down Expand Up @@ -492,13 +492,13 @@ describe('Definition generation', () => {
expect(propertySchema.$ref).to.eq('#/definitions/GenericRequest_Array_TypeAliasModel1__', `for property ${propertyName}.$ref`);
},
genericNestedArrayCharacter1: (propertyName, propertySchema) => {
expect(propertySchema.$ref).to.eq('#/definitions/GenericRequest_TypeAliasModel1Array_', `for property ${propertyName}.$ref`);
expect(propertySchema.$ref).to.eq('#/definitions/GenericRequest_TypeAliasModel1-Array_', `for property ${propertyName}.$ref`);
},
genericNestedArrayKeyword2: (propertyName, propertySchema) => {
expect(propertySchema.$ref).to.eq('#/definitions/GenericRequest_Array_TypeAliasModel2__', `for property ${propertyName}.$ref`);
},
genericNestedArrayCharacter2: (propertyName, propertySchema) => {
expect(propertySchema.$ref).to.eq('#/definitions/GenericRequest_TypeAliasModel2Array_', `for property ${propertyName}.$ref`);
expect(propertySchema.$ref).to.eq('#/definitions/GenericRequest_TypeAliasModel2-Array_', `for property ${propertyName}.$ref`);
},
defaultGenericModel: (propertyName, propertySchema) => {
expect(propertySchema.$ref).to.eq('#/definitions/GenericModel', `for property ${propertyName}.$ref`);
Expand Down Expand Up @@ -679,13 +679,13 @@ describe('Definition generation', () => {
excludeToEnum: { $ref: '#/definitions/Exclude_EnumUnion.EnumNumberValue_', description: undefined, format: undefined, example: undefined },
excludeToAlias: { $ref: '#/definitions/Exclude_ThreeOrFour.TypeAliasModel3_', description: undefined, format: undefined, example: undefined },
excludeLiteral: {
$ref: '#/definitions/Exclude_keyofTestClassModel.account~OR~defaultValue2~OR~indexedTypeToInterface~OR~indexedTypeToClass~OR~indexedTypeToAlias_',
$ref: '#/definitions/Exclude_keyofTestClassModel.account-or-defaultValue2-or-indexedTypeToInterface-or-indexedTypeToClass-or-indexedTypeToAlias_',
description: undefined,
format: undefined,
example: undefined,
},
excludeToInterface: { $ref: '#/definitions/Exclude_OneOrTwo.TypeAliasModel1_', description: undefined, format: undefined, example: undefined },
excludeTypeToPrimitive: { $ref: '#/definitions/NonNullable_number~OR~null_', description: undefined, format: undefined, example: undefined },
excludeTypeToPrimitive: { $ref: '#/definitions/NonNullable_number-or-null_', description: undefined, format: undefined, example: undefined },
pick: { $ref: '#/definitions/Pick_ThingContainerWithTitle_string_.list_', description: undefined, format: undefined, example: undefined },
readonlyClass: { $ref: '#/definitions/Readonly_TestClassModel_', description: undefined, format: undefined, example: undefined },
defaultArgs: { $ref: '#/definitions/DefaultTestModel', description: undefined, format: undefined, example: undefined },
Expand Down Expand Up @@ -806,7 +806,7 @@ describe('Definition generation', () => {
`for definition linked by ${propertyName}`,
);

const excludeLiteral = getValidatedDefinition('Exclude_keyofTestClassModel.account~OR~defaultValue2~OR~indexedTypeToInterface~OR~indexedTypeToClass~OR~indexedTypeToAlias_', currentSpec);
const excludeLiteral = getValidatedDefinition('Exclude_keyofTestClassModel.account-or-defaultValue2-or-indexedTypeToInterface-or-indexedTypeToClass-or-indexedTypeToAlias_', currentSpec);
expect(excludeLiteral).to.deep.eq(
{
type: 'string',
Expand Down Expand Up @@ -846,7 +846,7 @@ describe('Definition generation', () => {
`for definition linked by ${propertyName}`,
);

const excludeTypeToPrimitive = getValidatedDefinition('NonNullable_number~OR~null_', currentSpec);
const excludeTypeToPrimitive = getValidatedDefinition('NonNullable_number-or-null_', currentSpec);
expect(excludeTypeToPrimitive).to.deep.eq(
{
type: 'number',
Expand Down Expand Up @@ -1290,7 +1290,7 @@ describe('Definition generation', () => {
expect(property.$ref).to.equal('#/definitions/TestModel');
});
it('should generate different definitions for a generic model array', () => {
const definition = getValidatedDefinition('GenericModel_TestModelArray_', currentSpec).properties;
const definition = getValidatedDefinition('GenericModel_TestModel-Array_', currentSpec).properties;

if (!definition) {
throw new Error(`There were no properties on model.`);
Expand Down Expand Up @@ -1326,7 +1326,7 @@ describe('Definition generation', () => {
expect(property.type).to.equal('string');
});
it('should generate different definitions for a generic primitive array', () => {
const definition = getValidatedDefinition('GenericModel_stringArray_', currentSpec).properties;
const definition = getValidatedDefinition('GenericModel_string-Array_', currentSpec).properties;

if (!definition) {
throw new Error(`There were no properties on model.`);
Expand All @@ -1348,13 +1348,13 @@ describe('Definition generation', () => {
expect((property.items as Swagger.Schema).type).to.equal('string');
});
it('should propagate generics', () => {
const definition = getValidatedDefinition('GenericModel_TestModelArray_', currentSpec).properties;
const definition = getValidatedDefinition('GenericModel_TestModel-Array_', currentSpec).properties;

expect(definition!.result).to.deep.equal({ items: { $ref: '#/definitions/TestModel' }, type: 'array', description: undefined, format: undefined, example: undefined, default: undefined });
expect(definition!.union).to.deep.equal({ type: 'object', description: undefined, format: undefined, default: undefined, example: undefined });
expect(definition!.nested).to.deep.equal({ $ref: '#/definitions/GenericRequest_TestModelArray_', description: undefined, format: undefined, example: undefined });
expect(definition!.nested).to.deep.equal({ $ref: '#/definitions/GenericRequest_TestModel-Array_', description: undefined, format: undefined, example: undefined });

const ref = getValidatedDefinition('GenericRequest_TestModelArray_', currentSpec).properties;
const ref = getValidatedDefinition('GenericRequest_TestModel-Array_', currentSpec).properties;
expect(ref!.name).to.deep.equal({ type: 'string', description: undefined, format: undefined, default: undefined, example: undefined });
expect(ref!.value).to.deep.equal({ items: { $ref: '#/definitions/TestModel' }, type: 'array', description: undefined, format: undefined, default: undefined, example: undefined });
});
Expand All @@ -1365,16 +1365,16 @@ describe('Definition generation', () => {
expect(definition!.shouldBeString!.$ref).to.deep.equal('#/definitions/TSameNameDifferentValue');
});
it('should check heritage clauses for type args', () => {
const definition = getValidatedDefinition('GenericModel_TestModelArray_', currentSpec).properties;
const definition = getValidatedDefinition('GenericModel_TestModel-Array_', currentSpec).properties;

expect(definition!.heritageCheck).to.deep.equal({
$ref: '#/definitions/ThingContainerWithTitle_TestModelArray_',
$ref: '#/definitions/ThingContainerWithTitle_TestModel-Array_',
description: undefined,
format: undefined,
example: undefined,
});

const ref = getValidatedDefinition('ThingContainerWithTitle_TestModelArray_', currentSpec).properties;
const ref = getValidatedDefinition('ThingContainerWithTitle_TestModel-Array_', currentSpec).properties;
expect(ref!.title).to.deep.equal({ type: 'string', description: undefined, format: undefined, default: undefined, example: undefined });
expect(ref!.t).to.deep.equal({
default: undefined,
Expand Down
18 changes: 9 additions & 9 deletions tests/unit/swagger/schemaDetails3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,11 +594,11 @@ describe('Definition generation for OpenAPI 3.0.0', () => {
expect(propertySchema.$ref).to.eq('#/components/schemas/Generic__foo-string--bar-boolean__');
},
indexed: (propertyName, propertySchema) => {
expect(propertySchema.$ref).to.eq('#/components/schemas/Partial_Indexed~foo~_');
expect(propertySchema.$ref).to.eq('#/components/schemas/Partial_Indexed-at-foo_');
},
record: (propertyName, propertySchema) => {
expect(propertySchema.$ref).to.eq('#/components/schemas/Record_record-foo~OR~record-bar._data-string__');
const schema = getComponentSchema('Record_record-foo~OR~record-bar._data-string__', currentSpec);
expect(propertySchema.$ref).to.eq('#/components/schemas/Record_record-foo-or-record-bar._data-string__');
const schema = getComponentSchema('Record_record-foo-or-record-bar._data-string__', currentSpec);
expect(schema).to.be.deep.eq({
properties: {
'record-foo': {
Expand Down Expand Up @@ -683,13 +683,13 @@ describe('Definition generation for OpenAPI 3.0.0', () => {
expect(propertySchema.$ref).to.eq('#/components/schemas/GenericRequest_Array_TypeAliasModel1__', `for property ${propertyName}.$ref`);
},
genericNestedArrayCharacter1: (propertyName, propertySchema) => {
expect(propertySchema.$ref).to.eq('#/components/schemas/GenericRequest_TypeAliasModel1Array_', `for property ${propertyName}.$ref`);
expect(propertySchema.$ref).to.eq('#/components/schemas/GenericRequest_TypeAliasModel1-Array_', `for property ${propertyName}.$ref`);
},
genericNestedArrayKeyword2: (propertyName, propertySchema) => {
expect(propertySchema.$ref).to.eq('#/components/schemas/GenericRequest_Array_TypeAliasModel2__', `for property ${propertyName}.$ref`);
},
genericNestedArrayCharacter2: (propertyName, propertySchema) => {
expect(propertySchema.$ref).to.eq('#/components/schemas/GenericRequest_TypeAliasModel2Array_', `for property ${propertyName}.$ref`);
expect(propertySchema.$ref).to.eq('#/components/schemas/GenericRequest_TypeAliasModel2-Array_', `for property ${propertyName}.$ref`);
},
defaultGenericModel: (propertyName, propertySchema) => {
expect(propertySchema.$ref).to.eq('#/components/schemas/GenericModel', `for property ${propertyName}.$ref`);
Expand Down Expand Up @@ -848,13 +848,13 @@ describe('Definition generation for OpenAPI 3.0.0', () => {
excludeToEnum: { $ref: '#/components/schemas/Exclude_EnumUnion.EnumNumberValue_', description: undefined, format: undefined, example: undefined },
excludeToAlias: { $ref: '#/components/schemas/Exclude_ThreeOrFour.TypeAliasModel3_', description: undefined, format: undefined, example: undefined },
excludeLiteral: {
$ref: '#/components/schemas/Exclude_keyofTestClassModel.account~OR~defaultValue2~OR~indexedTypeToInterface~OR~indexedTypeToClass~OR~indexedTypeToAlias_',
$ref: '#/components/schemas/Exclude_keyofTestClassModel.account-or-defaultValue2-or-indexedTypeToInterface-or-indexedTypeToClass-or-indexedTypeToAlias_',
description: undefined,
format: undefined,
example: undefined,
},
excludeToInterface: { $ref: '#/components/schemas/Exclude_OneOrTwo.TypeAliasModel1_', description: undefined, format: undefined, example: undefined },
excludeTypeToPrimitive: { $ref: '#/components/schemas/NonNullable_number~OR~null_', description: undefined, format: undefined, example: undefined },
excludeTypeToPrimitive: { $ref: '#/components/schemas/NonNullable_number-or-null_', description: undefined, format: undefined, example: undefined },
pick: { $ref: '#/components/schemas/Pick_ThingContainerWithTitle_string_.list_', description: undefined, format: undefined, example: undefined },
readonlyClass: { $ref: '#/components/schemas/Readonly_TestClassModel_', description: undefined, format: undefined, example: undefined },
defaultArgs: { $ref: '#/components/schemas/DefaultTestModel', description: undefined, format: undefined, example: undefined },
Expand Down Expand Up @@ -1067,7 +1067,7 @@ describe('Definition generation for OpenAPI 3.0.0', () => {
`for a schema linked by property ${propertyName}`,
);

const excludeLiteral = getComponentSchema('Exclude_keyofTestClassModel.account~OR~defaultValue2~OR~indexedTypeToInterface~OR~indexedTypeToClass~OR~indexedTypeToAlias_', currentSpec);
const excludeLiteral = getComponentSchema('Exclude_keyofTestClassModel.account-or-defaultValue2-or-indexedTypeToInterface-or-indexedTypeToClass-or-indexedTypeToAlias_', currentSpec);
expect(excludeLiteral).to.deep.eq(
{
oneOf: [
Expand Down Expand Up @@ -1105,7 +1105,7 @@ describe('Definition generation for OpenAPI 3.0.0', () => {
`for a schema linked by property ${propertyName}`,
);

const excludeTypeToPrimitive = getComponentSchema('NonNullable_number~OR~null_', currentSpec);
const excludeTypeToPrimitive = getComponentSchema('NonNullable_number-or-null_', currentSpec);
expect(excludeTypeToPrimitive).to.deep.eq(
{
type: 'number',
Expand Down

0 comments on commit af70226

Please sign in to comment.