Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: type are not determined in cyclic models #1906

Merged

Conversation

jonaslagoni
Copy link
Member

@jonaslagoni jonaslagoni commented Mar 18, 2024

Description

As #1903 explains, the underlying problem occurs because of the way we constrain models and their types, basically doing both at the same time.

This means that when we have circular models and they depend on each other, and the type has not been set yet, but we expect it to, we end up with the type having the value ''. I.e. why we end up with self._items: | List[] = input['items'] where the union model that items reference has not been sat when we set the item's type.

With this change in approach, we first build the constrained model (with any value that makes sense at the factory time), and then we apply types based on "safe types" first, and once those have been resolved, if we detect a circular model, we open it up by replacing it with AnyModel, and apply the more complex types automatically.

For example;

  • Model a: Union model with Model b
  • Model b: Union model with Model a

In such case we are unable to render the type, cause each depends on each other to determine the type.

The algorithm currently adapts the models so we end up with;

  • Model a: Union model with Model b
  • Model b: Union model with any model

Lastly, we then apply constants and discriminatory values (because they depend on type).

I would have loved to find a better way to do this implementation, but this is the best my bandwidth has come up with for the past weeks...

The reason this is not a breaking change is because it fixes types just being generated with '' values, and therefore this corrects that incorrect syntax. If a type would have been generated, this would have been a breaking change.

Fixes #1903
Blocked by #1893

@jonaslagoni jonaslagoni changed the base branch from master to next March 18, 2024 10:05
Copy link

netlify bot commented Mar 18, 2024

Deploy Preview for modelina canceled.

Name Link
🔨 Latest commit feac4af
🔍 Latest deploy log https://app.netlify.com/sites/modelina/deploys/662f79a0d253b40008e6efa2

@jonaslagoni jonaslagoni force-pushed the fix_incorrect_self_reference_problem branch from 95c97e3 to c6a8d72 Compare April 29, 2024 06:25
@jonaslagoni jonaslagoni changed the title fix!: incorrect self reference problem fix: type are not determined in cyclic models Apr 29, 2024
Copy link

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

@jonaslagoni jonaslagoni merged commit 6ff9698 into asyncapi:next May 6, 2024
39 of 47 checks passed
@jonaslagoni jonaslagoni deleted the fix_incorrect_self_reference_problem branch May 6, 2024 12:34
@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 4.0.0-next.36 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants