Page 1 of 1

Custom route not returning all fields

PostPosted: Mon Sep 20, 2021 6:31 pm
by DannyC
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"
  },

Re: Custom route not returning all fields

PostPosted: Mon Sep 20, 2021 7:40 pm
by SBarnes
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.

Re: Custom route not returning all fields  Topic is solved

PostPosted: Tue Sep 21, 2021 11:00 am
by Mike.Sheen
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!

Re: Custom route not returning all fields

PostPosted: Tue Sep 21, 2021 2:00 pm
by DannyC
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.

Re: Custom route not returning all fields

PostPosted: Tue Sep 21, 2021 2:31 pm
by Mike.Sheen
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?