Validation for integer enums fails with good values.

Issue #118 resolved
Former user created an issue

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)

  1. Sven Döring

    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?

  2. Sven Döring

    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.
    The ModelImpl.java is returning an List<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 number 10 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 the swagger-models.

    We solved this issue by compiling a fixed version of the ModelImpl.java and reassembling it into a fixed swagger-models.jar.

  3. Sven Döring

    Perhaps Mr. @jfnavin knows a proper place for a workaround in the S-R-V until the real issue is fixed.

  4. James Navin

    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.

  5. Former user 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?

  6. James Navin

    @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.

  7. James Navin

    Can I just confirm whether the failure you're seeing is in a parameter (e.g. query param) or a request/response body?

  8. James Navin

    Any chance you could attach an example schema and request? Im struggling to reproduce the problem in unit tests...

  9. James Navin

    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 the json-schema-validator code now to try and work out whats happening.

  10. James Navin

    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 for enums 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.

  11. James Navin

    For completeness - there is a bug in the swagger v2 parser that means the enum is dropped for numeric properties when format 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
          ]
        }
    
  12. Log in to comment