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

RC3 Updates for JSON API #853

Merged
merged 13 commits into from
Mar 24, 2015
Merged

Conversation

mateomurphy
Copy link
Contributor

  • Implements latest structure (data root, included, and linkages)
  • id and type are present on all resources, the latter is named consistently

This was referenced Mar 20, 2015
end

def type
object.class.to_s.demodulize.underscore.pluralize
Copy link
Member

Choose a reason for hiding this comment

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

  1. I believe this should go into the adapter because it's not really used by anyone else and it's a JSONAPI need.
  2. Having this in the serializer, we're forced to keep it public, which I think doesn't make much sense.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I see what you're doing. You are:

  1. relying on the serialized object's type, not the serializer name.
  2. leaving it here so people can override the inferred type.

Is that it? I'm onboard with that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, easily overriding the type is one of the main reasons I put this here. One can create an ApplicationSerializer and use different inflection rules.

I put the id here as well because it made thing more symmetrical, but I can remove it.

Choose a reason for hiding this comment

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

Wouldn't be a good idea to add comments or GOTCHA's to those methods with explanation why the was placed here and what the original intention was behind that decision?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, I was already planning on adding it to the documentation.

However, we need to figure out what to do about the id here. The main issue is that the jsonapi adapter requires an id attribute, so how do we handle the cases where the user doesn't add the id attribute to the configuration (or any other required attribute)? Raise an error, or add the id transparently? If the latter, I can implement a generic solution, something like

    def attributes(options = {})
      attributes =
        if options[:fields]
          self.class._attributes & options[:fields]
        else
          self.class._attributes.dup
        end

      attributes += options[:required_fields] if options[:required_fields]

      attributes.each_with_object({}) do |name, hash|
        hash[name] = read_attribute(name)
      end
    end

    def read_attribute(attr)
      if respond_to?(attr)
        send(attr)
      else
        object.read_attribute_for_serialization(attr)
      end
    end

Choose a reason for hiding this comment

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

I'd suggest raising an error when the id attribute is missing since in some cases the id used to build the URL isn't the resource's original id but a slug maybe.

@kurko
Copy link
Member

kurko commented Mar 21, 2015

Superb job here. I'm really glad this was done. Could you:

  1. edit README to mention we support RC3, and
  2. add an entry to the CHANGELOG?

@@ -172,6 +180,8 @@ def attributes(options = {})
self.class._attributes.dup
end

attributes += options[:required_fields] if options[:required_fields]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just noticed that this could lead to duplicates, I'll fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nvm it's fine

@joshsmith
Copy link
Contributor

@kurko ping. Looks like changelog and readme were updated.

@joshsmith
Copy link
Contributor

@mateomurphy can you rebase?

@mateomurphy mateomurphy force-pushed the jsonapi-format-updates branch from b04efe3 to b695180 Compare March 23, 2015 00:44
@mateomurphy
Copy link
Contributor Author

@joshsmith Yep, done

@@ -16,7 +16,7 @@ def initialize(serializer, options = {})
end

def serializable_hash(options = {})
@root = (@options[:root] || serializer.json_key.to_s.pluralize).to_sym
@root = :data

Choose a reason for hiding this comment

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

As this fixed by standard whould be better to move that into initialize method?

Choose a reason for hiding this comment

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

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, there's no reason to store the root, as it's not used elsewhere

@@ -164,6 +164,14 @@ def json_key
end
end

def id
object.id if object

Choose a reason for hiding this comment

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

The JSON API spec requires resource id to be a string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

Okay, great 👍

@kurko
Copy link
Member

kurko commented Mar 24, 2015

LGTM. HERE WE GO!

kurko added a commit that referenced this pull request Mar 24, 2015
@kurko kurko merged commit db788a5 into rails-api:master Mar 24, 2015
@kurko
Copy link
Member

kurko commented Mar 24, 2015

Reference #829

@mateomurphy mateomurphy deleted the jsonapi-format-updates branch March 24, 2015 23:37
@mateomurphy
Copy link
Contributor Author

I found some bugs right after you merged this. I fixed them here: #858

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants