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

DefectEntityModel getters throw ClassCastException on EmptyFieldModel #133

Closed
alb-i986 opened this issue Jan 18, 2022 · 0 comments · Fixed by #134
Closed

DefectEntityModel getters throw ClassCastException on EmptyFieldModel #133

alb-i986 opened this issue Jan 18, 2022 · 0 comments · Fixed by #134

Comments

@alb-i986
Copy link
Contributor

There's a bug in the sdk-generate-entity-models-maven-plugin.

Given a defect retrieved from the server with no owner set, DefectEntityModel#getOwner throws the following exception:

java.lang.ClassCastException: com.hpe.adm.nga.sdk.model.EmptyFieldModel cannot be cast to com.hpe.adm.nga.sdk.model.ReferenceFieldModel
  	at com.hpe.adm.nga.sdk.model.DefectEntityModel.getOwner(DefectEntityModel.java:1187)

Here is the implementation of the getOwner method, part of the class DefectEntityModel, which was generated by the maven plugin:

    public WorkspaceUserEntityModel getOwner(){
       final ReferenceFieldModel owner = (ReferenceFieldModel) wrappedEntityModel.getValue("owner");
		if (owner == null) {
            return null;
		}
		final EntityModel referenceFieldModel = owner.getValue();
                return new WorkspaceUserEntityModel(referenceFieldModel);
    }

The bug lies in the first line, which throws the ClassCastException above.

Instead of checking for null after the cast, it should check the type before attempting the cast:

if (wrappedEntityModel.getValue("owner") instanceof EmptyFieldModel) {
    return null;
}
final ReferenceFieldModel owner = (ReferenceFieldModel) wrappedEntityModel.getValue("owner");	

This applies to all the getters, I believe.

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

Successfully merging a pull request may close this issue.

1 participant