Skip to content

Add parameters for top-level request body properties #29

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

Closed
wants to merge 2 commits into from

Conversation

sethmlarson
Copy link
Contributor

@sethmlarson sethmlarson commented Jan 27, 2021

Inspired by @ezimuel I'd like to add named body parameters where possible while preserving the ability to call body=... in the free-form body cases.

  • Adds names to bodies that are scalars or arrays and define x-codegen-request-body-name
  • Adds required parameters which map to the JSON body
  • Allows users to continue to use body if there is any optional parameters or if the body is completely freeform (like put_schema())
  • If there's a conflict between path and body parameters we give up and only allow free-form body for now
  • If there's a conflict between query and body parameters we favor body parameters to avoid putting too much in query string, although this case is rare.

Also while working on this found that WorkplaceSearch.indexDocuments spec likely should have x-codegen-request-body-name equal to documents to match AppSearch.indexDocuments parameter name.

# If there's a request body build a list of
# parameters from the top-level of keys in the
# request body in case that body is a dictionary.
body_params = []
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This construction gets a lot more complex with request body parameters but hopefully the comments make things easier to follow.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can write an example case?

for param in self.required_params:
if param.in_ == "body":
body_params.append(param)
if len(body_params) != 1 or not body_params[0].named_body:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's more than one body parameter we are called body

@@ -1,24 +0,0 @@
class {{ component.class_name }}({% if component.is_response %}JSONResponse{% else %}dict{% endif %}):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not doing anything with structured responses yet so deleting this for now. However structured responses can be directly added on to what we have without it being a breaking change (since structured responses will still be accessible like a dict/list)

def expand_refs(x, depth=0):
# Hack to prevent recursive expansion, we only
# really need top-most layers expanded.
if depth > 10:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some reference loops within requestBody schemas that needs resolving, for now this is an easy way to ensure we resolve all $ref instances as far as we need without writing a "better" ref resolver.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to prevent circular reference in $ref? I found this comment about circular ref in OpenAPI.

@sethmlarson sethmlarson requested a review from ezimuel January 27, 2021 21:09
@sethmlarson sethmlarson changed the title Use naming hints from OpenAPI spec for required or non-object body parameters Add parameters for top-level request body properties Feb 2, 2021
Copy link

@ezimuel ezimuel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only point that I raise is about the possible naming conflict between params, body and query. This is because you are passing everything in the constructor, right? Maybe you can define an alternative way, for instance leaving the body as is and providing a builder class to create the body using parameters.

@sethmlarson sethmlarson closed this Aug 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants