Skip to content

Commit

Permalink
fix: weird payload generation (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Nov 1, 2024
1 parent 6b87102 commit 7aa7867
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/codegen/generators/helpers/payloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ export async function generateAsyncAPIPayloads<GeneratorType>(
const schema = AsyncAPIInputProcessor.convertToInternalSchema(
message.payload() as any
);
const payloadId = message.id() ?? message.name();
if (typeof schema === 'boolean') {
schemaObj.oneOf.push(schema);
} else {
schemaObj.oneOf.push({
...schema,
$id: message.id()
$id: payloadId
});
}
}
Expand All @@ -50,14 +51,20 @@ export async function generateAsyncAPIPayloads<GeneratorType>(
const schema = AsyncAPIInputProcessor.convertToInternalSchema(
messagePayload as any
);

const message = messages[0];
let payloadId = message.id() ?? message.name();
if (payloadId.includes('AnonymousSchema_')) {
payloadId = pascalCase(
`${findNameFromChannel(channel)}_Payload`
);
}
if (typeof schema === 'boolean') {
schemaObj = schema;
} else {
schemaObj = {
...schemaObj,
...(schema as any),
$id: messages[0].id()
$id: payloadId
};
}
} else {
Expand All @@ -77,6 +84,7 @@ export async function generateAsyncAPIPayloads<GeneratorType>(
}
} else {
const generatedModels = await generator(asyncapiDocument);

otherModels = generatedModels.map((model) => {
return {
messageModel: model,
Expand Down
2 changes: 0 additions & 2 deletions src/codegen/inputs/asyncapi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Parser, fromFile} from '@asyncapi/parser';

import {AvroSchemaParser} from '@asyncapi/avro-schema-parser';
import {OpenAPISchemaParser} from '@asyncapi/openapi-schema-parser';
import {RamlDTSchemaParser} from '@asyncapi/raml-dt-schema-parser';
Expand All @@ -26,6 +25,5 @@ export async function loadAsyncapi(context: RunGeneratorContext) {
`Could not load AsyncAPI document, errors was: ${JSON.stringify(document.diagnostics)}`
);
}

return document.document;
}
31 changes: 31 additions & 0 deletions test/codegen/generators/typescript/payload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,36 @@ describe('payloads', () => {
expect(renderedContent.otherModels.length).toEqual(1);
expect(renderedContent.otherModels[0].messageModel.result).toMatchSnapshot();
});
it('should get correct model names', async () => {
const parsedAsyncAPIDocument = await loadAsyncapi(path.resolve(__dirname, '../../../configs/payload-complex.json'));

const renderedContent = await generateTypescriptPayload({
generator: {
enum: 'enum',
map: 'record',
rawPropertyNames: false,
serializationType: 'json',
useForJavaScript: false,
outputPath: path.resolve(__dirname, './output'),
preset: 'payloads',
language: 'typescript',
dependencies: [],
id: 'test'
},
inputType: 'asyncapi',
asyncapiDocument: parsedAsyncAPIDocument,
dependencyOutputs: { }
});
const payloadNames = renderedContent.otherModels.map((value) => value.messageModel.modelName);
expect(payloadNames.includes('ComplexMessage')).toEqual(true);
expect(payloadNames.includes('OpenApiGenerator')).toEqual(true);
expect(payloadNames.includes('AsyncApiGenerator')).toEqual(true);
expect(payloadNames.includes('DocumentContext')).toEqual(true);
expect(payloadNames.includes('Simple')).toEqual(true);
expect(payloadNames.includes('NpmRelease')).toEqual(true);
expect(payloadNames.includes('GeneratorTypes')).toEqual(true);
expect(payloadNames.includes('TypescriptNatsSettings')).toEqual(true);
expect(payloadNames.includes('OpenapiTypescriptFetch')).toEqual(true);
});
});
});
162 changes: 162 additions & 0 deletions test/configs/payload-complex.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
{
"asyncapi": "2.0.0",
"info": {
"title": "",
"version": "0.1.0",
"description": ""
},
"defaultContentType": "application/json",
"components": {
"messages": {
"complex_message": {
"name": "complex_message",
"payload": {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "complex_message",
"type": "object",
"properties": {
"types": {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "GeneratorTypes",
"type": "string",
"enum": ["openapi", "asyncapi"]
},
"openapi": {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "OpenAPIGenerator",
"type": "object",
"required": [],
"properties": {
"language": {
"const": "openapi"
},
"version": {
"type": "string"
},
"documentContext": {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "DocumentContext",
"type": "object",
"properties": {
"specificationDocument": {
"description": "The full spec file loaded into memory - Stringified",
"type": "string"
}
}
}
},
"allOf": [
{
"if": {
"properties": {
"language": {
"const": "typescript-fetch"
}
}
},
"then": {
"properties": {
"typeScriptFetchSettings": {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "openapi_typescript_fetch",
"type": "object",
"properties": {
"generatorVersion": {
"type": "string"
},
"npmReleaseConfig": {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "npm-release",
"type": "object",
"properties": {
"npmRepository": {
"type": "string",
"default": "https://registry.npmjs.org"
}
},
"required": ["npmRepository"]
}
},
"x-language": {
"const": "typescript-fetch"
},
"required": ["generatorVersion"]
}
}
}
}
]
},
"asyncapi": {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "AsyncAPIGenerator",
"type": "object",
"required": [],
"properties": {
"language": {
"const": "asyncapi"
},
"documentContext": {
"$ref": "#/components/schemas/simple"
}
},
"allOf": [
{
"if": {
"properties": {
"language": {
"const": "typescript-nats"
}
}
},
"then": {
"properties": {
"typeScriptNatsSettings": {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "typescript-nats-settings",
"type": "object",
"properties": {
"generatorVersion": {
"type": "string"
},
"npmReleaseConfig": {
"$ref": "#/components/schemas/simple"
}
},
"x-language": {
"const": "typescript-nats"
}
}
}
}
}
]
}
}
}
},
"schema_reference_no_name": {
"payload": {
"$ref": "#/components/schemas/simple"
}
},
"schema_reference": {
"name": "schema_reference_name",
"payload": {
"$ref": "#/components/schemas/simple"
}
}
},
"schemas": {
"simple": {
"type": "object",
"required": [],
"properties": {
"datetime": {
"type": "string"
}
}
}
}
}
}

0 comments on commit 7aa7867

Please sign in to comment.