After update 2.9.12, response.json is messing the screen

47 views
Skip to first unread message

André Kablu

unread,
Jan 21, 2015, 2:04:31 PM1/21/15
to web...@googlegroups.com
Hi all,

I just updated one of my projects to 2.9.12 and after that all views that have a code like this does not work:

SCRIPT('somecode(' + response.json + ')')

the response.json is changing the content-type of the page to json...

if we use:
  return response.json

there will be no problem,

but when we use response.json as part of another string, it will transform content-type to json.... messing the view...

Think we need to fix that...

Thanks!

André Kablu

unread,
Jan 21, 2015, 2:23:11 PM1/21/15
to web...@googlegroups.com
Here is the reason why:

Before 2.9.12 it was (globals.py):
def json(self, data, default=None):
 
return json(data, default=default or custom_json)


Now it is:
def json(self, data, default=None):
 
if 'Content-Type' not in self.headers:
 
self.headers['Content-Type'] = 'application/json'
 
return json(data, default=default or custom_json)

Niphlod

unread,
Jan 21, 2015, 3:05:58 PM1/21/15
to web...@googlegroups.com
pretty undocumented feature (so its not a bug per se)....

response.json() is clearly NOT meant to be used in views.... just use json(whatever) instead.

on the "can I call it a bug of not" matter, that seems to surface every once in a while, please take the following as a word of caution (from the initial "heavy pusher" of Continous Integration in web2py)... the (hard ?) truth is that for a project the size of web2py, for all intents and purposes, as long as something is NOT included in the automated tests, you can't call a behavioural change as a bug (or a regression).

If something changed and has been introduced in the core code it's because there WASN'T a test for it (or documentation about it in the book) at that time and someone proposed a valid usecase for it (that MAY NOT coincide with the usecase you're used to).
In other words, given that:
- the change seemed legit
- nobody cared to add a test for it
- there was no test case for it
- it raised no alarms

it went merged as a proper fix.

Consider also that if you cared to test your app with trunk, you'd have noticed the issue starting from the 17-dec, and could have raised an issue before the stable release.

Consider also that if you patch your controller with

response.headers['Content-Type'] = 'text/html'

before returning, also with the current stable version your issue will be resolved.

Kablu®

unread,
Jan 21, 2015, 3:36:09 PM1/21/15
to web2py
I was expecting someone tell me that I am using it wrong :)

Since it was there and I was using it (wrong or not) for past 2 years... I was not expecting its behavior to change...

And I always consider testing my apps with trunk... bug I was focusing tests on DAL... sometimes we don`t have much time...

but ok no problem I will change my code...

I just think it must be mention in the changelog that its behavior changed... b/c others can have the same problem...


--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/l5CLfJ7cUHQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Niphlod

unread,
Jan 21, 2015, 3:57:37 PM1/21/15
to


On Wednesday, January 21, 2015 at 9:36:09 PM UTC+1, André Kablu wrote:
I was expecting someone tell me that I am using it wrong :)

Since it was there and I was using it (wrong or not) for past 2 years... I was not expecting its behavior to change...

And I always consider testing my apps with trunk... bug I was focusing tests on DAL... sometimes we don`t have much time...

but ok no problem I will change my code...

I just think it must be mention in the changelog that its behavior changed... b/c others can have the same problem...


Unfortunately it pretty much goes in the same way, being it a feature 1-month old or 6-years.
We - maintainers - can't foresee the usage in your app of undocumented features and can't mention every commit in a CHANGELOG that  is not a bugfix, because from "our side", nobody raised an hand for it.
We encourage everyone to contribute with tests because our life would be far easier: we'd propose a change being sure that nothing breaks.
This specific case is even worse, because the change came from a bug and it got treated as one: no way we could have even considered to put it in the CHANGELOG.
So, I humble bow before you and this "breakage" but - unfortunately - that's not going to save you, or anybody else in your situation, from being stung a taddle bit after an upgrade.
Reply all
Reply to author
Forward
0 new messages