add own includes and imports

This commit is contained in:
marys
2026-06-08 14:27:51 +02:00
parent dd31750e1a
commit 0b0af515be
6 changed files with 45 additions and 41 deletions
+28 -17
View File
@@ -1,7 +1,7 @@
// 1
import { File } from '@asyncapi/generator-react-sdk';
// 2
import { PythonGenerator, FormatHelpers } from '@asyncapi/modelina';
import { PythonGenerator, PYTHON_PYDANTIC_PRESET } from '@asyncapi/modelina';
/**
* @typedef RenderArgument
@@ -9,25 +9,36 @@ import { PythonGenerator, FormatHelpers } from '@asyncapi/modelina';
* @property {AsyncAPIDocument} asyncapi document object received from the generator.
*/
function generateModels(model) {
const properties = model.model?.properties ?? {};
const ownIncludes = Object.values(properties)
.filter(p => p.property.constructor.name === 'ConstrainedReferenceModel')
.map(p => `from ${p.property.ref.name} import ${p.property.ref.name}`).join('\n')
const imports = model.dependencies
.join('\n');
return (
<File name={`${model.modelName}.py`}>
{imports}
{'\n\n'}
{ownIncludes}
{'\n\n'}
{model.result}
</File>
);
}
/**
* Render all schema models
* @param {RenderArgument} param0
* @returns
* @param {RenderArgument} param0
* @returns
*/
// 3
export default async function schemaRender({ asyncapi }) {
// 4
const pythonGenerator = new PythonGenerator();
// 5
const pythonGenerator = new PythonGenerator({
presets: [PYTHON_PYDANTIC_PRESET],
processorOptions: {}
});
const models = await pythonGenerator.generate(asyncapi);
// 6
const files = [];
// 7
for (const model of models) {
// 8
const modelFileName = `${FormatHelpers.toPascalCase(model.modelName)}.py`;
// 9
files.push(<File name={modelFileName}>{model.result}</File>);
}
return files;
return models.map(model => generateModels(model))
}