Comments (7)
Can you please give an example schema and payload that is failing? Do you mean json schema number type with float as the format? With or without formatting?
from openapi-json-schema-generator.
Hmm I see that ingesting 0 is failing for format float and double ingestion.
>>> schemas.Float32Schema.validate(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/justinblack/programming/openapi-json-schema-generator/samples/client/petstore/python/venv/lib/python3.8/site-packages/petstore_api/schemas/schemas.py", line 102, in validate
return super().validate_base(arg, configuration=configuration)
File "/Users/justinblack/programming/openapi-json-schema-generator/samples/client/petstore/python/venv/lib/python3.8/site-packages/petstore_api/schemas/schema.py", line 556, in validate_base
path_to_schemas = cls.__get_path_to_schemas(cast_arg, validation_metadata, path_to_type)
File "/Users/justinblack/programming/openapi-json-schema-generator/samples/client/petstore/python/venv/lib/python3.8/site-packages/petstore_api/schemas/schema.py", line 311, in __get_path_to_schemas
other_path_to_schemas = cls._validate(arg, validation_metadata=validation_metadata)
File "/Users/justinblack/programming/openapi-json-schema-generator/samples/client/petstore/python/venv/lib/python3.8/site-packages/petstore_api/schemas/validation.py", line 139, in _validate
other_path_to_schemas = validator(
File "/Users/justinblack/programming/openapi-json-schema-generator/samples/client/petstore/python/venv/lib/python3.8/site-packages/petstore_api/schemas/validation.py", line 288, in validate_types
raise __get_type_error(
petstore_api.exceptions.ApiTypeError: Invalid type. Required value type is float and passed type was int at ['args[0]']
and
>>> schemas.Float64Schema.validate(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/justinblack/programming/openapi-json-schema-generator/samples/client/petstore/python/venv/lib/python3.8/site-packages/petstore_api/schemas/schemas.py", line 102, in validate
return super().validate_base(arg, configuration=configuration)
File "/Users/justinblack/programming/openapi-json-schema-generator/samples/client/petstore/python/venv/lib/python3.8/site-packages/petstore_api/schemas/schema.py", line 556, in validate_base
path_to_schemas = cls.__get_path_to_schemas(cast_arg, validation_metadata, path_to_type)
File "/Users/justinblack/programming/openapi-json-schema-generator/samples/client/petstore/python/venv/lib/python3.8/site-packages/petstore_api/schemas/schema.py", line 311, in __get_path_to_schemas
other_path_to_schemas = cls._validate(arg, validation_metadata=validation_metadata)
File "/Users/justinblack/programming/openapi-json-schema-generator/samples/client/petstore/python/venv/lib/python3.8/site-packages/petstore_api/schemas/validation.py", line 139, in _validate
other_path_to_schemas = validator(
File "/Users/justinblack/programming/openapi-json-schema-generator/samples/client/petstore/python/venv/lib/python3.8/site-packages/petstore_api/schemas/validation.py", line 288, in validate_types
raise __get_type_error(
petstore_api.exceptions.ApiTypeError: Invalid type. Required value type is float and passed type was int at ['args[0]']
And both the java and python generators impose this type constraint.
from openapi-json-schema-generator.
The openapi format registry defines float as:
single precision floating point number
And it defines format double
as:
double precision floating point number
Why not define this data type as type: number
with no format? That way serialization + deserialization will work.
Do you need the single precision min and max constraint applied?
My suspicion is that float and double formats should allow integers in by default because some language contexts (javascript) do not know the difference. One could add a command line option intsAllowedForFloatDoubleFormats
to allow users to opt in or out of this behavior in the generated code. It would adjust the type definitions in that case.
I am asking for guidance from other implementers on this issue.
from openapi-json-schema-generator.
Hmm I see that ingesting 0 is failing for format float and double ingestion.
We get the same error message in our use case.
The openapi format registry defines float as:
single precision floating point number
And it defines formatdouble
as:double precision floating point number
Why not define this data type astype: number
with no format? That way serialization + deserialization will work. Do you need the single precision min and max constraint applied?
Just using type: number
will work as it includes integer values. The only problem we have is that we automatically create the openapi.yaml
with TSOA which will by default set format: double
. As the issue is more related to the behavior of TSOA and not primary to this repository, I created ticket on their side.
My suspicion is that float and double formats should allow integers in by default because some language contexts (javascript) do not know the difference. One could add a command line option
intsAllowedForFloatDoubleFormats
to allow users to opt in or out of this behavior in the generated code. It would adjust the type definitions in that case. I am asking for guidance from other implementers on this issue.
Independent of our problem, having such flag could give the user more flexibility but with a certain cost of adjusting the type definition as you said. Additionally, I could think of use cases where such flag is suitable and with making it optional, the default behavior will not change. The error message is still correct as ingesting a 0
where a float value is expected, should lead to a validation fail.
from openapi-json-schema-generator.
I just released version 4.3.0 which includes the command line arg --ints-allowed-for-float-double-formats
When one generates a client with it set to true, ints are allowed in to number with format float/double per:
>>> from unit_test_api.schemas import schemas
>>> schemas.Float32Schema.types
frozenset({<class 'float'>, <class 'int'>})
>>> schemas.Float32Schema.validate(0)
0
>>> schemas.Float64Schema.types
frozenset({<class 'float'>, <class 'int'>})
>>> schemas.Float64Schema.validate(0)
0
The value is defaulting to false right now to not break legacy code. In the next major release (5.0.0) I will change its default value to true so the defaults will allow normal json schema validation.
from openapi-json-schema-generator.
Thanks for your work :)
from openapi-json-schema-generator.
You're welcome
from openapi-json-schema-generator.
Related Issues (20)
- [REQ] Update pattern handling to disallow `/pattern/` and `/pattern/flags` HOT 1
- Java client, adds missing 3.1.0 schema validators HOT 1
- Java client, integrate servers into api configuration
- Java client, create component security schemes HOT 1
- Java client integrate component security schemes into ApiConfiguration
- [Announcement] 2024 Project Ideas HOT 1
- Java client, add requestBody feature
- Java client: add Response Object
- Java client: add Responses
- Java client, add header + serialization implementation
- [BUG] Error transforming enum values beginning with numbers HOT 1
- [REQ] add OAuth2SecurityScheme with the password flow HOT 1
- Java client, add operation
- [BUG][python] Missing quotes for array property in TypedDict definition HOT 2
- [BUG][python] Reference Error when using schema with optional properties HOT 2
- [BUG][python] Validation for nullable property HOT 3
- [REQ] Allow Content Type JSON Lines HOT 12
- [BUG] Build failure on java 17 and 21 HOT 1
- [BUG][python] Crash when generating const property HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from openapi-json-schema-generator.