Best practice to send only required fields in REST API in Symfony

Multi tool use
Multi tool use


Best practice to send only required fields in REST API in Symfony



We need to send certificates list to another application using REST API. So Object response contains


[
{
"id":1,
"orderId":123,
"certificateStatus":true,
"certificateNo":"xyz123abc",
"customer":{
"id":36,
"email":"abc@cc.com",
"firstName":"abc",
"lastName":"dfg",
"user":{
"id":23,
"username":"abc@cc.com",
"enabled":true,
"kycStatus":false
},
"_links":{
"self":{
"href":"/app_dev.php/api/v1/customers/36"
}
}
},
"orderItem":{
"id":60,
"quantity":2,
"unitPrice":177581,
"total":355162,
"units":[
{
"id":1711,
"adjustments":[

],
"adjustmentsTotal":0
},
{
"id":1712,
"adjustments":[

],
"adjustmentsTotal":0
}
],
"unitsTotal":355162,
"adjustments":[

],
"adjustmentsTotal":0,
"variant":{
"id":334,
"code":"pool-gold-1oz",
"optionValues":[

],
"position":0,
"translations":{
"en_US":{
"locale":"en_US",
"id":334
}
},
"version":2,
"tracked":false,
"channelPricings":{
"UK_WEB":{
"channelCode":"UK_WEB",
"price":177582
},
"US_WEB":{
"channelCode":"US_WEB",
"price":177581
}
},
"_links":{
"self":{
"href":"/app_dev.php/api/v1/products/pool-gold-1oz/variants/pool-gold-1oz"
}
}
},
"_links":{
"order":{
"href":"/app_dev.php/api/v1/orders/29"
},
"product":{
"href":"/app_dev.php/api/v1/products/pool-gold-1oz"
},
"variant":{
"href":"/app_dev.php/api/v1/products/pool-gold-1oz/variants/pool-gold-1oz"
}
}
}
}
]



I want JSON response something like below sample response
- which need extra custom fields
- status code and message
- extra fields
- remove unwanted fields


{
"code":"custom_code_xxx",
"message":"Successful",
"data":[
{
"custom_extra_fields1":"asd",
"custom_extra_fields2":"xyz",
"id":1,
"orderId":123,
"certificateStatus":true,
"certificateNo":"xyz123abc",
"customer":{
"id":36,
"email":"abc@xyz.com",
"firstName":"abc",
"lastName":"dfg",
"user":{
"id":23,
"username":"abc@xyz.com",
"enabled":true,
"kycStatus":false
}
},
"orderItem":{
"id":60,
"quantity":2,
"unitPrice":177581,
"total":355162,
"unitsTotal":355162
}
}
]
}



Any best practice we can use to simplify JSON response ? or we need to construct an array in the required format





any reason for -1?
– stefun
Jul 4 at 9:37




1 Answer
1



When you use something like e.g. JMS Serializer Bundle you can use



Virtual Properties for additional custom fields.



And Groups and/or Exclusion Policies to get rid of unwanted fields.



When using the Symfony Serializer you have at least the option of Groups to exclude some fields.



To add additional fields I'd either use simply an additional Getter in your Entity (no clean approach but helps) or work with custom normalizers and/or encoders.





According to my understanding expose will be applicable to a complete model. But in my case, I need to hide some fields in one api that might require to expose in another API
– stefun
Jul 4 at 11:11





@stefun - so did you check my other option: Groups? it's available for both common serializers - which one do you use?
– LBA
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Dyig08SST3zWLqLrX34HMBdxBFbD,6m7lBgKyX QHFjPkYtaKpuVvaU,Z
EBinI9I6NG FT2n7eMJ,aTebIS 9UsT Nt dK 7DFzGlBLQ2Uo5GjDw9vhQw1T

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications