OpenAPITools / OpenAPITools/openapi-generator
[BUG] Python API replaces "double quotes" with 'single quotes' for API endpoints that return strings containing json
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator?
- [] 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
I noticed, that "double quotes" got replaced with 'single quotes' for an API method that returned a string containing json, which is highly problematic since in this case, it is no longer valid json.
I tried looking into this, and I think the issue is an unfortunate combination of content-type = "application/json", response_type="str", and the returned string being actually a valid json object!
In ApiClient.deserialize, the response text is deserialized into json - and because it is a valid json string, this returns now a dictionary!
Now, in ApiClient.__deserialize_primitve, the dictionary is mapped back as string, via str(data).
However, since data is a Python dictionary now, string keys and values are now stored with SINGLE quotes, rendering the json string invalid.
Expected Output (this is what we actually get in the initial response text):
{
"message" : "Success",
"url" : "https://...",
"status" : 200
}
Actual Output (this is what the generated Python API Method finally returns):
{'message': 'Success', 'url': 'https://...', 'status': 200}
openapi-generator version
7.21.0
Using the Python generator.
OpenAPI declaration file content or url
Generation Details
Steps to reproduce
Sorry, got no time right now for a reproducible example :(
Related issues/PRs
Suggest a fix
Not sure, what would be a good fix for this. Maybe, if response_type is a string, don't call json.loads here?
class ApiClient:
def deserialize(self, response_text: str, response_type: str, content_type: Optional[str]):
....
elif re.match(r'^application/(json|[\w!#$&.+\-^_]+\+json)\s*(;|$)', content_type, re.IGNORECASE):
if response_text == "":
data = ""
else:
data = json.loads(response_text)
However, this might cause other unwanted side effects...
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading ApiClient.deserialize and __deserialize_primitve, focusing on the interaction between content-type application/json and response_type str. Reproduce the reported case when possible using a JSON response string, then verify that the generated Python API returns the expected valid JSON string and add regression coverage for the behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 38/100