Custom route not returning all fields  Topic is solved

Discussions relating to the REST API of Jiwa 7.

Custom route not returning all fields

Postby DannyC » Mon Sep 20, 2021 6:31 pm

Based on this thread, viewtopic.php?f=32&t=1493I have developed my own view & plugin from the plugin Mike supplied. My version attached.

It's not returning all the fields from the view even though they are all specified in the plugin. I am wanting to get all the P1 to P10 prices - some of them might be 0 but I still want it returned as 0. I also want Picture (which might be null), LastSavedDateTime and a few other fields also.
This is what is getting returned via Swagger UI.


Code: Select all
  {
    "InventoryID": "cfce627bfbc048a3ba72",
    "PartNo": "00-2312",
    "Description": "BALL BEARING FOR",
    "ForwardPriceDate": "2031-07-01T14:41:49.1030000",
    "CurrentPriceDate": "2021-07-01T14:41:49.1030000",
    "P1": 10.32,
    "P2": 9.38,
    "P3": 8.92,
    "P4": 8.45,
    "P5": 7.98,
    "PhysicalItem": true,
    "Brand": "Default - No Category",
    "Category": "Default - No Category",
    "Range": "Default - No Category",
    "TBA": "Default - No Category",
    "Packaging": "Default - No Category"
  },
Attachments
Plugin REST API Custom Routes Example.xml
(38.15 KiB) Downloaded 42 times
User avatar
DannyC
Senpai
Senpai
 
Posts: 636
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 30

Re: Custom route not returning all fields

Postby SBarnes » Mon Sep 20, 2021 7:40 pm

This has been mentioned before see viewtopic.php?f=32&t=1273&p=5276&hilit=json#p5276 as well, I am pretty sure the other issue I found were the types also need to be nullable types, at least if memory serves correctly that was the case with Booleans when I needed to force it to show the property as false.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: Custom route not returning all fields  Topic is solved

Postby Mike.Sheen » Tue Sep 21, 2021 11:00 am

There is nothing really wrong with not getting the field in the json if it's the default value.

That's because you'd expect the receiver to deserialise that json into class with properties matching the json, and if the field is not in the json then the class is left with the default value after deserialisation.

If you are depending on all fields being there - default or not - then you can force that with the JsConfig.ExcludeDefaultValues = false; configuration - in the REST API plugin you'll see where we set this to true:

Code: Select all
ServiceStack.Text.JsConfig.ExcludeDefaultValues = true;


In your own plugin you can override that to false and it should do what you want - but I think if you are depending on these values which are 0 or null being present then you might be doing something wrong!
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: Custom route not returning all fields

Postby DannyC » Tue Sep 21, 2021 2:00 pm

That fixed it.
I tried that line in various areas of my plugin but the only way I could get it to work was to edit the main REST API plugin. I know I shouldn't but I couldn't get it to work otherwise.
User avatar
DannyC
Senpai
Senpai
 
Posts: 636
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 30

Re: Custom route not returning all fields

Postby Mike.Sheen » Tue Sep 21, 2021 2:31 pm

DannyC wrote:That fixed it.
I tried that line in various areas of my plugin but the only way I could get it to work was to edit the main REST API plugin. I know I shouldn't but I couldn't get it to work otherwise.


Can I ask why you need the null or zero (default) values to be included in the json serialisation?
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756


Return to REST API

Who is online

Users browsing this forum: No registered users and 1 guest

cron