Skip to content

fix: unable to get model class when using arrays in schema #207

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

Merged

Conversation

CameronRushton
Copy link
Member

@CameronRushton CameronRushton commented Nov 4, 2021

Description
Resolves an NPE when attempting to use a null modelClass and additionally makes the asyncapi files in the issues usable. As part of making them usable, I changed how inner schemas are identified and created which includes schemas of type array.

The problem is that we created the modelClass map using the x-schema-parser-id as keys, then as we go through the schema when generating the template, we use the property name instead of the x-schema-parser-id. This leads to returning a null modelClass.
Now, we use the x-parser-schema-id property of the source schema first, then if that turns up nothing, try the property name to get the modelClass. Additionally, the modelClass map is populated with all possible keys for a modelClass. For example, there may be an entry like 'MyAnonSchema' -> ModelClass and another that has the same modelClass copied but for another key like 'anonymous-schema-3' -> ModelClass. This means that we're nearly guaranteed to get a hit when we're getting a modelClass by name.

This also works for $ref. Example:

{
    "title": "Myschema",
    "type": "object",
    "properties": {
        "my-prop": {
            "$ref": #/otherSchema
        }
    }
    ...
    "otherSchema": {
        "title": "somethingElse"
    }
}

Normally, the schema referred by my-prop will be empty since they’re no data for that ModelClass. Now, the otherSchema ModelClass will be picked up instead because we’ve accounted for this mapping indicated by the $ref. The name ‘my-prop’ isn’t lost though, yes we’re using the OtherSchema class for my-prop, but we’ll use @JsonProperty("my-prop") on OtherSchema otherSchema like we have been doing before. It’s debatable whether we still call the variable name my-prop throughout, ie OtherSchema myProp.

Aside from some spacing adjustments, this is the biggest change in the existing snapshots (two snapshots use this schema, one being the first test, the solace binder test).
solace binder test difference in schema (OLD -> NEW)
image

The all args constructor has been refactored into another partial as well.

Related issue(s)
Resolves #194 and resolves #201 which is a duplicate.

…o use the x-parser-schema-id property of the source schemafirst, then try the property name.
@CameronRushton CameronRushton changed the title fix: Error: Unable to get modelClass when using arrays in schema fix: Unable to get modelClass when using arrays in schema Nov 4, 2021
@CameronRushton CameronRushton changed the title fix: Unable to get modelClass when using arrays in schema fix: unable to get model class when using arrays in schema Nov 4, 2021
@CameronRushton CameronRushton marked this pull request as ready for review December 22, 2021 22:37
damaru-inc
damaru-inc previously approved these changes Dec 22, 2021
Copy link
Contributor

@damaru-inc damaru-inc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So very happy you're supporting this, Cameron, thanks very much!

@sonarqubecloud
Copy link

sonarqubecloud bot commented Jan 5, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.7% 0.7% Duplication

@damaru-inc damaru-inc merged commit 582fced into asyncapi:master Jan 5, 2022
@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 0.11.5 🎉

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
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error: Unable to call modelClass["getClassName"], which is undefined or falsey Generation of array types is not possible anymore
3 participants