Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON:API #41

Closed
HrachMD opened this issue Nov 27, 2020 · 9 comments
Closed

JSON:API #41

HrachMD opened this issue Nov 27, 2020 · 9 comments

Comments

@HrachMD
Copy link

HrachMD commented Nov 27, 2020

This one also not issue, but a proposal.
It will be very useful for response schema validation in jsonapi standards.

@vitalik
Copy link
Owner

vitalik commented Nov 27, 2020

hi @HrachMD

Not sure I understand... do you have any references/links to this schema validation ?

@HrachMD
Copy link
Author

HrachMD commented Nov 27, 2020

Thank you for your response. I mean if we have such response schema:

class SigninOutput(Schema):

email: EmailStr
name: str

Then do not return just {'email': '', 'name': ''}
But instead do this kind transformation:
{'data': {'type': 'sometype', attributes: {'email': '', 'name': ''}}}

@hbutau
Copy link
Contributor

hbutau commented Jan 3, 2021

Maybe we need something like this 👉 https://github.com/django-json-api/django-rest-framework-json-api

@skorokithakis
Copy link

We're having this issue as well, and currently the responses are a bit awkward because JSONAPI uses an "envelope" that the actual data gets wrapped in, so Ninja only ever sees the "envelope" and the resulting names are "wrong" (it basically thinks that every API returns a class of type Envelope).

Maybe it would be nice to have something that takes the proper classes and wraps them in the envelope.

@vitalik
Copy link
Owner

vitalik commented Oct 14, 2021

maybe what you need is custom renderer ?
https://django-ninja.rest-framework.com/tutorial/response-renderers/

Note: Django Ninja is designed on top of OpenAPI specifcation/standard

while JSON:API is an alternative standard

technically with custom renderer you can achieve responses like you describe, but autogenerated openapi docs and schema will be useless

@skorokithakis
Copy link

Ah, that makes sense, thank you.

@skorokithakis
Copy link

Does OpenAPI mandate the response schema that APIs should return? What is the recommended way of returning e.g. relationships in Ninja? Currently, if we need to return, for example, a User object and a Subscription object, we'll need to have a UserSubscriptionResponse class that contains them, and that can quickly become unwieldy with larger APIs.

@stephenrauch
Copy link
Contributor

@skorokithakis You can use generics to help cleanup the code with embedded pydantic models. Here is an example for JSONAPI:

https://github.com/DeanWay/pydantic-jsonapi

@skorokithakis
Copy link

Hm, that's an interesting solution, thanks @stephenrauch. I don't think our juniors are going to love it, as generics are a rather advanced type concept, but it seems to solve the problem well, thanks.

@vitalik vitalik closed this as completed Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants