Dorthu / openapi3

A Python3 OpenAPI 3 Spec Parser
BSD 3-Clause "New" or "Revised" License
118 stars 47 forks source link

Support float values for minimum and maximum #30

Closed ghost closed 3 years ago

ghost commented 3 years ago

Closes #29.

Not sure why I bothered with such a detailed report when it would've taken me a lot less time to just send this patch.

guyarb commented 3 years ago

@i-sf this fix seems to have a bug as well, if you set minimum or maximum fields to an integer (i.e. 1 which is a valid float), it will fail after your fix. I think the solution should be setting types as [int, float].

ghost commented 3 years ago

Note that type and format is ignored. I could put type: integer and format: int32 and use a float, and vice versa for the number double. This validation, which would probably be good to make, would require more changes, and is out of scope for this changeset (which is just support at all).

Aside: I spent an embarrasingly long time figuring out how lists for _get worked because I had to run pip install (which I wasn't doing) before pytest or it wouldn't pick up my changes. Do you know how to run pytest so that it uses the directory as the library without having to install it, or if it's possible at all?

gwerbin commented 3 years ago

FWIW, JSONSchema itself is vague about what constitutes a "number", and OAS borrows minimum and maximum directly from JSONSchema. It's probably safe to assume that { "format": "int32", "minimum": -32.5 } should be handled equivalently to { "format": "int32", "minimum": -32 }.

Note that type and format is ignored. I could put type: integer and format: int32 and use a float, and vice versa for the number double. This validation, which would probably be good to make, would require more changes, and is out of scope for this changeset (which is just support at all).

Aside: I spent an embarrasingly long time figuring out how lists for _get worked because I had to run pip install (which I wasn't doing) before pytest or it wouldn't pick up my changes. Do you know how to run pytest so that it uses the directory as the library without having to install it, or if it's possible at all?

Try doing pip install -e instead of just pip install. This will install in "editable" mode, which acts like a symlink to the package, rather than copying the .py. files. The practical effect is that you don't have to re-run pip install before every run of pytest.

ghost commented 3 years ago

JSON Schema

4.2.1. Instance Data Model

  • number: An arbitrary-precision, base-10 decimal number value, from the JSON "number" value

And also:

6.3. Mathematical Integers

Some programming languages and parsers use different internal representations for floating point numbers than they do for integers.

For consistency, integer JSON numbers SHOULD NOT be encoded with a fractional part.

Both these sections seem to imply that floating point numbers are supported. It also says:

The terms "JSON", "JSON text", "JSON value", "member", "element", "object", "array", "number", "string", "boolean", "true", "false", and "null" in this document are to be interpreted as defined in RFC 8259.

And from that RFC:

6. Numbers

The representation of numbers is similar to that used in most programming languages. A number is represented in base 10 using decimal digits. It contains an integer component […] which may be followed by a fraction part and/or an exponent part.


I would not consider the above vague by any means. Note that it is the JSON Schema Core the one who talks about numbers. The JSON Schema Validation, which refers to minimum, simply refers to the number previously defined, which can clearly be a float. Even if this was not the case, dropping a fractional part would be way too much. It would mean minimum: 0.1, maximum: 0.3 would make both 0.

ghost commented 3 years ago

@Dorthu could you please take a look? Thanks. (If possible, https://github.com/Dorthu/openapi3/pull/34, https://github.com/Dorthu/openapi3/pull/35 and https://github.com/Dorthu/openapi3/pull/38 as well…)

Dorthu commented 3 years ago

Sorry I missed this , looking at the now

Dorthu commented 3 years ago

Sorry again for the delay; all of the above are released in version 1.4.0