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

Do not expose internal state in the public encoder API (i.e. as_json) #12785

Merged
merged 2 commits into from Nov 7, 2013

Conversation

@chancancode
Copy link
Member

@chancancode chancancode commented Nov 6, 2013

See 1 for why this is not a good idea.

As part of this refactor, circular reference protection in as_json has
been removed and the corresponding error class has been deprecated.

As discussed with @jeremy, circular reference error is considered
programmer errors and protecting against it is out of scope for
the encoder.

This is again based on the excellent work by @sergiocampama in #11728.

@jeremy
jeremy reviewed Nov 6, 2013
View changes
activesupport/lib/active_support/core_ext/object/json.rb Outdated
# use encoder as a proxy to call as_json on all elements, to protect from circular references
encoder = options && options[:encoder] || ActiveSupport::JSON::Encoding::Encoder.new(options)
map { |v| encoder.as_json(v, options) }
map{ |v| v.as_json(options && options.dup) }

This comment has been minimized.

@jeremy

jeremy Nov 6, 2013
Member

map{ -> map {

@ghost ghost assigned jeremy Nov 6, 2013
@jeremy
jeremy reviewed Nov 7, 2013
View changes
activesupport/lib/active_support/core_ext/object/json.rb Outdated
end

def encode_json(encoder) #:nodoc:
# we assume here that the encoder has already run as_json on self and the elements, so we run encode_json directly
"[#{map { |v| v.encode_json(encoder) } * ','}]"
"[#{map { |v| v.as_json.encode_json(encoder) } * ','}]"

This comment has been minimized.

@jeremy

jeremy Nov 7, 2013
Member

Indentation

This comment has been minimized.

@chancancode

chancancode Nov 7, 2013
Author Member

Oops, fixed

@jeremy
jeremy reviewed Nov 7, 2013
View changes
activesupport/lib/active_support/json/encoding.rb Outdated
# Deprecate CircularReferenceError
def const_missing(name)
if name == :CircularReferenceError
message = "The JSON encoder in Rails 4.1 no longer offer protection from circular references. " \

This comment has been minimized.

@jeremy

jeremy Nov 7, 2013
Member

"no longer offers"

This comment has been minimized.

@chancancode

chancancode Nov 7, 2013
Author Member

fixed

"For example, the following Hash contains a circular reference to itself:\n" \
" h = {}\n" \
" h['circular'] = h\n" \
"In this case, calling h.to_json would not work properly."

This comment has been minimized.

@jeremy

jeremy Nov 7, 2013
Member

ActiveSupport::JSON.encode

chancancode added 2 commits Nov 6, 2013
See [1] for why this is not a good idea.

As part of this refactor, circular reference protection in as_json has
been removed and the corresponding error class has been deprecated.

As discussed with @jeremy, circular reference error is considered
programmer errors and protecting against it is out of scope for
the encoder.

This is again based on the excellent work by @sergiocampama in #11728.

[1]: intridea/multi_json#138 (comment)
jeremy added a commit that referenced this pull request Nov 7, 2013
Do not expose internal state in the public encoder API (i.e. as_json)
@jeremy jeremy merged commit 240863a into rails:master Nov 7, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.