function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jay JanarthananJay Janarthanan 

RestResource REST API returns different results for XML vs JSON

@RestResource(urlMapping='/api/restqa')
global with sharing class Jay_RestTest {

    global virtual class BaseDemo {
        public String className { set; get; }
    }

    global class Demo extends BaseDemo {
        public String Name { set; get; }
    }

    @HttpGet
    global static BaseDemo doGet() {
        Demo d = new Demo();
        d.className = 'Demo';
        d.Name = 'Jay';
        return d;
    }
}

In the above code if the Accept type is application/json I will get
 
{
  "className": "Demo",
  "Name": "Jay"
}

But if its application/xml then I get 
 
<?xml version="1.0" encoding="UTF-8"?>
<response xmlns:Demo="http://soap.sforce.com/schemas/class/Jay_RestTest">
    <Demo:Name>Jay</Demo:Name>
</response>

As you can see the value of the base class is missing 

Jay
 
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri
interesting and wierd for me :O
Amit Chaudhary 8Amit Chaudhary 8
Hi Jay,

Please do below changes in your Apex Class and try
@RestResource(urlMapping='/api/restqa')
global with sharing class Jay_RestTest 
{
    global class Demo 
    {
        public String Name { set; get; }
        public String className { set; get; }
    }

    @HttpGet
    global static Demo doGet() {
        Demo d = new Demo();
        d.className = 'Demo';
        d.Name = 'Jay';
        return d;
    }
}

Execute with below Contect Type
Content-Type: application/xml; charset=UTF-8
Accept: application/xml

Response will be :-
<?xml version="1.0" encoding="UTF-8"?> 
<response xmlns:Demo="http://soap.sforce.com/schemas/class/Jay_RestTest"> <Demo:className>Demo</Demo:className> 
<Demo:Name>Jay</Demo:Name> 
</response>
Execute with below Contect Type
Content-Type: application/json; charset=UTF-8
Accept: application/json


Response will be :-
{ "Name" : "Jay", 
"className" : "Demo" 
}

NOTE:- If you will not genrate the response then salesforce is using there own parser which is creating the wrapper of return type class only.
Please check below respone . Salesforce is also adding the class name
<Demo:className>Demo</Demo:className>
<Demo:Name>Jay</Demo:Name>


Please let us know if this will help you.

 
Jay JanarthananJay Janarthanan
The whole idea here is I was trying to show that if you have a base class - sub class situation the XML that is generated is wrong.