michiel / jsonapi-rust

Rust library for serialization, deserialization and working with JSON-API data
MIT License
110 stars 30 forks source link

Resources with optional `id` #74

Open genbattle opened 4 years ago

genbattle commented 4 years ago

Thanks for providing this crate, I've found it really useful as a base for my jsonapi implementation.

One problem that I've run into is that the jsonapi spec allows id to be optional for create operations where an id will be generated. This library requires that all Resource objects have an id and they fail to serialize from any json resource representation that doesn't include an id field.

This might be achievable by expanding the PrimaryData definition to add an option which takes some kind of PartialResource or something. The id field of the Resource struct could be make optional, but that would violate the jsonapi spec in every case except this specific one.

I'm not sure what the right answer is, but I would appreciate it if this use case can be supported. For now I'm using dummy id values in my create requests as a workaround, but it doesn't match the behavior of other APIs we have that are implemented in other languages.