diff --git a/docs/migrations/version-3-to-4.md b/docs/migrations/version-3-to-4.md index d2581755d1..525b445ba1 100644 --- a/docs/migrations/version-3-to-4.md +++ b/docs/migrations/version-3-to-4.md @@ -2,13 +2,6 @@ This document contain all the breaking changes and migrations guidelines for adapting your code to the new version. -## Rewrite of how types are determined - -In v3 and below, there was a edge case where a type could not be determined IF it was a cyclic model that was NOT split out into separate models. This change affects all generators to various degrees. - -For example - - ## Deprecation of `processor.interpreter` Since the early days we had the option to set `processorOptions.interpreter` options to change how JSON Schema is interpreted to Meta models. However, these options are more accurately part of the `processorOptions.jsonSchema` options. diff --git a/modelina-cli/package-lock.json b/modelina-cli/package-lock.json index d456e0cda2..f6eb206c81 100644 --- a/modelina-cli/package-lock.json +++ b/modelina-cli/package-lock.json @@ -187,7 +187,7 @@ "node_modules/@asyncapi/modelina": { "version": "4.0.0-next.35", "resolved": "file:scripts/modelina-package/asyncapi-modelina.tgz", - "integrity": "sha512-obL4nusJo8o6YseNm1g93dc3K0c+CDIbx6fEKRKIah9X3FGu6omP847om+o/5pMQHCrZd4xjr238QoEC/cudSg==", + "integrity": "sha512-yYSfE0UIuCrzcxDYkUygbm1nFjSGrx16KyT12QofnSQJG8aqGHchuS3JvperfPUU6HYVJ/fJ2LrojkdLGLQeyg==", "license": "Apache-2.0", "dependencies": { "@apidevtools/json-schema-ref-parser": "^11.1.0", diff --git a/src/helpers/ConstrainedTypes.ts b/src/helpers/ConstrainedTypes.ts index 7be6362838..0602f99b40 100644 --- a/src/helpers/ConstrainedTypes.ts +++ b/src/helpers/ConstrainedTypes.ts @@ -12,7 +12,6 @@ import { ConstrainedUnionModel } from '../models/ConstrainedMetaModel'; import { Constraints } from './MetaModelToConstrained'; - import { TypeMapping, getTypeFromMapping } from './TypeHelpers'; export interface ApplyingTypesOptions< @@ -35,6 +34,42 @@ export interface ApplyingTypesOptions< constrainRules: Constraints; } +/** + * Apply const and type to models from the constraint rules + */ +function applyTypeAndConst< + GeneratorOptions, + DependencyManager extends AbstractDependencyManager +>(context: ApplyingTypesOptions) { + const { + constrainedModel, + typeMapping, + partOfProperty, + dependencyManager, + generatorOptions, + alreadySeenModels, + constrainRules + } = context; + if (constrainedModel.type !== '') { + return; + } + constrainedModel.type = getTypeFromMapping(typeMapping, { + constrainedModel, + options: generatorOptions, + partOfProperty, + dependencyManager + }); + + if (constrainedModel.options.const) { + const constrainedConstant = constrainRules.constant({ + constrainedMetaModel: constrainedModel, + options: generatorOptions + }); + constrainedModel.options.const.value = constrainedConstant; + } + alreadySeenModels.set(constrainedModel, constrainedModel.type); +} + /** * Applying types and const through cyclic analysis (https://en.wikipedia.org/wiki/Cycle_(graph_theory)) * to detect and adapt unmanageable cyclic models where we cant determine types as normal. @@ -56,37 +91,11 @@ export function applyTypesAndConst< >( context: ApplyingTypesOptions ): ConstrainedMetaModel | undefined { - const { - constrainedModel, - safeTypes, - typeMapping, - partOfProperty, - dependencyManager, - generatorOptions, - alreadySeenModels, - constrainRules - } = context; + const { constrainedModel, safeTypes, alreadySeenModels } = context; const isCyclicModel = alreadySeenModels.has(constrainedModel) && alreadySeenModels.get(constrainedModel) === undefined; const hasBeenSolved = alreadySeenModels.has(constrainedModel); - const applyTypeAndConst = (model: ConstrainedMetaModel) => { - model.type = getTypeFromMapping(typeMapping, { - constrainedModel: model, - options: generatorOptions, - partOfProperty, - dependencyManager - }); - - if (model.options.const) { - const constrainedConstant = constrainRules.constant({ - constrainedMetaModel: model, - options: generatorOptions - }); - model.options.const.value = constrainedConstant; - } - alreadySeenModels.set(model, model.type); - }; if (isCyclicModel) { //Cyclic models detected, having to make the edge (right before cyclic occur) to use AnyModel (most open type we have) @@ -101,7 +110,7 @@ export function applyTypesAndConst< constrainedModel.options, '' ); - applyTypeAndConst(anyModel); + applyTypeAndConst({ ...context, constrainedModel: anyModel }); return anyModel; } else if (hasBeenSolved) { return undefined; @@ -113,7 +122,7 @@ export function applyTypesAndConst< //Walk over all safe models that can determine it's type right away for (const safeType of safeTypes) { if (constrainedModel instanceof safeType) { - applyTypeAndConst(constrainedModel); + applyTypeAndConst({ ...context, constrainedModel }); break; } } @@ -131,7 +140,7 @@ function walkNode< GeneratorOptions, DependencyManager extends AbstractDependencyManager >(context: ApplyingTypesOptions) { - const { constrainedModel, constrainRules, generatorOptions } = context; + const { constrainedModel } = context; if (constrainedModel instanceof ConstrainedObjectModel) { walkObjectNode(context); @@ -146,26 +155,8 @@ function walkNode< } else if (constrainedModel instanceof ConstrainedReferenceModel) { walkReferenceNode(context); } - if (constrainedModel.type === '') { - constrainedModel.type = getTypeFromMapping(context.typeMapping, { - constrainedModel, - options: context.generatorOptions, - partOfProperty: context.partOfProperty, - dependencyManager: context.dependencyManager - }); - context.alreadySeenModels.set(constrainedModel, constrainedModel.type); - } - if ( - constrainedModel instanceof ConstrainedReferenceModel && - constrainedModel.options.const - ) { - const constrainedConstant = constrainRules.constant({ - constrainedMetaModel: constrainedModel, - options: generatorOptions - }); - constrainedModel.options.const.value = constrainedConstant; - } + applyTypeAndConst({ ...context, constrainedModel }); if (constrainedModel instanceof ConstrainedUnionModel) { addDiscriminatorTypeToUnionModel(constrainedModel);