-
Notifications
You must be signed in to change notification settings - Fork 477
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
Make mixins public to make re-use of Jackson2HalModule easier #492
base: main
Are you sure you want to change the base?
Conversation
+1 |
+1 |
@olivergierke any concerns about this PR? Is it not the intended way to use the mixins? Anything we can do to get it merged sooner? Thanks! |
@jstaffans Exactly how are you running into a visibility issue? Are you trying to use the MixIns outside of |
@gregturn yes, something like that. I was trying to customise the way links and embeds were serialised. I'm however no longer working on the project for which I needed the customisation and am a bit hazy on the details. If nothing else though, the changes in this PR make the mixin classes consistent in their visibility (the |
Thanks for getting back to me. |
Resolved via 135aca9 |
Can we take a step back and reconsider? "Something like that" is not a very good justification to make things public that don't need to be public. I'd love to learn how "trying to customize the way links and embeds" makes sense in a HAL context, as it defines the format pretty precisely. |
Actually, Happy to reconsider if we learn about a use case in detail. |
I didn't agree with extensibility but I did agree with consistency. I was just picking the option that had 0% chance of breaking backward compatibility. I can make them all package protected D that's preferred. |
See #549 |
I'd vote for resetting |
@olivergierke "...as it defines the format pretty precisely..." - not exactly, for instance look at the issue with the single item in lists - #288. The specification says that
Which is ambiguous in case of the single-item array. The latter is the reason I voted +1 for this PR since I would be able to change current behavior (such array represented as a single item) and fit it for our project needs (represented as a 1-item array). |
We already expose a boolean flag to control whether to prefer collection relations (defaulting to Actually that's the reason I'd like to learn about the actual use case. What are you trying to do exactly? What is the representation you see? What do you want to see? What do you think prevents you from achieving the latter. "I need the types to be public" is way too much thinking in implementation, i.e. the solution space. "I don't have fine grained enough control over whether a single link shall be rendered as array or not" is thinking in the problem space. We need to do the latter before we do the former. 🙂 |
@olivergierke Personally I would prefer having you guys closed this one (with |
My use case was serialising an empty collection as an empty JSON array instead of leaving out the embed completely. As far as I can recall, this would have required a modification of "_embedded": {
"users": []
} The HAL specification is pretty light on details for cases like this, I think. I agree with the comment above by @Berastau about it being a matter of either designing for flexibility through configuration or extensibility. |
I'm running into the same issue with |
@jstaffans Please sign the Contributor License Agreement! Click here to manually synchronize the status of this Pull Request. See the FAQ for frequently asked questions. |
@jstaffans Thank you for signing the Contributor License Agreement! |
We currently have It's not outrageous to extend this to ALSO define an option for rendering empty stuff. The code inside public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
List<?> list = (List<?>) value;
if (list.isEmpty()) {
return;
}
if (list.size() == 1 && this.halConfiguration.getRenderSingleLinks() == RenderSingleLinks.AS_SINGLE) {
serializeContents(list.iterator(), jgen, provider);
return;
}
jgen.writeStartArray();
serializeContents(list.iterator(), jgen, provider);
jgen.writeEndArray();
} It wouldn't be hard to add another setting for |
I have poked at this, and don't really see a path forward. @Test // #492
void renderEmbeddedWithEmptyCollection() throws JsonProcessingException {
mapper = HalTestUtils.halObjectMapper(new HalConfiguration().withRenderEmptyCollections(true));
CollectionModel<?> model = CollectionModel.empty();
model.add(Link.of("http://example.com"));
String serialized = mapper.writeValueAsString(model);
assertThat(serialized).isEqualTo("{\"_embedded\":{},\"_links\":{\"self\":{\"href\":\"http://example.com\"}}}");
} This prototype only gets you an empty collection, not a link relation with an empty list. The only thing that appears to work, is to use HAL directly, like this: @Test // #492
void renderEmbeddedWithEmptyLinkRelation() throws JsonProcessingException {
CollectionModel<?> model = CollectionModel
.of(Arrays.asList(new EmbeddedWrappers(false).emptyCollectionOf(SomeSample.class)));
model.add(Link.of("http://example.com"));
String serialized = mapper.writeValueAsString(model);
assertThat(serialized)
.isEqualTo("{\"_embedded\":{\"someSample\":[]},\"_links\":{\"self\":{\"href\":\"http://example.com\"}}}");
} I don't see how this can be picked up and generalized for the vendor-neutral API. Essentially, what you are asking HAL to do in the event of an empty collection, is glean what the generic type is without there being any data there. Java doesn't make it easy to find a generic parameter unless something is there. Unless you manually create a list with the type in it. Framework code doesn't lend itself to this sort of general behavior. Suggestions welcome! |
2e02d7a
to
856b6b9
Compare
5828e78
to
e643c37
Compare
I would like to be able to use my own implementation of Jackson2HalModule but re-use the existing mixin classes found in the
org.springframework.hateoas.hal
package. One mixin was already declared public (theResourcesMixin
class). This PR just gives the two other mixins the same visibility.