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

Query serializer support #10

Closed
MissiaL opened this issue Mar 13, 2020 · 12 comments
Closed

Query serializer support #10

MissiaL opened this issue Mar 13, 2020 · 12 comments
Assignees
Labels
enhancement New feature or request

Comments

@MissiaL
Copy link
Contributor

MissiaL commented Mar 13, 2020

In my current project i i find use query_serializer. Like this

class PublicCommentsRequest(Serializer):
    include = CharField(required=False)
    order_by = CharField(required=False)
    order_direction = ChoiceField(choices=['ASC', 'DESC'], required=False)
    start_datetime = DateTimeField(required=False)
    end_datetime = DateTimeField(required=False)
    unsafe = BooleanField(required=False)

swagger_auto_schema(
    query_serializer=PublicCommentsRequest,
    deprecated=True,
    tags=['comments'],
    operation_id='public:get_comments',
),

It looks like this
image

Could you add similar functionality?

I could use extra_parameters, but it looks complicated
Thanks

@tfranzel tfranzel added the enhancement New feature or request label Mar 14, 2020
@tfranzel
Copy link
Owner

that looks interesting. i need to refactor the parameter section. the old code did some funky stuff that needs changing. i'll try to incorporate this with it.

@tfranzel tfranzel self-assigned this Mar 14, 2020
@MissiaL
Copy link
Contributor Author

MissiaL commented Mar 22, 2020

Any updates? Maybe I can help you somehow?

@tfranzel
Copy link
Owner

sry too keep you waiting. i had to do some heavy structural changes. this will be the next feature. 😄
apart from that feature how far along are you on your project? is spectacular covering most of it already?

@MissiaL
Copy link
Contributor Author

MissiaL commented Mar 22, 2020

It seems that this is the last feature that remains to be implemented. Otherwise, it looks like it’s ready :-)

@tfranzel
Copy link
Owner

So OpenAPI3 has multiple ways achieving this. could you post an example query for that?

option 1: exploding the serializer into seperate fields, as if OpenApiParameter was used multiple times
option 2: An object with those fields as parameters. like a nested serializer packed into one query parameter

and option 2 has even more sub-options for formatting and serialization. i think this is one of the more complicated sections of the spec.

@MissiaL
Copy link
Contributor Author

MissiaL commented Mar 23, 2020

If I understand you correctly, then you need to give an example of how this should look?
It seems to me that the parameter should be able to accept two types of objects:
Serializer and OpenApiParameter

One example

class PublicCommentsRequest(Serializer):
    include = CharField(required=False)
    order_by = CharField(required=False)
    order_direction = ChoiceField(choices=['ASC', 'DESC'], required=False)
    start_datetime = DateTimeField(required=False)
    end_datetime = DateTimeField(required=False)

@extend_schema(
    parameters=[PublicCommentsRequest,OpenApiParameter('expiration_date', OpenApiTypes.DATETIME, description='time the object will expire at')],
    responses={201: None},
 )

@tfranzel
Copy link
Owner

unfortunately, there is more info required to build the schema here. i suppose you refer to option1. i was asking about how the actual query will look like, that is how this is supposed to look in the query section of the HTTP request.

https://swagger.io/docs/specification/serialization/#query

Object id = {"role": "admin", "firstName": "Alex"}

  • form/explode=True:/users?role=admin&firstName=Alex (option 1)
  • form/explode=False:/users?id=role,admin,firstName,Alex (option 2 variation 1)
  • deepObject: /users?id[role]=admin&id[firstName]=Alex (option 2 variation 2)

option2 has even more variations.

@MissiaL
Copy link
Contributor Author

MissiaL commented Mar 23, 2020

Wow, I didn't think there were so many different options!
I think we could try to make the simplest basic option 1

Perhaps then it will be possible to expand it somehow. It’s better to make a simple option, and then gradually complicate it, if necessary

@tsouvarev
Copy link
Contributor

I think that option 1 is the most used by far in REST APIs

@tfranzel
Copy link
Owner

@tsouvarev yes i think so too.
@MissiaL yes option 1 looks good. i already prepared something. i need to streamline it though. i prob will have something for you by tomorrow.

tfranzel added a commit that referenced this issue Mar 24, 2020
@tfranzel
Copy link
Owner

@MissiaL: i had a hard look and decided to do it as you suggested.

parameters=[...,serializer] for query exploding (as it is used most offen)

parameters=[...,OpenApiParamter(serializer)], intended for nesting (but style and explode are not exposed yet. ) swagger explodes it automatically in that case also because of the defaults. so atm they result in the "same" in swagger-ui (even though the generated spec is slightly different)

i think that should do it for now.

@MissiaL
Copy link
Contributor Author

MissiaL commented Mar 24, 2020

Thanks! It works great!
I have a couple more improvements that I will add now.

#12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants