-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Wrongly formated ids when using mongoid head #354
Comments
I literally just finished dealing with this so thought I'd respond :) This is actually a Mongoid / Moped thing and intentional, to match what Mongo does natively. You'll need to handle this yourself for the time being ( not sure if they are going to do anything different or not ). What I did was to just override .to_json and .as_json on Moped::BSON::ObjectId in a Rails initializer. module Moped
module BSON
class ObjectId
alias :to_json :to_s
alias :as_json :to_s
end
end
end This was also the suggestion offered by Durran. You can find more info here |
@brentkirby 👍 now I can clean up my code again. |
If I understand correctly that's not AMS issue and this can be closed. |
/cc @arthurnn |
@brentkirby is right. On mongoid the default JSON output for Object id is: module Moped
module BSON
class ObjectId
alias :to_json :to_s
alias :as_json :to_s
end
end
end Mongoid 4: module BSON
class ObjectId
alias :to_json :to_s
alias :as_json :to_s
end
end That should be enough to solve this issue. |
…xed_json` method Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object", which breaks Elasticsearch. See: * elastic/elasticsearch#3517 * rails-api/active_model_serializers#354 (comment)
…xed_json` method Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object", which breaks Elasticsearch. See: * elastic/elasticsearch#3517 * rails-api/active_model_serializers#354 (comment)
@arthurnn as_json and to_json can accept arguments. The code below worked better for me. module BSON
class ObjectId
def as_json(*args)
to_s
end
alias :to_json :as_json
end
end |
@mateusdelbianco thx, just had a little issue with your code, it produced a JSON like this for me which was invalid : {
"id":5123bf6c104729020000004f
...
} I had to add a module BSON
class ObjectId
def as_json(*args)
to_s.to_json
end
alias :to_json :as_json
end
end |
@mateusdelbianco @barodeur this is really helpful, thanks! Also, you can actually just override #as_json without the aliases: module BSON
class ObjectId
def as_json(*args)
to_s
end
end
end This will produce proper json output since #to_json jsonifyies whatever #as_json returns, so it will add the proper quotes to the string, and #as_json will return just a regular string as intended. |
What if I want the exact opposite? from: to: |
Just had an issue using elasticsearch mongoid gem (calling rake es:reindex). After Some Search I found this github issue solving my problem : nesquena/backburner#80 |
Configuration# Gemfile
gem 'rails', '~> 5.2.0'
gem 'mongoid', '~> 7.0.1'
gem 'bson_ext', '~> 1.5.1'
gem 'active_model_serializers', '~> 0.10.7' mongoid document serialized using
|
The solution @jstejada posted worked better for me: module BSON
class ObjectId
def as_json(*args)
to_s
end
end
end The other solutions crashed due to lack of *args or generated id strings containing special quote character at the begining and end of string, which led my REST consumer code to read id field as "\"123131b23b1b23b1b3\"" instead of the expected "123131b23b1b23b1b3" |
…xed_json` method Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object", which breaks Elasticsearch. See: * elastic/elasticsearch#3517 * rails-api/active_model_serializers#354 (comment)
…xed_json` method Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object", which breaks Elasticsearch. See: * elastic/elasticsearch#3517 * rails-api/active_model_serializers#354 (comment)
…xed_json` method Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object", which breaks Elasticsearch. See: * elastic/elasticsearch#3517 * rails-api/active_model_serializers#354 (comment)
…xed_json` method Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object", which breaks Elasticsearch. See: * elastic/elasticsearch#3517 * rails-api/active_model_serializers#354 (comment)
…xed_json` method Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object", which breaks Elasticsearch. See: * elastic/elasticsearch#3517 * rails-api/active_model_serializers#354 (comment)
…xed_json` method Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object", which breaks Elasticsearch. See: * elastic/elasticsearch#3517 * rails-api/active_model_serializers#354 (comment)
…xed_json` method Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object", which breaks Elasticsearch. See: * elastic/elasticsearch#3517 * rails-api/active_model_serializers#354 (comment)
…xed_json` method Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object", which breaks Elasticsearch. See: * elastic/elasticsearch#3517 * rails-api/active_model_serializers#354 (comment)
…xed_json` method Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object", which breaks Elasticsearch. See: * elastic/elasticsearch#3517 * rails-api/active_model_serializers#354 (comment)
…xed_json` method Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object", which breaks Elasticsearch. See: * elastic/elasticsearch#3517 * rails-api/active_model_serializers#354 (comment)
…xed_json` method Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object", which breaks Elasticsearch. See: * elastic/elasticsearch#3517 * rails-api/active_model_serializers#354 (comment)
I am upgrade my application to rails 4 and I have noticed that the id field is being formatted wrong. it is being formated like this
"_id": {
"$oid": "51c7bd98e77989d1f1000002"
}
I believe this stems from mongoid returning a json object now instead of simple a string .
The text was updated successfully, but these errors were encountered: