Skip to content

fix: template uses anonymous schema name when object type is unknown #296

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
merged 3 commits into from
Oct 20, 2022

Conversation

CameronRushton
Copy link
Member

@CameronRushton CameronRushton commented Oct 20, 2022

Description

Throw error when schema type is unknown instead of setting to the x-schema-parser-id.

This doesn't break the functionality of setting $ref for a referenced schema (that doesn't have type set)
Given

"components": {
    "schemas": {
      "FelineSchema": {
        "type": "object",
        "properties": {
          "canMeow": {
            "$ref": "#/components/schemas/CatSchema"
          }
        }
      },
      "CatSchema": {
        "type": "object",
        "properties": {
          "meow": {
            "type": "integer"
          }
        }
      }
    }
  }

'canMeow' doesn't have a type but that's okay because in the modelClass mapping, we said that canMeow maps to the CatSchema object which does have a type so we get the following correct result with CatSchema defined in its own file.

@JsonInclude(JsonInclude.Include.NON_NULL)
public class FelineSchema {

	public FelineSchema () {
	}

	public FelineSchema (
		CatSchema catSchema) {
		this.catSchema = catSchema;
	}

	@JsonProperty("canMeow")
	private CatSchema catSchema;
	
	public CatSchema getCatSchema() {
		return catSchema;
	}

	public FelineSchema setCatSchema(CatSchema catSchema) {
		this.catSchema = catSchema;
		return this;
	}

	public String toString() {
		return "FelineSchema ["
		+ " catSchema: " + catSchema
		+ " ]";
	}
}

Related issue(s)
Resolves #288

Comment on lines 342 to +343
} else {
// check to see if it's a ref to another schema.
typeName = property.ext('x-parser-schema-id');

if (!typeName) {
throw new Error(`Can't determine the type of property ${ name}`);
}
throw new Error(`Can't determine the type of property ${ name }`);
Copy link
Member Author

Choose a reason for hiding this comment

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

We could assume it's an object and the class name derived from the model class mapping is correct with
typeName = javaName; but it may not always be true. The model class mapping takes the x-schema-parser-id into account when determining the class name so typeName = javaName here should be the same idea. However, we'd only do this if we're okay with guessing the type.
If we do guess, this error here would never be thrown because (I think) there is always an x-parser-schema-id present.

@CameronRushton CameronRushton changed the title template uses anonymous schema name when object type is unknown fix: template uses anonymous schema name when object type is unknown Oct 20, 2022
@sonarqubecloud
Copy link

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.0% 0.0% Duplication

@CameronRushton CameronRushton marked this pull request as ready for review October 20, 2022 16:19
@CameronRushton CameronRushton merged commit 860152d into asyncapi:master Oct 20, 2022
@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 0.13.3 🎉

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.

Generator produces Java code with <anonymous-schema-6> Object types
4 participants