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

Namespace separator fix for type field in jsonapi adapter #1216

Closed
wants to merge 2 commits into from
Closed

Namespace separator fix for type field in jsonapi adapter #1216

wants to merge 2 commits into from

Conversation

youroff
Copy link
Contributor

@youroff youroff commented Sep 30, 2015

This PR removes namespace from automatically generated type field.
Discussion's going on here:
http://discuss.jsonapi.org/t/convention-for-namespacing-type/99

@@ -212,7 +212,7 @@ def test_underscore_model_namespace_for_linked_resource_type
expected = {
related: {
data: [{
type: 'spam_unrelated_links',
type: 'unrelated_links',
Copy link
Member

Choose a reason for hiding this comment

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

why did the expectation change here?

aside, the type should probably be spam:unrelated_links' orspam-unrelated_links`

json-api/json-api#850 (the issue I made that the jsonapi forum references )

(aside, why is there a related= writer on Post that is important? just confusing naming)

@youroff
Copy link
Contributor Author

youroff commented Oct 1, 2015

The idea was to reject namespace by default:

  • There's no standard way to encode namespace yet.
  • The way how it was done before is confusing: Namespace::SomeModel was converted to namespace_some_model which is the same as it would be for the model NamespaceSomeModel.

So my idea was to cut namespace by default, but type directive will be there to set it explicitly.

@youroff
Copy link
Contributor Author

youroff commented Oct 1, 2015

For #1029 I would stick with / separator for namespace, but it's against json-api standard (hopefully it can be changed to comply that usecase). But _ or - wouldn't work as it's standard separator for words in multiword classes. Which will bring ambiguity.

@bf4
Copy link
Member

bf4 commented Oct 1, 2015

@youroff agree about _ and -. how about spam:unrelated_links ? Or even spam::unrelated_links ( U+003A COLON, ":" is disallowed)
per spec recommendation json-api/json-api#850 (comment) it would be spam--unrelated_links which I'm ok with

it's up to us, and we can create the ruby convention right here.

@beauby
Copy link
Contributor

beauby commented Oct 1, 2015

After giving it a thought, I tend to agree with @bf4. A user that uses namespaces should know what they are doing, and overriding the type with type "MyType" seems like a good option for those willing to get rid of the namespace.

@youroff
Copy link
Contributor Author

youroff commented Oct 1, 2015

Honestly, / looks more reasonable for me, cause it's common sign for paths (which namespaces in fact are for the class hierarchy), so maybe it's worth proposing for json-api team. But right now I can do -- to move it farther, if everyone agrees with this convention.

BTW, not related to this one, but I discovered recently while playing with ember, that by default it generates attribute names dasherized, and not underscored for payload. Not sure if it's explicitly stated in standard, but the point is that when we use ams with ember the keys are transferred differently encoded for inbound and outbound packets. And the payload for server doesn't seem to be ready for rails because of that, so I got a thought — are there deserializers for AR? Could AMS solve this issue be providing some wrapper for strong parameters?

@beauby
Copy link
Contributor

beauby commented Oct 1, 2015

@youroff Let's comply with JSON API for now. If they change the spec, we can always change AMS then.

Considering deserializers, it's a WIP, it's kinda almost ready, but we need to discuss a few things before rebasing/pushing it.

@youroff youroff changed the title Remove namespace while generating type field for jsonapi Namespace separator fix for type field in jsonapi adapter Oct 1, 2015
@bf4
Copy link
Member

bf4 commented Oct 1, 2015

See linked issue, ember is 'wrong'

@youroff
Copy link
Contributor Author

youroff commented Oct 1, 2015

You mean that it doesn't convert /?

@bf4
Copy link
Member

bf4 commented Oct 2, 2015

You mean that it doesn't convert /?

No, that ember uses / which is invalid

@youroff
Copy link
Contributor Author

youroff commented Oct 2, 2015

Does ember support namespaced models or it uses it somehow else?

@youroff
Copy link
Contributor Author

youroff commented Oct 2, 2015

Btw, what if we add setting for separator? So that anyone will be able to change it to / or anything else for their setup.

@bf4
Copy link
Member

bf4 commented Oct 2, 2015

legit.. new global config

@bf4
Copy link
Member

bf4 commented Dec 28, 2015

Looks like we lost sight of this. Is a good PR. @youroff Would you like to rebase and we'll finish it up?

@youroff
Copy link
Contributor Author

youroff commented Jan 14, 2016

Sorry, missed that on holidays... Sure, will take a look at it soon. What's the final decision on how we deal with namespaces?
And btw, I remember talking about global settings for deriving type name from seralizer name instead of model name. What do you think about that one? Is it worth of implementation?

@bf4
Copy link
Member

bf4 commented Jan 15, 2016

type is really the resource type, so should be derived from the object, I think.

-- is fine until we think of something better. I've been meaning to make a PR to ember to fix their impl :)

@bf4 bf4 added the C: Feature label Jan 15, 2016
@youroff
Copy link
Contributor Author

youroff commented Jan 15, 2016

Well, in our app we use the same model in the different ways. For example, there is a User -> Membership -> Dealership relation. And Membership reflects into two different Ember models: one ProfileMembership — is a light version to be attachable to profile (and loadable with). So it's something used throughout the site to adjust current user's experience, and another Memebership is manageable by dealership Admin. It contains various additional fields, which are used on the backend. Currently I use type directive to set type explicitly inside of different serializers and having option to derive type name from the name of serializer would make the code a bit cleaner. So I believe it's not that unique a use case and having an option (with taking type from the model as a default) wouldn't hurt anyone, what do you think? And it's pretty easy to implement, I believe I've done some sketch of it already.

So for namespace, we'll have a setting for separator with '--' as default, right?

Btw, what's the way of using namespaces on the side of Ember? If you have some example in mind, could you point out?

@youroff
Copy link
Contributor Author

youroff commented Jan 15, 2016

Here's more generic example... Let's say we have Post model. It may have title, intro, body, lots of metadata. Though in a news feed we only need title and intro. So that would be PostPreview on ember's end. And when user wants to read whole article it would load Post model. Sure we could implement fields parameter, but the approach seems cleaner. Also, not sure about now, but when ember was about version 1-something, it would take record from the store by default, so once loaded reduced record, it wouldn't reload it when called with findRecord.

@NullVoxPopuli
Copy link
Contributor

I think we need to wait for the JSON API spec to decide on what to do about namespaces, yeah?

@bf4
Copy link
Member

bf4 commented Mar 10, 2016

@NullVoxPopuli

we need to wait for the JSON API spec to decide on what to do about namespaces, yeah?

In 1.0 it is up to us. See linked issue I made re 'kebab' case in the json api repo

@bf4 bf4 reopened this Mar 10, 2016
@remear
Copy link
Member

remear commented Mar 17, 2016

Can we make a decision on this?

@youroff
Copy link
Contributor Author

youroff commented Mar 17, 2016

Actually, I would commit some time to fix that if we have a decission. Just ran into the need of namespace separator compatible with Ember. @bf4?

@remear
Copy link
Member

remear commented Mar 17, 2016

I'm late to this conversation, but I'm fine with either spam--unrelated_links or spam__unrelated_links.

@youroff
Copy link
Contributor Author

youroff commented Mar 17, 2016

I think all agree that we need some global setting for json-api adapter and the last question is what would be the default.

@youroff
Copy link
Contributor Author

youroff commented Mar 20, 2016

Just created a PR #1609 for this feature. Codebase changed a lot, so I just made it from scratch. @bf4 could you review?

@NullVoxPopuli
Copy link
Contributor

Closing in favor of #16009

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.

5 participants