diff --git a/package-lock.json b/package-lock.json index f2ba216b47..bb97ac3846 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@asyncapi/modelina", - "version": "1.0.0-next.39", + "version": "1.0.0-next.40", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@asyncapi/modelina", - "version": "1.0.0-next.39", + "version": "1.0.0-next.40", "license": "Apache-2.0", "dependencies": { "@apidevtools/json-schema-ref-parser": "^9.0.9", diff --git a/package.json b/package.json index e23335e247..8ea324d31b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@asyncapi/modelina", - "version": "1.0.0-next.39", + "version": "1.0.0-next.40", "description": "Library for generating data models based on inputs such as AsyncAPI, OpenAPI, or JSON Schema documents", "license": "Apache-2.0", "homepage": "https://github.com/asyncapi/modelina", diff --git a/src/generators/csharp/CSharpConstrainer.ts b/src/generators/csharp/CSharpConstrainer.ts index fe38992424..8108a24e34 100644 --- a/src/generators/csharp/CSharpConstrainer.ts +++ b/src/generators/csharp/CSharpConstrainer.ts @@ -1,10 +1,8 @@ import { ConstrainedEnumValueModel, ConstrainedObjectPropertyModel } from '../../models'; -import { TypeMapping } from '../../helpers'; import { defaultEnumKeyConstraints, defaultEnumValueConstraints } from './constrainer/EnumConstrainer'; import { defaultModelNameConstraints } from './constrainer/ModelNameConstrainer'; import { defaultPropertyKeyConstraints } from './constrainer/PropertyKeyConstrainer'; -import { CSharpOptions, CSharpTypeMapping } from './CSharpGenerator'; -import { CSharpDependencyManager } from './CSharpDependencyManager'; +import { CSharpTypeMapping } from './CSharpGenerator'; function getFullTypeDefinition(typeName: string, partOfProperty: ConstrainedObjectPropertyModel | undefined) { return partOfProperty?.required ?? true @@ -30,7 +28,7 @@ const fromEnumValueToType = (enumValueModel: ConstrainedEnumValueModel): string } }; -export const CSharpDefaultTypeMapping: TypeMapping = { +export const CSharpDefaultTypeMapping: CSharpTypeMapping = { Object({ constrainedModel, partOfProperty }): string { return getFullTypeDefinition(constrainedModel.name, partOfProperty); }, diff --git a/src/generators/csharp/CSharpGenerator.ts b/src/generators/csharp/CSharpGenerator.ts index 0690a6409b..0661e57438 100644 --- a/src/generators/csharp/CSharpGenerator.ts +++ b/src/generators/csharp/CSharpGenerator.ts @@ -14,13 +14,13 @@ import { CSharpDefaultConstraints, CSharpDefaultTypeMapping } from './CSharpCons import { DeepPartial, mergePartialAndDefault } from '../../utils/Partials'; import { CSharpDependencyManager } from './CSharpDependencyManager'; -export type CSharpTypeMapping = TypeMapping export interface CSharpOptions extends CommonGeneratorOptions { collectionType: 'List' | 'Array'; - typeMapping: CSharpTypeMapping; + typeMapping: TypeMapping; constraints: Constraints; autoImplementedProperties: boolean; } +export type CSharpTypeMapping = TypeMapping; export interface CSharpRenderCompleteModelOptions { namespace: string @@ -41,7 +41,6 @@ export class CSharpGenerator extends AbstractGenerator { return {} as CSharpDependencyManager;} }; - static defaultCompleteModelOptions: CSharpRenderCompleteModelOptions = { namespace: 'Asyncapi.Models' }; @@ -53,11 +52,10 @@ export class CSharpGenerator extends AbstractGenerator): CSharpOptions { + static getCSharpOptions(options?: DeepPartial): CSharpOptions { const optionsToUse = mergePartialAndDefault(this.defaultOptions, options) as CSharpOptions; //Always overwrite the dependency manager unless user explicitly state they want it (ignore default temporary dependency manager) if (options?.dependencyManager === undefined) { diff --git a/src/generators/dart/DartDependencyManager.ts b/src/generators/dart/DartDependencyManager.ts index b246481074..ca64462339 100644 --- a/src/generators/dart/DartDependencyManager.ts +++ b/src/generators/dart/DartDependencyManager.ts @@ -1,3 +1,5 @@ +import { FormatHelpers } from '../../helpers'; +import { ConstrainedMetaModel } from '../../models'; import { AbstractDependencyManager } from '../AbstractDependencyManager'; import { DartOptions } from './DartGenerator'; @@ -8,4 +10,14 @@ export class DartDependencyManager extends AbstractDependencyManager { ) { super(dependencies); } + + renderImport(model: ConstrainedMetaModel, packageName: string): string { + return `import 'package:${packageName}/${FormatHelpers.snakeCase(model.name)}.dart';`; + } + + renderAllModelDependencies(model: ConstrainedMetaModel, packageName: string): string { + return model.getNearestDependencies().map((dependencyModel) => { + return this.renderImport(dependencyModel, packageName); + }).join('\n'); + } } diff --git a/src/generators/dart/DartGenerator.ts b/src/generators/dart/DartGenerator.ts index 2cb8b43640..8104fa2e74 100644 --- a/src/generators/dart/DartGenerator.ts +++ b/src/generators/dart/DartGenerator.ts @@ -10,16 +10,15 @@ import {ClassRenderer} from './renderers/ClassRenderer'; import {EnumRenderer} from './renderers/EnumRenderer'; import {isReservedDartKeyword} from './Constants'; import {Logger} from '../../'; -import {FormatHelpers} from '../../helpers/FormatHelpers'; import { DartDefaultConstraints, DartDefaultTypeMapping } from './DartConstrainer'; import { DeepPartial, mergePartialAndDefault } from '../../utils/Partials'; import { DartDependencyManager } from './DartDependencyManager'; -export type DartTypeMapping = TypeMapping; export interface DartOptions extends CommonGeneratorOptions { collectionType?: 'List'; - typeMapping: DartTypeMapping; + typeMapping: TypeMapping; constraints: Constraints; } +export type DartTypeMapping = TypeMapping; export interface DartRenderCompleteModelOptions { packageName: string; @@ -45,11 +44,10 @@ export class DartGenerator extends AbstractGenerator): DartOptions { + static getDartOptions(options?: DeepPartial): DartOptions { const optionsToUse = mergePartialAndDefault(DartGenerator.defaultOptions, options) as DartOptions; //Always overwrite the dependency manager unless user explicitly state they want it (ignore default temporary dependency manager) if (options?.dependencyManager === undefined) { @@ -127,10 +125,8 @@ export class DartGenerator extends AbstractGenerator { - return `import 'package:${completeModelOptionsToUse.packageName}/${FormatHelpers.snakeCase(dependencyModelName.name)}.dart';`; - }); - const outputContent = `${modelDependencies.join('\n')} + const modelDependencies = dependencyManagerToUse.renderAllModelDependencies(model, completeModelOptionsToUse.packageName); + const outputContent = `${modelDependencies} ${outputModel.dependencies.join('\n')} ${outputModel.result}`; return RenderOutput.toRenderOutput({ diff --git a/src/generators/go/GoGenerator.ts b/src/generators/go/GoGenerator.ts index f678b6b577..5e1e520134 100644 --- a/src/generators/go/GoGenerator.ts +++ b/src/generators/go/GoGenerator.ts @@ -12,12 +12,12 @@ import { Logger } from '../../utils/LoggingInterface'; import { GoDefaultConstraints, GoDefaultTypeMapping } from './GoConstrainer'; import { DeepPartial, mergePartialAndDefault } from '../../utils/Partials'; import { GoDependencyManager } from './GoDependencyManager'; -export type GoTypeMapping = TypeMapping; export interface GoOptions extends CommonGeneratorOptions { - typeMapping: GoTypeMapping; + typeMapping: TypeMapping; constraints: Constraints } +export type GoTypeMapping = TypeMapping; export interface GoRenderCompleteModelOptions { packageName: string @@ -38,7 +38,6 @@ export class GoGenerator extends AbstractGenerator, ) { @@ -49,7 +48,7 @@ export class GoGenerator extends AbstractGenerator): GoOptions { + static getGoOptions(options?: DeepPartial): GoOptions { const optionsToUse = mergePartialAndDefault(GoGenerator.defaultOptions, options) as GoOptions; //Always overwrite the dependency manager unless user explicitly state they want it (ignore default temporary dependency manager) if (options?.dependencyManager === undefined) { diff --git a/src/generators/java/JavaDependencyManager.ts b/src/generators/java/JavaDependencyManager.ts index 7de79bfd17..e8fa7e2e4d 100644 --- a/src/generators/java/JavaDependencyManager.ts +++ b/src/generators/java/JavaDependencyManager.ts @@ -1,3 +1,4 @@ +import { ConstrainedMetaModel } from '../../models'; import { AbstractDependencyManager } from '../AbstractDependencyManager'; import { JavaOptions } from './JavaGenerator'; @@ -8,4 +9,14 @@ export class JavaDependencyManager extends AbstractDependencyManager { ) { super(dependencies); } + + renderImport(model: ConstrainedMetaModel, packageName: string): string { + return `import ${packageName}.${model.name};`; + } + + renderAllModelDependencies(model: ConstrainedMetaModel, packageName: string): string { + return model.getNearestDependencies().map((dependencyModel) => { + return this.renderImport(dependencyModel, packageName); + }).join('\n'); + } } diff --git a/src/generators/java/JavaGenerator.ts b/src/generators/java/JavaGenerator.ts index bbd37f4e35..6bb9b93891 100644 --- a/src/generators/java/JavaGenerator.ts +++ b/src/generators/java/JavaGenerator.ts @@ -15,13 +15,12 @@ import { JavaDefaultConstraints, JavaDefaultTypeMapping } from './JavaConstraine import { DeepPartial, mergePartialAndDefault } from '../../utils/Partials'; import { JavaDependencyManager } from './JavaDependencyManager'; -export type JavaTypeMapping = TypeMapping; - export interface JavaOptions extends CommonGeneratorOptions { collectionType: 'List' | 'Array'; - typeMapping: JavaTypeMapping; + typeMapping: TypeMapping; constraints: Constraints; } +export type JavaTypeMapping = TypeMapping; export interface JavaRenderCompleteModelOptions { packageName: string } @@ -125,12 +124,10 @@ export class JavaGenerator extends AbstractGenerator { - return `import ${completeModelOptionsToUse.packageName}.${dependencyModel.name};`; - }); + const outputModel = await this.render(model, inputModel, optionsToUse); + const modelDependencies = dependencyManagerToUse.renderAllModelDependencies(model, completeModelOptionsToUse.packageName); const outputContent = `package ${completeModelOptionsToUse.packageName}; -${modelDependencies.join('\n')} +${modelDependencies} ${outputModel.dependencies.join('\n')} ${outputModel.result}`; return RenderOutput.toRenderOutput({result: outputContent, renderedName: outputModel.renderedName, dependencies: outputModel.dependencies}); diff --git a/src/generators/javascript/JavaScriptGenerator.ts b/src/generators/javascript/JavaScriptGenerator.ts index bd05cf0abb..2aa3593ef8 100644 --- a/src/generators/javascript/JavaScriptGenerator.ts +++ b/src/generators/javascript/JavaScriptGenerator.ts @@ -4,19 +4,19 @@ import { defaultGeneratorOptions } from '../AbstractGenerator'; import { ConstrainedMetaModel, ConstrainedObjectModel, InputMetaModel, MetaModel, RenderOutput } from '../../models'; -import { TypeMapping, Constraints, split, constrainMetaModel, renderJavaScriptDependency, SplitOptions } from '../../helpers'; +import { TypeMapping, Constraints, split, constrainMetaModel, SplitOptions } from '../../helpers'; import { JavaScriptPreset, JS_DEFAULT_PRESET } from './JavaScriptPreset'; import { ClassRenderer } from './renderers/ClassRenderer'; import { Logger } from '../../'; import { JavaScriptDefaultConstraints, JavaScriptDefaultTypeMapping } from './JavaScriptConstrainer'; import { DeepPartial, mergePartialAndDefault } from '../../utils'; import { JavaScriptDependencyManager } from './JavaScriptDependencyManager'; -export type JavaScriptTypeMapping = TypeMapping; export interface JavaScriptOptions extends CommonGeneratorOptions { - typeMapping: JavaScriptTypeMapping; + typeMapping: TypeMapping; constraints: Constraints; moduleSystem: 'ESM' | 'CJS'; } +export type JavaScriptTypeMapping = TypeMapping; // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface JavaScriptRenderCompleteModelOptions { } @@ -41,11 +41,10 @@ export class JavaScriptGenerator extends AbstractGenerator): JavaScriptOptions { + static getJavaScriptOptions(options?: DeepPartial): JavaScriptOptions { const optionsToUse = mergePartialAndDefault(JavaScriptGenerator.defaultOptions, options) as JavaScriptOptions; //Always overwrite the dependency manager unless user explicitly state they want it (ignore default temporary dependency manager) if (options?.dependencyManager === undefined) { diff --git a/src/generators/python/PythonDependencyManager.ts b/src/generators/python/PythonDependencyManager.ts index f7cfc11794..a041850f44 100644 --- a/src/generators/python/PythonDependencyManager.ts +++ b/src/generators/python/PythonDependencyManager.ts @@ -1,4 +1,4 @@ -import { ConstrainedMetaModel } from 'models'; +import { ConstrainedMetaModel } from '../../models'; import { AbstractDependencyManager } from '../AbstractDependencyManager'; import { PythonOptions } from './PythonGenerator'; @@ -10,7 +10,6 @@ export class PythonDependencyManager extends AbstractDependencyManager { super(dependencies); } - /** * Simple helper function to render a dependency based on the module system that the user defines. */ diff --git a/src/generators/python/PythonGenerator.ts b/src/generators/python/PythonGenerator.ts index 263a1eeb1d..dbafbf46a6 100644 --- a/src/generators/python/PythonGenerator.ts +++ b/src/generators/python/PythonGenerator.ts @@ -14,13 +14,13 @@ import { constrainMetaModel, Constraints } from '../../helpers/ConstrainHelpers' import { PythonDefaultConstraints, PythonDefaultTypeMapping } from './PythonConstrainer'; import { DeepPartial, mergePartialAndDefault } from '../../utils/Partials'; import { PythonDependencyManager } from './PythonDependencyManager'; -export type PythonTypeMapping = TypeMapping; export interface PythonOptions extends CommonGeneratorOptions { - typeMapping: PythonTypeMapping; + typeMapping: TypeMapping; constraints: Constraints; importsStyle: 'explicit' | 'implicit'; } +export type PythonTypeMapping = TypeMapping; // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface PythonRenderCompleteModelOptions { } @@ -47,7 +47,7 @@ export class PythonGenerator extends AbstractGenerator): PythonOptions { + static getPythonOptions(options?: DeepPartial): PythonOptions { const optionsToUse = mergePartialAndDefault(PythonGenerator.defaultOptions, options) as PythonOptions; //Always overwrite the dependency manager unless user explicitly state they want it (ignore default temporary dependency manager) if (options?.dependencyManager === undefined) { @@ -125,7 +125,7 @@ export class PythonGenerator extends AbstractGenerator {return dependencyManagerToUse.renderDependency(model)}); + const modelDependencies = model.getNearestDependencies().map((model) => {return dependencyManagerToUse.renderDependency(model);}); const outputContent = `${modelDependencies.join('\n')} ${outputModel.dependencies.join('\n')} ${outputModel.result}`; diff --git a/src/generators/rust/RustConstrainer.ts b/src/generators/rust/RustConstrainer.ts index 101bdb5866..8265f7716e 100644 --- a/src/generators/rust/RustConstrainer.ts +++ b/src/generators/rust/RustConstrainer.ts @@ -1,10 +1,9 @@ import { defaultEnumKeyConstraints, defaultEnumValueConstraints } from './constrainer/EnumConstrainer'; import { defaultModelNameConstraints } from './constrainer/ModelNameConstrainer'; import { defaultPropertyKeyConstraints } from './constrainer/PropertyKeyConstrainer'; -import { RustOptions, RustTypeMapping } from './RustGenerator'; -import { FormatHelpers, Constraints, TypeMapping } from '../../helpers'; +import { RustTypeMapping } from './RustGenerator'; +import { FormatHelpers, Constraints } from '../../helpers'; import { ConstrainedAnyModel, ConstrainedArrayModel, ConstrainedDictionaryModel, ConstrainedEnumModel, ConstrainedFloatModel, ConstrainedMetaModel, ConstrainedObjectModel, ConstrainedReferenceModel, ConstrainedStringModel, ConstrainedTupleModel, ConstrainedUnionModel } from '../../models'; -import { RustDependencyManager } from './RustDependencyManager'; export function deriveHash(model: ConstrainedMetaModel): boolean { // float primitives and std::collection::HashMap do not implement Hash trait diff --git a/src/generators/rust/RustDependencyManager.ts b/src/generators/rust/RustDependencyManager.ts index c1d8fb6bad..2ab397c2e7 100644 --- a/src/generators/rust/RustDependencyManager.ts +++ b/src/generators/rust/RustDependencyManager.ts @@ -8,5 +8,4 @@ export class RustDependencyManager extends AbstractDependencyManager { ) { super(dependencies); } - } diff --git a/src/generators/rust/RustGenerator.ts b/src/generators/rust/RustGenerator.ts index 8d22733fd3..f38183f3e2 100644 --- a/src/generators/rust/RustGenerator.ts +++ b/src/generators/rust/RustGenerator.ts @@ -3,7 +3,7 @@ import { CommonGeneratorOptions, defaultGeneratorOptions } from '../AbstractGenerator'; -import { OutputModel, InputMetaModel, RenderOutput, ConstrainedObjectModel, ConstrainedEnumModel, ConstrainedMetaModel, MetaModel, ConstrainedTupleModel, ConstrainedUnionModel, UnionModel } from '../../models'; +import { OutputModel, InputMetaModel, RenderOutput, ConstrainedObjectModel, ConstrainedEnumModel, ConstrainedMetaModel, MetaModel, ConstrainedTupleModel, ConstrainedUnionModel } from '../../models'; import { constrainMetaModel, Constraints, IndentationTypes, split, SplitOptions, TypeMapping } from '../../helpers'; import { RustPreset, RUST_DEFAULT_PRESET } from './RustPreset'; import { StructRenderer } from './renderers/StructRenderer'; @@ -15,11 +15,11 @@ import { UnionRenderer } from './renderers/UnionRenderer'; import { PackageRenderer } from './renderers/PackageRenderer'; import { DeepPartial, mergePartialAndDefault } from '../../utils/Partials'; import { RustDependencyManager } from './RustDependencyManager'; -export type RustTypeMapping = TypeMapping; export interface RustOptions extends CommonGeneratorOptions { - typeMapping: RustTypeMapping; + typeMapping: TypeMapping; constraints: Constraints } +export type RustTypeMapping = TypeMapping; export enum RustPackageFeatures { json, @@ -165,7 +165,7 @@ export class RustGenerator extends AbstractGenerator, options: DeepPartial): Promise { - const _ = RustGenerator.getRustCompleteOptions(completeModelOptions); + //const _ = RustGenerator.getRustCompleteOptions(completeModelOptions); const optionsToUse = RustGenerator.getRustOptions({...this.options, ...options}); Logger.debug('Generating complete models with options: ', optionsToUse); const outputModel = await this.render(model, inputModel); diff --git a/src/generators/typescript/TypeScriptGenerator.ts b/src/generators/typescript/TypeScriptGenerator.ts index 229c8d2a46..0a910e6d90 100644 --- a/src/generators/typescript/TypeScriptGenerator.ts +++ b/src/generators/typescript/TypeScriptGenerator.ts @@ -4,7 +4,7 @@ import { defaultGeneratorOptions } from '../AbstractGenerator'; import { ConstrainedEnumModel, ConstrainedMetaModel, ConstrainedObjectModel, InputMetaModel, MetaModel, RenderOutput } from '../../models'; -import { constrainMetaModel, Constraints, hasPreset, split, SplitOptions, TypeMapping } from '../../helpers'; +import { constrainMetaModel, Constraints, split, SplitOptions, TypeMapping } from '../../helpers'; import { TypeScriptPreset, TS_DEFAULT_PRESET } from './TypeScriptPreset'; import { ClassRenderer } from './renderers/ClassRenderer'; import { InterfaceRenderer } from './renderers/InterfaceRenderer'; @@ -21,7 +21,7 @@ export interface TypeScriptOptions extends CommonGeneratorOptions; constraints: Constraints; moduleSystem: TypeScriptModuleSystemType; } @@ -118,7 +118,7 @@ export class TypeScriptGenerator extends AbstractGenerator = { diff --git a/src/utils/Partials.ts b/src/utils/Partials.ts index ff59a702cf..15811d9417 100644 --- a/src/utils/Partials.ts +++ b/src/utils/Partials.ts @@ -6,7 +6,6 @@ // eslint-disable-next-line @typescript-eslint/ban-types export type DeepPartial = T extends Function ? T : (T extends object ? { [P in keyof T]?: DeepPartial; } : T); - /** * Return true or false based on whether the input object is a regular object or a class * @@ -15,14 +14,14 @@ export type DeepPartial = T extends Function ? T : (T extends object ? { [P i */ function isClass(obj: any): boolean { const isCtorClass = obj.constructor - && obj.constructor.toString().substring(0, 5) === 'class' - if(obj.prototype === undefined) { - return isCtorClass + && obj.constructor.toString().substring(0, 5) === 'class'; + if (obj.prototype === undefined) { + return isCtorClass; } const isPrototypeCtorClass = obj.prototype.constructor && obj.prototype.constructor.toString - && obj.prototype.constructor.toString().substring(0, 5) === 'class' - return isCtorClass || isPrototypeCtorClass + && obj.prototype.constructor.toString().substring(0, 5) === 'class'; + return isCtorClass || isPrototypeCtorClass; } /** diff --git a/test/utils/Partials.spec.ts b/test/utils/Partials.spec.ts index 1c1db872a1..1d4756a178 100644 --- a/test/utils/Partials.spec.ts +++ b/test/utils/Partials.spec.ts @@ -68,7 +68,7 @@ describe('mergePartialAndDefault', () => { test('should keep class types for instances of non-regular objects', () => { class TestClass { - test(){ return true }; + test() { return true; } } interface TestType { nestedObject: TestClass,