-
-
Notifications
You must be signed in to change notification settings - Fork 872
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
OAS 3.0 schemas cannot make non-nullable references nullable #1882
Comments
Hey 👋 We're looking to find a tool that allows us to validate the As a bit of background, I want to reference the following tickets:
In general, when writing OAS 3.0 schemas, its very common for you to define your object schemas in the We also don't want to redefine the From the linked ticket, and the support added to various other tools that we rely on (such as the generators linked above), I have concluded that the appropriate way to express our scenario in OAS 3.0 is to utilise After trying to understand this project a little more, I have concluded that since the primary focus here is JSON Schema, support for I did however find that once I changed the {
"anyOf": [
{ "$ref": "#/$defs/Fork" },
{ "type": "object", "nullable": true }
]
} It's great that this works, but unfortunately this approach does not play well with other OAS tools (specifically the generators that we rely on) which suggests to me that its not an appropriate way to solve this problem. It does however give me an idea that maybe If not, I guess I can implement this myself, or maybe even in the OAS specific validator tools that depend on this package, but I wanted to start here before I went anywhere else 🙂 Let me know what you think! If you have a suggestion for how to approach solving the problem, I'd love to try and help out but I am not too familiar with the project so I'll probably need some guidance first. Thanks! |
nullable is defined as extension of type keyword that should be located in the same schema object, not as extension of any schema. To make it work you need to add |
Thanks @epoberezkin, I didn't know the specifics around that however after adding
I wanted to write the report using Here is a more complete OAS example: openapi: 3.0.3
info:
version: 1.0.0
title: Sample API
servers:
- url: https://cookpad.github.io/global-mobile-hiring
paths:
/api/foos:
get:
responses:
200:
description: ''
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Foo'
components:
schemas:
Foo:
type: object
required:
- nullable_bar
- non_nullable_bar
properties:
nullable_bar:
type: object
allOf:
- $ref: '#/components/schemas/Bar'
nullable: true
example: null
non_nullable_bar:
$ref: '#/components/schemas/Bar'
Bar:
type: object
required:
- id
properties:
id:
type: integer
example: 42
|
There is a bug that makes it unusable in typescript interface test {
test?: string | Array<string>
}
const schema1: JSONSchemaType<test> = {
type: 'object',
properties: {
test: {
// nullable: true,
anyOf: [
{
type: 'string',
},
{
type: 'array',
items: {
type: 'string',
},
},
],
},
},
} Without nullable it throws a typescript error, with nullable it throws the runtime error |
@liamnichols - This issue is a bit old but if it's still relevant you could try adding I've run into a similar issue (htps://github.com/cdimascio/express-openapi-validator/issues/839) and adding an extra nullable seems to be a possible workaround, i.e. Bar:
type: object
nullable: true
required:
- id
properties:
id:
type: integer
example: 42 |
Thanks @pebo for the suggestion. The problem that we had with this (in our use case) is that code generators will consider any property with a ref to |
Hello @liamnichols, I had exactly the same problem and I found a solution: my basic schema which gave me the same error:
I went through an additional diagram and it's ok. well supported by AJV, and in my case I am on Fastify and fastify swagger and everything is ok
|
What version of Ajv are you using? Does the issue happen if you use the latest version?
8.9.0
Ajv options object
JSON Schema
Sample data
Your code
RunKit link - https://runkit.com/liamnichols/61ed680062e12c0008e66e2a
Validation result, data AFTER validation, error messages
After adding
"type": "object"
:What results did you expect?
Sample data to validate without any errors
Are you going to resolve the issue?
I dug into the code, but I got stuck and I need guidance on how best to approach the problem.
The text was updated successfully, but these errors were encountered: