Skip to content

Fix #71: Nova: Permit host-aggregate metadata to be deleted#106

Closed
bboyHan wants to merge 1 commit intoopenstack4j:masterfrom
bboyHan:serialize-null-value
Closed

Fix #71: Nova: Permit host-aggregate metadata to be deleted#106
bboyHan wants to merge 1 commit intoopenstack4j:masterfrom
bboyHan:serialize-null-value

Conversation

@bboyHan
Copy link
Copy Markdown
Contributor

@bboyHan bboyHan commented Nov 13, 2020

#71

@olivergondza
Copy link
Copy Markdown
Member

@bboyHan, thanks for the patch. Note that Jackson seems to have corresponding functionality already: http://fasterxml.github.io/jackson-annotations/javadoc/2.6/com/fasterxml/jackson/annotation/JsonInclude.html

@olivergondza olivergondza changed the title allow null value to be serialized. Fix #71: Nova: Permit host-aggregate metadata to be deleted Nov 16, 2020
@bboyHan
Copy link
Copy Markdown
Contributor Author

bboyHan commented Nov 16, 2020

The problem does not seem to be that JSON serialization is not supported.

But because the api used to set parameters in class of org.openstack4j.core.transport.ObjectMapperSingleton:

mapper.setSerializationInclusion(Include.NON_NULL);

So it can not support this problem. #106

@olivergondza
Copy link
Copy Markdown
Member

Good point, I have not realized the code is actually forcing the use of Include.NON_NULL. I such a case, I suggest removing the new annotation (@SerializationAllowNull) and check if the class is annotated with @JsonInclude and set its exact declared value to #setSerializationInclusion().

It will make the model objects use the standard Jackson mechanism and gain more flexibility as it would support all the other inclusion strategies.

Let me have a look at this.

metadata.put("key1", "value1");
metadata.put("key2", null);
HostAggregate hostAggregate = osv3().compute().hostAggregates().setMetadata("aggregateId", metadata);
assertNotNull(hostAggregate);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The test is passing even without the fix.

return type.getAnnotation(JsonRootName.class) == null ? INSTANCE.mapper : INSTANCE.rootMapper;
return type.getAnnotation(JsonRootName.class) == null ?
type.getAnnotation(SerializationAllowNull.class) == null ? INSTANCE.mapper : INSTANCE.mapper.setSerializationInclusion(Include.ALWAYS) :
type.getAnnotation(SerializationAllowNull.class) == null ? INSTANCE.rootMapper : INSTANCE.rootMapper.setSerializationInclusion(Include.ALWAYS);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Careful here, it changes the state of the shared mappers so it will propagate to unrelated serializations.

olivergondza added a commit to olivergondza/openstack4j that referenced this pull request Nov 16, 2020
Fix openstack4j#71: Nova: Permit host-aggregate metadata to be deleted
@olivergondza
Copy link
Copy Markdown
Member

Replaced by #110

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 this pull request may close these issues.

2 participants