Skip to content

Commit

Permalink
fix: AsyncAPIInputProcessor should handle multiple references to the …
Browse files Browse the repository at this point in the history
…same schema (#548)
  • Loading branch information
kamko committed Jan 3, 2022
1 parent 3d1208a commit 0697efb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/processors/AsyncAPIInputProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
if (alreadyIteratedSchemas.has(schemaUid)) {
return alreadyIteratedSchemas.get(schemaUid) as AsyncapiV2Schema;
}
let convertedSchema = new AsyncapiV2Schema();
alreadyIteratedSchemas.set(schemaUid, convertedSchema);
convertedSchema = Object.assign({}, schema.json());

const convertedSchema = Object.assign(new AsyncapiV2Schema(), schema.json());
convertedSchema[this.MODELGEN_INFFERED_NAME] = schemaUid;
alreadyIteratedSchemas.set(schemaUid, convertedSchema);

if (schema.allOf() !== null) {
convertedSchema.allOf = schema.allOf().map((item) => this.convertToInternalSchema(item, alreadyIteratedSchemas));
Expand Down
9 changes: 9 additions & 0 deletions test/processors/AsyncAPIInputProcessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,14 @@ describe('AsyncAPIInputProcessor', () => {
expect(expected.anyOf[1]['x-modelgen-inferred-name']).toEqual('<anonymous-schema-17>');
expect(expected.anyOf[1].properties.prop['x-modelgen-inferred-name']).toEqual('<anonymous-schema-18>');
});
test('should correctly convert when schema has more than one properties referencing one other schema', async () => {
const basicDocString = fs.readFileSync(path.resolve(__dirname, './AsyncAPIInputProcessor/schema_with_2_properties_referencing_one_schema.json'), 'utf8');
const doc = await parse(basicDocString, {} as ParserOptions);
const schema = doc.channels()['/user/signedup'].subscribe().message().payload();
const result = AsyncAPIInputProcessor.convertToInternalSchema(schema) as any;

// both are referencing PersonName
expect(result.properties['firstName']).toEqual(result.properties['lastName']);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"asyncapi": "2.2.0",
"info": {
"title": "Account Service",
"version": "1.0.0",
"description": "This service is in charge of processing user signups"
},
"channels": {
"/user/signedup": {
"subscribe": {
"message": {
"$ref": "#/components/messages/UserSignedUp"
}
}
}
},
"components": {
"messages": {
"UserSignedUp": {
"payload": {
"type": "object",
"properties": {
"firstName": {
"$ref": "#/components/schemas/PersonName"
},
"lastName": {
"$ref": "#/components/schemas/PersonName"
},
"email": {
"type": "string",
"format": "email",
"description": "Email of the user"
}
}
}
}
},
"schemas": {
"PersonName": {
"type": "object",
"properties": {
"value": {
"type": "string"
}
}
}
}
}
}

0 comments on commit 0697efb

Please sign in to comment.