Open ssandr1kka opened 1 year ago
You can use a a schema for each nested element, for example:
class NetworkField(Schema):
available: list[str]
class ChecksField(Schema):
address_line1_check: str = None
address_postal_code_check: str = None
cvc_check: str
class CardField(Schema):
brand: str
checks: ChecksField
class AddressField(Schema):
city: str = None
country: str = None
line1: str = None
line2: str = None
postal_code: str = None
state: str = None
class BillingDetailsField(Schema):
address: AddressField
email: str = None
name: str = None
phone: str = None
...
And so on.
NOTE: I don't know data types of your fields so made them str , but I just wanted to show the approach
Hope that helped!
@ssandr1kka
The current way I have defined response, it does not recognize nested dictionaries
Not sure what you mean - but it does go nested items of the dict (you just need to make sure that all nested data is json serializible)
Here is a simple test:
@api.get("/test", response={200: Dict})
def test(request):
return {
"data": [
{
"billing_details": {
"address": {
"city": None,
"country": None,
"line1": None,
"line2": None,
"postal_code": None,
"state": None,
},
"email": None,
"name": None,
"phone": None,
},
"card": {
"brand": "visa",
"checks": {"address_line1_check": None, "address_postal_code_check": None, "cvc_check": "pass"},
},
}
]
}
it returns a pretty correct JSON response:
Well yeah schemas might change from their end, that is why I have decided not to go that way. I am looking a way that will address their data changes automatically and convert it into a dict fully... If there no other ways I guess that is the only option
You can use a a schema for each nested element, for example:
class NetworkField(Schema): available: list[str] class ChecksField(Schema): address_line1_check: str = None address_postal_code_check: str = None cvc_check: str class CardField(Schema): brand: str checks: ChecksField class AddressField(Schema): city: str = None country: str = None line1: str = None line2: str = None postal_code: str = None state: str = None class BillingDetailsField(Schema): address: AddressField email: str = None name: str = None phone: str = None ...
And so on.
NOTE: I don't know data types of your fields so made them str , but I just wanted to show the approach
Hope that helped!
So right now I have
response={200: Dict, codes_4xx: DefaultMessageSchema}
I am working on stripe integration, thus I cannot use a defined Schema, I need to pass stripe data. Their dict is nested and quite a big one... It goes something like:The current way I have defined response, it does not recognize nested dictionaries. Meaning instead of "card" it will return 'card' / instead of list it will return string and etc... How can I make it work?