Validation for integer enums fails with good values.
Maybe this is the fix ... https://bitbucket.org/atlassian/swagger-request-validator/addon/pipelines/home#!/results/426 ? Using 1.4.2 with a definition such as "type": "integer", "enum": [ 5, 10, 30 ] Passing any integer value such as 10 fails validation. Instance value (10) not found in enum (possible values: ["5","10","30"]) I pulled the available download but the fix mentioned above was not in there.
Comments (17)
-
-
My bad. We had the same issues last year and could only solve it with a hack.
Unfortunately this is not a bug in the S-R-V. It's deeper in the
swagger-models
dependency.
TheModelImpl.java
is returning anList<String>
for all enum values. Even for those with a number type. So if the S-R-V is validating it's checking if the number10
is in the this list with strings["5", "10", "30"]
. It's obviously not.Please have a look at this Swagger issue: https://github.com/swagger-api/swagger-core/issues/2449
A not so sweet workaround is described, there.
I don't think the bug is fixed any time soon. Perhaps never without a Swagger rewrite. This kind of architecture is deeply rooted within theswagger-models
.We solved this issue by compiling a fixed version of the
ModelImpl.java
and reassembling it into a fixedswagger-models.jar
. -
Perhaps Mr. @jfnavin knows a proper place for a workaround in the S-R-V until the real issue is fixed.
-
Interesting.
I think this might be resolved on the v2.0 branch as the OAI v3 models return the correct types for enums.
In the 1.x branch the fix is probably to override the enum validation in the
com.atlassian.oai.validator.parameter.BaseNumericParameterValidator
to convert the list returned from the Swagger model to numeric values prior to validation. -
Account Deleted Thanks for the response. So when @jfnavin says 'this might be resolved on the v2.0 branch' does he mean the v2.0 of S-R-V ? If so is there an ETA on that?
-
@underboss you can watch issue
#113- there is an RC release available for testing with - I'm hoping to get a finalized 2.0 release in the next few weeks. -
Can I just confirm whether the failure you're seeing is in a parameter (e.g. query param) or a request/response body?
-
Account Deleted @jfnavin A request body.
-
Any chance you could attach an example schema and request? Im struggling to reproduce the problem in unit tests...
-
I've created such a unit test. See how it fails: https://bitbucket.org/atlassian/swagger-request-validator/pull-requests/80
-
Thanks for that @sdoeringNew - I ended up reproducing it.
Looks like it only occurs for 'top-level' schema definitions, but I think that's because enum validation isn't happening for 'nested' elements (e.g. what you've called
innerEnum
). I'm digging around in thejson-schema-validator
code now to try and work out whats happening. -
Ive confirmed that this is fixed on the v2.0 branch.
I'm going to leave this as-is on the 1.x release train (the fix is a bit hacky - we essentially have to 'clean' the schema before validating in the
SchemaValidator
, which involves looking forenums
and then trying to 'guess' what type they should be) and invest time in getting the v2.0 branch stable instead.If this is pressing for you feel free to raise a PR against the 1.x (master) branch - I will happily give advice on how the fix could be applied.
-
Account Deleted @jfnavin Thanks for the fix, I will wait for v2.0. Is there an ETA?
-
For completeness - there is a bug in the swagger v2 parser that means the
enum
is dropped for numeric properties whenformat
is not specified - https://github.com/swagger-api/swagger-parser/issues/445 (this is why the 'nested' enum validation wasn't being triggered in my tests).E.g.
"Test": { "required": [ "intenumfield", "floatenumfield", "referencedenumfield" ], "properties": { "intenumfield": { "type": "integer", "format": "int32", // Format specified, enum validation will work "enum": [ 10, 20 ] }, "floatenumfield": { "type": "number", // Format not specified, enum will not be validated "enum": [ 10.5, 20.5 ] }, "referencedenumfield": { "$ref": "#/definitions/IntEnum" } } }, "IntEnum": { // 'top-level' schema, enum validation will occur "type": "integer", "enum": [ 10, 20 ] }
-
For even more completeness, this is the issue in swagger-core https://github.com/swagger-api/swagger-core/issues/2449 which leads to the issue in the swagger-parser.
-
Indeed. I should've paid more attention when you first posted it :)
-
- changed status to resolved
Available in v1.4.5
- Log in to comment
Can you be a bit more specific?
We have a lot of integer type with enums in our Swagger definition and the SRV validates just fine and as expected. Can you share a part of your Swagger interface?