OpenAPITools / OpenAPITools/openapi-generator
[BUG] [Python-flask] generator creates invalid code for fields that are determined to be "numeric" type
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 (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
Using the python code sample generator config of ....
This bug exists in all python generators I think - i replicated it in python-fastapi because the problem is in the python generators and none seem to handle the necessary transformation that happens in other generators.
generatorName: python-flask
outputDir: samples/server/petstore/python-flask-with-fake-endpoints-models-for-testing-with-http-signature
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml
templateDir: modules/openapi-generator/src/main/resources/python-flask
Then the OAS fragment ...
format_test:
type: object
....
properties:
......
decimal:
type: string
format: number
Generates python format_test.py looking like this ...
THERE IS AN IMPORT ...
from openapi_server.models.decimal import Decimal
AS WELL AS
self.openapi_types = {
....
'decimal': Decimal,
This is a bug as there is no such local class openapi_server.models.decimal and instead this should be referring to some python native datatype I believe.
In anycase this doesn't work in python as far as I can see.
ModelUtils.java has
public static boolean isDecimalSchema(Schema schema) {
if (SchemaTypeUtil.STRING_TYPE.equals(schema.getType()) // type: string
&& "number".equals(schema.getFormat())) { // format: number
return true;
}
return false;
}
.. which detects the OAS fragment above.
DefaultCodegen.java has ...
else if (ModelUtils.isDecimalSchema(schema)) {
// special handle of type: string, format: number
return "decimal";
I'm not sure what happens next but I assume that do to an explicit type mapping of "decimal" into Python then the code generator assumes this is a local model class and we get bad code.
This seems like a pretty big issue for the python code gen - surely a field like "decimal: type: string format: number" must crop up the whole time in OAS?? How mature is this product?
openapi-generator version
Master as of this bug
OpenAPI declaration file content or url
Use the generator config above.
Generation Details
Use the generator cfg above then to to the outputDir and take a look at test_format.py
Try running the server and it fails.
Steps to reproduce
as above
Suggest a fix
Map this type to a sensible py type
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 with ModelUtils.java and DefaultCodegen.java, then generate the Python-Flask sample from the configuration and OpenAPI fragment in the issue. Inspect the generated test_format.py and its model imports and run the generated server to reproduce the failure. Done means the string/number field maps to a valid Python type and the generated server runs without importing a nonexistent local model.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- flask, java, python
- Domain
- backend-api-design, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100