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

[Python] JSON request with body=None produces empty payload #9558

Closed
r4victor opened this issue Jul 10, 2019 · 2 comments
Closed

[Python] JSON request with body=None produces empty payload #9558

r4victor opened this issue Jul 10, 2019 · 2 comments
Assignees

Comments

@r4victor
Copy link

r4victor commented Jul 10, 2019

Description

When sending JSON request with body=None, payload is set to empty string, which is not a valid JSON and violates the definition of Content-Type header. Practically, it prevents requests parsing. The semantically correct payload would be '{}'.

This behaviour is implemented in rest.mustache:

request_body = None
if body is not None:
    request_body = json.dumps(body)
r = self.pool_manager.request(
    method, url,
    body=request_body,
    preload_content=_preload_content,
    timeout=timeout,
    headers=headers)
Swagger-codegen version

2.4.6

Swagger declaration file content or url
swagger: "2.0"
info:
  version: "1.0.0"
  title: "Swagger Petstore"
  
paths:
  /pet/{petId}:
    delete:
      operationId: "deletePet"
      parameters:
      - name: "petId"
        in: "path"
        description: "Pet id to delete"
        required: true
        type: "integer"
        format: "int64"
      responses:
        204:
          description: "Deleted"
Command line used for generation

docker run --rm -v ${PWD}:/local swaggerapi/swagger-codegen-cli generate -l python -i /local/spec.yaml -o /local/swagger_out

Steps to reproduce

Describe a resource with POST, PUT, PATCH, OPTIONS or DELETE method,
Content-Type: application/json and empty body.
Make a request, try to parse its body with JSON parsing library such as python's built-in json.
json.loads(body) raises the error since body is empty string.

Related issues/PRs

3020 - issue and corresponding PR which introduced such a behaviour.

Suggest a fix/enhancement

Replace

request_body = None
if body is not None:
    request_body = json.dumps(body)

With

request_body = '{}'
if body is not None:
    request_body = json.dumps(body)
@r4victor r4victor reopened this Jul 10, 2019
@r4victor r4victor changed the title [Python] JSON request with body=None produces 'None' payload [Python] JSON request with body=None produces empty payload Jul 10, 2019
@IgnatBeresnev
Copy link

Any update on this?

We've just come across this problem. Fixable with custom codegen settings, but it'd be nice to have it built-in

@HugoMario
Copy link
Contributor

fixed by #10098, thanks guys for help!

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

3 participants