Skip to content

WIP: Add partial resources #77

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

Closed
wants to merge 1 commit into from

Conversation

johnchildren
Copy link

Adds a notion of partial resources which are an extension of the
PrimaryData internal type. This allows for separate definitions of
models that do not have id fields and are not expected to have included
data.

This is useful in cases where a server should generate the IDs for
resources: https://jsonapi.org/format/1.0/#crud-creating-client-ids

There may be a few edge cases where a user wants support included resources
without an id in the primary data, or wants to only submit relationships
with an ID, but this can be resolved separately.

TODO:

  • Check sanity of this change
  • Document methods
  • Document macros
  • More tests
  • Fixup commits

Hi,

Have been thinking about how to resolve #74 and therefore implemented the suggestion of PartialResources to what I would understand them to be. I have added a test, but this change requires an extra macro for implementing a new PartialJsonApiModel trait, with the idea that users would invoke this macro for a separate struct, though I am unsure on the ergonomics of this.

If there is any feedback on these changes I would appreciate it, otherwise I will try to fixup this PR over the course of the next few weeks with docs and tests.

Adds a notion of partial resources which are an extension of the
`PrimaryData` internal type. This allows for separate definitions of
models that do not have `id` fields and are not expected to have included
data.

This is useful in cases where a server should generate the IDs for
resources: https://jsonapi.org/format/1.0/#crud-creating-client-ids

There may be a few edge cases where a user wants support included resources
without an id in the primary data, or wants to only submit relationships
with an ID, but this can be resolved separately.

TODO:

- Check sanity of this change
- Document methods
- More tests
match doc.data.as_ref() {
Some(primary_data) => match *primary_data {
PrimaryData::None => bail!("Document had no data"),
PrimaryData::Single(_) => unimplemented!(),
Copy link
Author

@johnchildren johnchildren Oct 28, 2020

Choose a reason for hiding this comment

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

This needs an implementation - presumably you should be able to build a partial resource from a full resource, though not the other way around.

partial_jsonapi_model!(NewAuthor; "authors"; has many books);

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct NewBook {
Copy link
Author

@johnchildren johnchildren Oct 28, 2020

Choose a reason for hiding this comment

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

Should include a test that this model generates relationships and doesn't just smuggle things past seralisation as an attribute.

@johnchildren johnchildren marked this pull request as draft November 13, 2020 16:21
@johnchildren
Copy link
Author

I don't think I'm going to get a chance to finish this unfortunately, and I'm not convinced it is even the best approach.

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.

Resources with optional id
1 participant