[Bug][Python] Generated Python client does not properly serialize JSON query parameters
Created by: mr-m-1t
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
Have you tested with the latest master to confirm the issue still exists? -
Have you searched for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
Generated Python clients do not properly serialize JSON query parameters. Specifically, single quotes are used instead of double quotes.
Expected call:
/foo?params={"someInt":5,"someString":"foo"}
Actual call:
/foo?params={'someInt':+5,+'someString':+'foo'}
Full example here: https://github.com/mr-m-1t/openapi-generator-python-json-parameter-serialization
openapi-generator version
I don't know if this is a regression - this is my first time using OpenAPI and openapi-generator
Tested with the following versions:
v5.3.0
openapi-generator-cli-5.3.1-20211106.175742-14
openapi-generator-cli-5.4.0-20211031.172749-2
openapi-generator-cli-6.0.0-20211031.180508-23
OpenAPI declaration file content or url
openapi.yml - Click to expand!
openapi: 3.0.1
info:
title: test API
description: 'This is an example API to demonstrate that the code generated by openapi-generator improperly serializes JSON query parameters'
license:
name: GNU GPLv3
url: https://www.gnu.org/licenses/gpl-3.0.en.html
version: 3.0.0
servers:
- url: http://127.0.0.1:8000
tags:
- name: test
description: it's a test endpoint
paths:
/foo:
get:
tags:
- test
summary: Make a test call
operationId: testCall
parameters:
- name: params
in: query
description:
Additional parameters to pass to an endpoint.
This should be passed as JSON
# schema:
# type: string
content:
application/json:
schema:
$ref: '#/components/schemas/SomeParams'
responses:
200:
description: Success response code does not correlate to a successful call.
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
404:
description: Non-integer ID supplied
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
components:
schemas:
SomeParams:
type: object
properties:
someInt:
type: integer
format: int64
someString:
type: string
ErrorResponse:
type: object
properties:
success:
type: boolean
message:
type: string
Generation Details
Full example here: https://github.com/mr-m-1t/openapi-generator-python-json-parameter-serialization
Generate a Python client from the OpenAPI spec in this repo.
pip3 uninstall test-generated-api
java -jar openapi-generator-cli-5.3.0.jar generate -i openapi.yml -g python --package-name test_generated_api -o ./generated --additional-properties=packageVersion="0.0.1",projectName="test-generated-api"
Install the generated client
cd generated/
python3 setup.py install --user
Steps to reproduce
Full example here: https://github.com/mr-m-1t/openapi-generator-python-json-parameter-serialization
Start an HTTP server
python3 -m http.server
# OR
# python3 start_http_server.py
Make an API call
python3 make_http_call.py
Related issues/PRs
None that I saw
Suggest a fix
I am new to python, but it looks to me like the generated code could be using built-in methods repr()
or str()
to convert models to strings. If this is the case, the generated code could be modified to use json.dumps()
instead, which would give us valid JSON.
import json
foo = {"something":"red"}
print(foo)
# {'something': 'red'}
print(json.dumps(foo))
# {"something": "red"}