question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: nullable and enum

See original GitHub issue

Hey,

I am hoping to get clarification on the following:

When the nullable and enum keywords are used together, which takes precedence?

i.e.

type: string
nullable: true
enum:
  - one
  - two
propertyA = null

When propertyA is validated against the definition. Should the result be:

  1. true, since the it’s nullable
  2. false, because null is not part of the enum
  3. something else?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:29 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
handrewscommented, Apr 28, 2021

@EvanCarroll you realize OAS is not a C++ code generation specification, right? Anything about mapping OAS to C++ code is going to be specific to your tooling. OAS says what will and won’t validate as a JSON/YAML/etc. instance. That doesn’t always map cleanly to every programming language, and it’s up to code generators to decide what to do with those scenarios.

nullable: True, in the presence of a type field, means that a JSON null is valid in addition to whatever type (plus constraints from other keywords including enum) the schema allows That’s it. That’s all OAS says. If a value fails against the enum constraint, then it doesn’t matter what the other keywords in the schema do, it’s already failed. OAS does not say anything about constructors, or the subtleties of null values vs typing systems in different languages. That is is outside of the scope of the spec.

2reactions
handrewscommented, Apr 7, 2021

@richardwhiuk if you want to forbid null while using an enum, you need to either remove null from the enum or set type: string so that the nullable: false takes effect. Without type, nullable has no effect (in OAS 3.0.3, and it may not be implemented consistently since it was ambiguous in 3.0.0-3.0.2). This is part of why nullable is gone in OAS 3.1.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A curious C# syntax with a question mark
Your code is using what's called a nullable type. An enum, much like an int or a DateTime, is what is known as...
Read more >
Nullable enumeration values vs. "NoValue" or "Undefined ...
My question is: is it better to represent the value of the Result field in my domain object as a nullable value (e.g....
Read more >
ENUM - MariaDB Knowledge Base
An ENUM can also contain NULL and empty values. If the ENUM column is declared to permit NULL values, NULL becomes a valid...
Read more >
Should enum be nullable or not?
Yes, the only downside there is that the value for null would have to be the default value for value types — otherwise,...
Read more >
[Solved] Argumentexception setting enum type property
Use the "?" notation to make a non-nullable type nullable. public enum Types : int { Good = 1, Medium = 2, Critical...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found