Uploaded image for project: 'Struts 2'
  1. Struts 2
  2. WW-4650

Json deserialization does not work in 2.5.1

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.5.1
    • Fix Version/s: 2.5.2
    • Component/s: Plugin - JSON
    • Labels:
      None

      Description

      A json posted from Angular request is not deserialized anymore. It worked in 2.5.

        Issue Links

          Activity

          Hide
          hudson Hudson added a comment -

          SUCCESS: Integrated in Struts-JDK7-master #487 (See https://builds.apache.org/job/Struts-JDK7-master/487/)
          WW-4650 Json deserialization does not work in 2.5.1 (victorsosa: rev 6e07a63797422994a96059188b1753597a22ffd1)

          • plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java
          • plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java
          • .gitignore
          Show
          hudson Hudson added a comment - SUCCESS: Integrated in Struts-JDK7-master #487 (See https://builds.apache.org/job/Struts-JDK7-master/487/ ) WW-4650 Json deserialization does not work in 2.5.1 (victorsosa: rev 6e07a63797422994a96059188b1753597a22ffd1) plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java .gitignore
          Hide
          lukaszlenart Lukasz Lenart added a comment -

          PR merged, thanks!

          Show
          lukaszlenart Lukasz Lenart added a comment - PR merged, thanks!
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user asfgit closed the pull request at:

          https://github.com/apache/struts/pull/103

          Show
          githubbot ASF GitHub Bot added a comment - Github user asfgit closed the pull request at: https://github.com/apache/struts/pull/103
          Hide
          jira-bot ASF subversion and git services added a comment -

          Commit 6e07a63797422994a96059188b1753597a22ffd1 in struts's branch refs/heads/master from victorsosa
          [ https://git-wip-us.apache.org/repos/asf?p=struts.git;h=6e07a63 ]

          WW-4650 Json deserialization does not work in 2.5.1

          logic need to be changed as accept can be a list; the check need to be
          done in the list itself.

          Accept:application/json, text/plain, /

          Show
          jira-bot ASF subversion and git services added a comment - Commit 6e07a63797422994a96059188b1753597a22ffd1 in struts's branch refs/heads/master from victorsosa [ https://git-wip-us.apache.org/repos/asf?p=struts.git;h=6e07a63 ] WW-4650 Json deserialization does not work in 2.5.1 logic need to be changed as accept can be a list; the check need to be done in the list itself. Accept:application/json, text/plain, /
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user lukaszlenart commented on the issue:

          https://github.com/apache/struts/pull/103

          This looks good!

          Show
          githubbot ASF GitHub Bot added a comment - Github user lukaszlenart commented on the issue: https://github.com/apache/struts/pull/103 This looks good!
          Hide
          fredgero Fred Ge added a comment -

          When setting the json interceptor only on the Action/method with json in request, the accept parameter is initialized with "application/json, text/plain, /" so the json object is not deserialized, I had to override accept with "application/json" like Lukasz said, and it worked. The patch from Victor Sosa should work.

          The counterpart is that I have to put json interceptor only on specific action/method as ContentType is no longer used.

          Thanks for your support

          Show
          fredgero Fred Ge added a comment - When setting the json interceptor only on the Action/method with json in request, the accept parameter is initialized with "application/json, text/plain, / " so the json object is not deserialized, I had to override accept with "application/json" like Lukasz said, and it worked. The patch from Victor Sosa should work. The counterpart is that I have to put json interceptor only on specific action/method as ContentType is no longer used. Thanks for your support
          Hide
          fredgero Fred Ge added a comment - - edited

          I forced header in $post with application/json but it is overriden in interceptor by :
          "text/html,application/xhtml+xml, application/xml;q=0.9, image/webp, /;q=0.8"
          Didn't find why.

          victorsosa : i don't have json response as posting json object failed.

          I cannot override the accept in the interceptor as the interceptor is in a common interceptors stack and not all requests are in json format.
          Struts.xml use wildcards for actions/methods, I'll try to specify json interceptor only on the concerned action/method .

          Show
          fredgero Fred Ge added a comment - - edited I forced header in $post with application/json but it is overriden in interceptor by : "text/html,application/xhtml+xml, application/xml;q=0.9, image/webp, / ;q=0.8" Didn't find why. victorsosa : i don't have json response as posting json object failed. I cannot override the accept in the interceptor as the interceptor is in a common interceptors stack and not all requests are in json format. Struts.xml use wildcards for actions/methods, I'll try to specify json interceptor only on the concerned action/method .
          Hide
          githubbot ASF GitHub Bot added a comment -

          GitHub user victorsosa opened a pull request:

          https://github.com/apache/struts/pull/103

          WW-4650 Json deserialization does not work in 2.5.1

          Fix for WW-4650 broked by WW-4558 and test cases changes too.

          You can merge this pull request into a Git repository by running:

          $ git pull https://github.com/victorsosa/struts WW-4650

          Alternatively you can review and apply these changes as the patch at:

          https://github.com/apache/struts/pull/103.patch

          To close this pull request, make a commit to your master/trunk branch
          with (at least) the following in the commit message:

          This closes #103


          commit 7985fd191a39921817316f0f71348ffdd8233a4a
          Author: victor sosa <victorsosa@users.noreply.github.com>
          Date: 2016-05-26T23:23:37Z

          Merge pull request #17 from apache/master

          update pull

          commit 4f07433626dc5bef4e626c00cf880e848ac70f80
          Author: victorsosa <victor.sosa@peopleware.do>
          Date: 2016-06-24T12:25:42Z

          Merge branch 'master' of https://github.com/apache/struts into apache-master

          commit b4622865893731277ac0034d54d26216f09b7054
          Author: victorsosa <victor.sosa@peopleware.do>
          Date: 2016-06-24T12:26:27Z

          Merge branch 'apache-master'

          commit 6e07a63797422994a96059188b1753597a22ffd1
          Author: victorsosa <victor.sosa@peopleware.do>
          Date: 2016-06-24T13:06:52Z

          WW-4650 Json deserialization does not work in 2.5.1

          logic need to be changed as accept can be a list; the check need to be
          done in the list itself.

          Accept:application/json, text/plain, /


          Show
          githubbot ASF GitHub Bot added a comment - GitHub user victorsosa opened a pull request: https://github.com/apache/struts/pull/103 WW-4650 Json deserialization does not work in 2.5.1 Fix for WW-4650 broked by WW-4558 and test cases changes too. You can merge this pull request into a Git repository by running: $ git pull https://github.com/victorsosa/struts WW-4650 Alternatively you can review and apply these changes as the patch at: https://github.com/apache/struts/pull/103.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #103 commit 7985fd191a39921817316f0f71348ffdd8233a4a Author: victor sosa <victorsosa@users.noreply.github.com> Date: 2016-05-26T23:23:37Z Merge pull request #17 from apache/master update pull commit 4f07433626dc5bef4e626c00cf880e848ac70f80 Author: victorsosa <victor.sosa@peopleware.do> Date: 2016-06-24T12:25:42Z Merge branch 'master' of https://github.com/apache/struts into apache-master commit b4622865893731277ac0034d54d26216f09b7054 Author: victorsosa <victor.sosa@peopleware.do> Date: 2016-06-24T12:26:27Z Merge branch 'apache-master' commit 6e07a63797422994a96059188b1753597a22ffd1 Author: victorsosa <victor.sosa@peopleware.do> Date: 2016-06-24T13:06:52Z WW-4650 Json deserialization does not work in 2.5.1 logic need to be changed as accept can be a list; the check need to be done in the list itself. Accept:application/json, text/plain, /
          Hide
          victorsosa victorsosa added a comment - - edited

          Fred Ge can you please post the json reponse that you get from the server, just to be clear about the struts error response?

          Show
          victorsosa victorsosa added a comment - - edited Fred Ge can you please post the json reponse that you get from the server, just to be clear about the struts error response?
          Hide
          victorsosa victorsosa added a comment - - edited

          Lukasz Lenart As you said the that logic need to be changed as accept can be a list the check need to be done in the list itself.

          Accept:application/json, text/plain, */*
          
          Show
          victorsosa victorsosa added a comment - - edited Lukasz Lenart As you said the that logic need to be changed as accept can be a list the check need to be done in the list itself. Accept:application/json, text/plain, */*
          Hide
          lukaszlenart Lukasz Lenart added a comment -

          You can always override the accept param in the interceptor

                <interceptor-ref name="json">
                  <param name="accept">application/json</param>
                </interceptor-ref>
          
          Show
          lukaszlenart Lukasz Lenart added a comment - You can always override the accept param in the interceptor <interceptor-ref name= "json" > <param name= "accept" > application/json </param> </interceptor-ref>
          Hide
          lukaszlenart Lukasz Lenart added a comment - - edited

          The case is that the Accept:application/json, text/plain, / sets accept to value application/json, text/plain, / and this fails on if ((accept != null) && accept.equalsIgnoreCase("application/json")). So I'm wondering it it makes sense to change this if to startWith?

          Show
          lukaszlenart Lukasz Lenart added a comment - - edited The case is that the Accept:application/json, text/plain, / sets accept to value application/json, text/plain, / and this fails on if ((accept != null) && accept.equalsIgnoreCase("application/json")) . So I'm wondering it it makes sense to change this if to startWith ?
          Hide
          fredgero Fred Ge added a comment -

          Yes it is related.
          I put breakpoint on JsonInterceptor 2.5.1, and here is what i get:

          • The request header by AngularJS is well set to "Accept:application/json".
          • But when getting in JsonInterceptor, accept parameter is already initialized with "text/html,application/xhtml+xml..." (line 60), so it is not initialized by request header (line 69) and the json object is not deserialized.
          Show
          fredgero Fred Ge added a comment - Yes it is related. I put breakpoint on JsonInterceptor 2.5.1, and here is what i get: The request header by AngularJS is well set to "Accept:application/json". But when getting in JsonInterceptor, accept parameter is already initialized with "text/html,application/xhtml+xml..." (line 60), so it is not initialized by request header (line 69) and the json object is not deserialized.
          Hide
          lukaszlenart Lukasz Lenart added a comment -

          Maybe it is related to WW-4558 - can you setup logging to debug and see what's going on?

          Show
          lukaszlenart Lukasz Lenart added a comment - Maybe it is related to WW-4558 - can you setup logging to debug and see what's going on?
          Hide
          fredgero Fred Ge added a comment -

          Hi,

          Here are some details I can provide:

          • nothing has changed in the webapp except struts version : 2.5 > 2.5.1
          • it worked too with previous version < 2.5

          struts.xml

          <package name="shared_package" extends="struts-default,json-default"  strict-method-invocation="false">
          	...
          	<interceptor-stack name="commonStack">
          		<interceptor-ref name="json">
          			<param name="enableSMD">false</param>
          		</interceptor-ref>
          	...
          </package>
          <package name="main" namespace="/" extends="shared_package"  strict-method-invocation="false">
          	
          </package>
          

          JS : AngularJS (1.5) request:

          $http({method: 'POST',
          	url: '/MyAction_method',
          	data : myJsObject,
          	}).success(function(data, status, headers, config) {
          		// ...
          	});
          

          Request headers:

          Accept:application/json, text/plain, */*
          Accept-Encoding:gzip, deflate
          Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
          Cache-Control:no-cache
          Connection:keep-alive
          Content-Length:949
          Content-Type:application/json;charset=UTF-8
          

          In the action the object corresponding to myJsObject is not deserialized, so it is null.
          No exception in logs, except the one due to NullPointerException.

          Show
          fredgero Fred Ge added a comment - Hi, Here are some details I can provide: nothing has changed in the webapp except struts version : 2.5 > 2.5.1 it worked too with previous version < 2.5 struts.xml <package name="shared_package" extends="struts-default,json-default" strict-method-invocation="false"> ... <interceptor-stack name="commonStack"> <interceptor-ref name="json"> <param name="enableSMD">false</param> </interceptor-ref> ... </package> <package name="main" namespace="/" extends="shared_package" strict-method-invocation="false"> </package> JS : AngularJS (1.5) request: $http({method: 'POST', url: '/MyAction_method', data : myJsObject, }).success(function(data, status, headers, config) { // ... }); Request headers: Accept:application/json, text/plain, */* Accept-Encoding:gzip, deflate Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4 Cache-Control:no-cache Connection:keep-alive Content-Length:949 Content-Type:application/json;charset=UTF-8 In the action the object corresponding to myJsObject is not deserialized, so it is null. No exception in logs, except the one due to NullPointerException.
          Hide
          lukaszlenart Lukasz Lenart added a comment -

          Any more details? Action name? Method? Maybe it's similar to WW-4649?

          Show
          lukaszlenart Lukasz Lenart added a comment - Any more details? Action name? Method? Maybe it's similar to WW-4649 ?

            People

            • Assignee:
              Unassigned
              Reporter:
              fredgero Fred Ge
            • Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development