Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenApi attribute nullable with enum #993

Closed
left-rite opened this issue Apr 14, 2019 · 9 comments
Closed

OpenApi attribute nullable with enum #993

left-rite opened this issue Apr 14, 2019 · 9 comments
Labels

Comments

@left-rite
Copy link

What version of Ajv are you using? Does the issue happen if you use the latest version?
6.10.0

Ajv options object

{
  nullable: true
}

JSON Schema

{
  "type": "object",
  "properties": {
    "propertyOne": { 
      "type": "string",
      "nullable": true,
      "enum": [ "foo", "bar" ]
    }
  }
}

Sample data

{
  "propertyOne": null
}

Your code

var ajv = new Ajv(options);
var validate = ajv.compile(schema);
var result = validate(data);

Validation result, data AFTER validation, error messages

[
  {
    "keyword": "enum",
    "dataPath": ".propertyOne",
    "schemaPath": "#/properties/propertyOne/enum",
    "params": {
      "allowedValues": [
        "foo",
        "bar"
      ]
    },
    "message": "should be equal to one of the allowed values"
  }
]

What results did you expect?
The result to be true, since support for the keyword nullable has be set to true and schema has set nullable to true for the property "propertyOne".

Are you going to resolve the issue?
Not sure

@epoberezkin
Copy link
Member

Interesting. I am not sure that this should be the expected behaviour. It is definitely possible to add null into the enum explicitly.

Could you point to the OpenAPI doc that says that nullable should change the behaviour of enum?

@left-rite
Copy link
Author

I couldn't find a doc or example so I asked OAI, essentially the response was validation constraints like enum should only apply when the value is not null.

@mattpolzin
Copy link
Contributor

mattpolzin commented Jun 13, 2019

I've got a "fix" for this, if @epoberezkin is ok with calling the current behavior undesirable.

For me the problem with the current behavior is that I cannot add null to the enum without metaschema validation failing (the enum should only contain the type specified for the property and because we are using nullable rather than adding null to the array of types null is not allowed in the enum).

The following fails as mentioned by the OP.

{
  "type": "object",
  "properties": {
    "propertyOne": { 
      "type": "string",
      "nullable": true,
      "enum": [ "foo", "bar" ]
    }
  }
}

The next variation fails metaschema validation for the reason I mention above.

{
  "type": "object",
  "properties": {
    "propertyOne": { 
      "type": "string",
      "nullable": true,
      "enum": [ "foo", "bar", null ]
    }
  }
}

The final variation is not allowed for OpenAPI, which is the primary motivator for the nullable property.

{
  "type": "object",
  "properties": {
    "propertyOne": { 
      "type": ["string", null],
      "nullable": true,
      "enum": [ "foo", "bar", null ]
    }
  }
}

@epoberezkin
Copy link
Member

The next variation fails metaschema validation for the reason I mention above.

Sorry, which metaschema? You can put any values to enum - JSON Schema metaschema doesn’t have any requirement for item type.

@mattpolzin
Copy link
Contributor

Sorry, which metaschema?

My apologies, I was conflating JSON Schema with a particular meta-schema that I was using. You are correct, there is no validation problem here inherent to JSON Schema.

Well, I do have a solution coded up that obviates the need to explicitly add null to the enum if nullable is true, but this no longer feels quite so important.

FWIW I do agree with @left-rite that it makes sense in the context of OpenAPI to implicitly allow null even if it does not appear in the enum if nullable: true. I just don't see this as quite the same problem I did before.

@mattpolzin
Copy link
Contributor

@epoberezkin I realize you might still decide this is not an issue that AJV should handle differently than it currently does. No hard feelings if you ask me to close the PR I just opened.

Thanks.

@epoberezkin
Copy link
Member

I added the comment in PR before I saw it :)

I don’t have a strong opinion - let’s see how OpenAPI addresses.

@joviol
Copy link

joviol commented Feb 3, 2020

Hello,

What is the state of this issue?
The open API documentation clearly states that nullable enums are valid:
https://swagger.io/docs/specification/data-models/enums/

The format should be:
type: string
nullable: true # <---
enum:

  • asc
  • desc
  • null # <--- without quotes, i.e. null not "null"

Note that null must be explicitly included in the list of enum values. Using nullable: true alone is not enough here.

I tried this syntax in my swagger but Validation still fails:
"Validation failed: field: null is not a valid enum value for path field."

Is there any plan to fix this issue?

Thanks,
Kind Regards,

Joe

@epoberezkin
Copy link
Member

epoberezkin commented Feb 3, 2020

It’s difficult to say why it doesn’t work with your Open API toolchain without the code sample, Ajv would definitely accept the schema - please test with Ajv directly without swagger

OpenAPI clarified now that nullable is only extension of type keyword and it doesn’t change the behaviour of enum or anything else, so this issue should be closed.

See OAI/OpenAPI-Specification#1900 (comment)

The change they made is either merged or will be merged into 3.0.4 and definitely into 3.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants