diff --git a/src/processors/AsyncAPIInputProcessor.ts b/src/processors/AsyncAPIInputProcessor.ts index f00569da3a..1b6409ac7d 100644 --- a/src/processors/AsyncAPIInputProcessor.ts +++ b/src/processors/AsyncAPIInputProcessor.ts @@ -95,16 +95,16 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor { } } - if (schema.properties !== null && Object.keys(schema.properties).length) { + if (schema.properties() !== null && Object.keys(schema.properties()).length) { const properties : {[key: string]: Schema | boolean} = {}; - Object.entries(schema.properties).forEach(([propertyName, propertySchema]) => { + Object.entries(schema.properties()).forEach(([propertyName, propertySchema]) => { properties[`${propertyName}`] = this.reflectSchemaNames(propertySchema); }); convertedSchema.properties = properties; } - if (schema.dependencies !== null && Object.keys(schema.dependencies).length) { + if (schema.dependencies() !== null && Object.keys(schema.dependencies()).length) { const dependencies: { [key: string]: Schema | boolean | string[] } = {}; - Object.entries(schema.dependencies).forEach(([dependencyName, dependency]) => { + Object.entries(schema.dependencies()).forEach(([dependencyName, dependency]) => { if (typeof dependency === 'object' && !Array.isArray(dependency)) { dependencies[`${dependencyName}`] = this.reflectSchemaNames(dependency); } else { @@ -113,16 +113,16 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor { }); convertedSchema.dependencies = dependencies; } - if (schema.patternProperties !== null && Object.keys(schema.patternProperties).length) { + if (schema.patternProperties() !== null && Object.keys(schema.patternProperties()).length) { const patternProperties: { [key: string]: Schema | boolean } = {}; - Object.entries(schema.patternProperties).forEach(([patternPropertyName, patternProperty]) => { + Object.entries(schema.patternProperties()).forEach(([patternPropertyName, patternProperty]) => { patternProperties[`${patternPropertyName}`] = this.reflectSchemaNames(patternProperty); }); convertedSchema.patternProperties = patternProperties; } - if (schema.definitions !== null && Object.keys(schema.definitions).length) { + if (schema.definitions() !== null && Object.keys(schema.definitions()).length) { const definitions: { [key: string]: Schema | boolean } = {}; - Object.entries(schema.definitions).forEach(([definitionName, definition]) => { + Object.entries(schema.definitions()).forEach(([definitionName, definition]) => { definitions[`${definitionName}`] = this.reflectSchemaNames(definition); }); convertedSchema.definitions = definitions; diff --git a/test/processors/AsyncAPIInputProcessor.spec.ts b/test/processors/AsyncAPIInputProcessor.spec.ts index 46ce7647cb..61963faaac 100644 --- a/test/processors/AsyncAPIInputProcessor.spec.ts +++ b/test/processors/AsyncAPIInputProcessor.spec.ts @@ -50,4 +50,43 @@ describe('AsyncAPIInputProcessor', function() { expect(commonInputModel).toEqual(expectedCommonInputModel); }); }); + + describe('reflectSchemaName()', function() { + test('should work', async function() { + const basicDocString = fs.readFileSync(path.resolve(__dirname, './AsyncAPIInputProcessor/schema_name_reflection.json'), 'utf8'); + const doc = await parse(basicDocString); + const schema = doc.channels()["/user/signedup"].subscribe().message().payload(); + const expected = AsyncAPIInputProcessor.reflectSchemaNames(schema) as any; + + // root + expect(expected['x-modelgen-inferred-name']).toEqual(''); + + // properties + expect(expected.properties.prop['x-modelgen-inferred-name']).toEqual(''); + expect(expected.properties.allOfCase['x-modelgen-inferred-name']).toEqual(''); + expect(expected.properties.allOfCase.allOf[0]['x-modelgen-inferred-name']).toEqual(''); + expect(expected.properties.allOfCase.allOf[1]['x-modelgen-inferred-name']).toEqual(''); + expect(expected.properties.object['x-modelgen-inferred-name']).toEqual(''); + expect(expected.properties.object.properties.prop['x-modelgen-inferred-name']).toEqual(''); + expect(expected.properties.propWithObject['x-modelgen-inferred-name']).toEqual(''); + expect(expected.properties.propWithObject.properties.propWithObject['x-modelgen-inferred-name']).toEqual(''); + + // patternProperties + expect(expected.patternProperties.patternProp['x-modelgen-inferred-name']).toEqual(''); + + // dependencies + expect(expected.dependencies.dep['x-modelgen-inferred-name']).toEqual(''); + + // definitions + expect(expected.definitions.def['x-modelgen-inferred-name']).toEqual(''); + expect(expected.definitions.oneOfCase['x-modelgen-inferred-name']).toEqual(''); + expect(expected.definitions.oneOfCase.oneOf[0]['x-modelgen-inferred-name']).toEqual(''); + expect(expected.definitions.oneOfCase.oneOf[1]['x-modelgen-inferred-name']).toEqual(''); + + // anyOf + expect(expected.anyOf[0]['x-modelgen-inferred-name']).toEqual(''); + expect(expected.anyOf[1]['x-modelgen-inferred-name']).toEqual(''); + expect(expected.anyOf[1].properties.prop['x-modelgen-inferred-name']).toEqual(''); + }); + }); }); \ No newline at end of file diff --git a/test/processors/AsyncAPIInputProcessor/commonInputModel/basic.json b/test/processors/AsyncAPIInputProcessor/commonInputModel/basic.json index bde6f23f22..baef7fddb5 100644 --- a/test/processors/AsyncAPIInputProcessor/commonInputModel/basic.json +++ b/test/processors/AsyncAPIInputProcessor/commonInputModel/basic.json @@ -7,7 +7,8 @@ "email":{ "type":"string", "format":"email", - "x-parser-schema-id":"" + "x-parser-schema-id":"", + "x-modelgen-inferred-name": "" } }, "x-parser-schema-id":"", @@ -20,7 +21,8 @@ "originalSchema":{ "type":"string", "format":"email", - "x-parser-schema-id":"" + "x-parser-schema-id":"", + "x-modelgen-inferred-name": "" }, "type":"string" } diff --git a/test/processors/AsyncAPIInputProcessor/schema_name_reflection.json b/test/processors/AsyncAPIInputProcessor/schema_name_reflection.json new file mode 100644 index 0000000000..290fa71d94 --- /dev/null +++ b/test/processors/AsyncAPIInputProcessor/schema_name_reflection.json @@ -0,0 +1,119 @@ +{ + "asyncapi": "2.0.0", + "defaultContentType": "application/json", + "externalDocs": { + "description": "Find more info here", + "url": "https://example.com" + }, + "tags": [ + { + "name": "user", + "description": "user signed up" + }, + { + "name": "signup" + } + ], + "info": { + "title": "Signup service example (internal)", + "version": "0.1.0" + }, + "channels": { + "/user/signedup": { + "subscribe": { + "message": { + "payload": { + "type": "object", + "x-parser-schema-id": "", + "properties": { + "prop": { + "x-parser-schema-id": "", + "type": "string" + }, + "allOfCase": { + "x-parser-schema-id": "", + "allOf": [ + { + "x-parser-schema-id": "", + "type": "string" + }, + { + "x-parser-schema-id": "", + "type": "string" + } + ] + }, + "object": { + "x-parser-schema-id": "", + "type": "object", + "properties": { + "prop": { + "x-parser-schema-id": "", + "type": "string" + } + } + }, + "propWithObject": { + "type": "object", + "x-parser-schema-id": "", + "properties": { + "propWithObject": { + "x-parser-schema-id": "", + "type": "object" + } + } + } + }, + "patternProperties": { + "patternProp": { + "x-parser-schema-id": "", + "type": "string" + } + }, + "dependencies": { + "dep": { + "x-parser-schema-id": "", + "type": "string" + } + }, + "definitions": { + "def": { + "x-parser-schema-id": "", + "type": "string" + }, + "oneOfCase": { + "x-parser-schema-id": "", + "oneOf": [ + { + "x-parser-schema-id": "", + "type": "string" + }, + { + "x-parser-schema-id": "", + "type": "string" + } + ] + } + }, + "anyOf": [ + { + "x-parser-schema-id": "", + "type": "string" + }, + { + "type": "object", + "x-parser-schema-id": "", + "properties": { + "prop": { + "x-parser-schema-id": "", + "type": "string" + } + } + } + ] + } + } + } + } + } +} \ No newline at end of file diff --git a/test/processors/JsonSchemaInputProcessor.spec.ts b/test/processors/JsonSchemaInputProcessor.spec.ts index 985b61d00a..9c72ad13c4 100644 --- a/test/processors/JsonSchemaInputProcessor.spec.ts +++ b/test/processors/JsonSchemaInputProcessor.spec.ts @@ -226,6 +226,6 @@ describe('JsonSchemaInputProcessor', function() { expect(expected.anyOf[0]['x-modelgen-inferred-name']).toEqual('anyOf_0'); expect(expected.anyOf[1]['x-modelgen-inferred-name']).toEqual('anyOf_1'); expect(expected.anyOf[1].properties.prop['x-modelgen-inferred-name']).toEqual('anyOf_1_prop'); - }) + }); }); });