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

Custom type in array key is not respected when serializing to JSON #1223

Closed
eduardoweiland opened this issue Jun 30, 2020 · 5 comments
Closed

Comments

@eduardoweiland
Copy link
Contributor

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no

I have a property annotated with key-value syntax for array, like @Type("array<K, V>"), where K is a custom type I defined. When a JSON is deserialized, the key is passed to my custom handler and it gets deserialized, as expected. But when it is serialized back to JSON, the custom handler is not called and the key is used as-is in the JSON output.

The custom type was defined to handle a map where the key was a database ID. In the API I'm working, all database IDs should be converted using Hashids. This handler is working well in other places, it just does not work for array keys. Also, I'm not sure how this might affect other formats, such as XML, as I'm only using JSON.

Steps required to reproduce the problem

I have created a small repo reproducing this problem: https://github.com/eduardoweiland/jmsserializer-json-map-key-transform

Expected Result

  • Custom handler should be called to serialize array key

Actual Result

  • Custom handler is not called to serialize array key
@eduardoweiland
Copy link
Contributor Author

I managed to work around this problem by creating a new custom type to handle serialization of this kind of array.

@goetas
Copy link
Collaborator

goetas commented Jul 2, 2020

type handlers on the array keys is something that is not supported (as min most of the cases keys are simple scalars as integers or strings).
I had a similar usecase in the past but decided to postpone it as it was impacting on performance and code complexity, for a very low number of cases where the feature is actually needed.

@eduardoweiland
Copy link
Contributor Author

So the docs is misleading about this. It states array<K, V>: A map of keys of type K to values of type V. Examples: array<string, string>, array<string, MyNamespaceMyObject>, etc.

Based on this, I thought that any type K would work. But, in fact, type K is only used for deserializing and it's completely ignored when serializing. Maybe adding a note in the docs could clarify this limitation.

@goetas
Copy link
Collaborator

goetas commented Jul 9, 2020

It would be great if you could add a note in the documentation about this (https://github.com/schmittjoh/serializer/tree/master/doc/reference)

@goetas
Copy link
Collaborator

goetas commented Jul 13, 2020

solved in #1235

@goetas goetas closed this as completed Jul 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants