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

API - How to have valid nullable fields in swagger ? #71

Closed
fdescamps opened this issue Jun 23, 2017 · 10 comments
Closed

API - How to have valid nullable fields in swagger ? #71

fdescamps opened this issue Jun 23, 2017 · 10 comments

Comments

@fdescamps
Copy link

Hi,

Thanks for express-openapi. I really appreciate to use it :)
But I have a problem, I need to expose fields which the type strings that could be nullable.
This following code works when i run the server based on express-api :

responses: { 200: { schema: { title: 'MyObject', properties: { myfield: { type: ['string', 'null'],

But it's not valid against Swagger (editor.swagger.io) :(

Semantic error at definitions.MyObject.myfield "type" should be a string

Do you have a valid way to use nullable fields with express-openapi ?

Thanks by advance !

And Thanks for this top library.

Regards,

F.

@MugeSo
Copy link
Contributor

MugeSo commented Jun 24, 2017

At first, your question is not library matter but just OpenAPI (swagger) matter.
So I think you should ask it in other better place.

Answer is here:
OAI/OpenAPI-Specification#229

@jsdevel
Copy link
Contributor

jsdevel commented Jun 24, 2017

@fdescamps try using anyOf. Glad that you like the framework!

@fdescamps
Copy link
Author

fdescamps commented Jun 26, 2017

Hi,

Thanks for your answers.

I tried anyOf like that :

`
responses: {

200: {

  description: 'MySchema',

  schema: {

    title: 'Schema Example',

    properties: {

      myproperty: {

        example: 'myvalue',

        anyOf: [

          { type: 'string' },

          { type: 'null' },

        ],

      },

`

But when the nodejs is starting, an exception is thrown :

`express-openapi: Validating schema after populating paths

express-openapi: validation errors [
{

"property": "instance.paths./leads/{id}.get.responses[200]",

"message": "is not exactly one from <#/definitions/response>,<#/definitions/jsonReference>",

"schema": {

  "oneOf": [

    {

      "$ref": "#/definitions/response"

    },

    {

      "$ref": "#/definitions/jsonReference"

    }

  ]

},

[...]

"name": "oneOf",

"argument": [

  "<#/definitions/response>",

  "<#/definitions/jsonReference>"

],
"stack": "instance.paths./leads/{id}.get.responses[200] is not exactly one from <#/definitions/response>,<#/definitions/jsonReference>"

`

Do you know how to write a right anyOf ?

Thanks.

@fdescamps
Copy link
Author

More Informations, oneOf is not valid, but allOf is valid

@jsdevel
Copy link
Contributor

jsdevel commented Jun 29, 2017

@fdescamps take a look at the openapi schema. It should have the answer you're looking for.

@jsdevel
Copy link
Contributor

jsdevel commented Mar 14, 2018

closing due to inactivity. let us know if you still have questions @fdescamps

@jsdevel jsdevel closed this as completed Mar 14, 2018
jsdevel added a commit that referenced this issue Jun 23, 2018
@jsdevel
Copy link
Contributor

jsdevel commented Jun 23, 2018

@fdescamps I recently adding schema extension support. Should address this.

@lzear
Copy link

lzear commented Jun 28, 2018

Is there a way to correctly validate requests/responses having nullable fields with OpenAPI v3?

The OAI specifies that nullable fields should use the keyword "nullable", which doesn't seem to be implemented in any JS library for validation.
There are several discussions related to this topic:
ajv-validator/ajv#486
OAI/OpenAPI-Specification#1389
OAI/OpenAPI-Specification#229

I started working on trying to convert OAI's syntax to JSON-schema's (see #127). Does it make sense to work in that direction?

@Frizzled
Copy link

Frizzled commented Jul 3, 2018

The example in the test wouldn't pass validation until the quotes were removed from the $ref value:

$ref: #/definitions/schema

Error with quotes around #/definitions/schema is:

Error resolving $ref pointer "#/definitions/schema". 
Token "schema" does not exist.

@jsdevel
Copy link
Contributor

jsdevel commented Jul 3, 2018

@Frizzled which test are you referring to?

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

No branches or pull requests

5 participants