Tsed: [BUG] Swagger\OpenApi schema nullable issue

Created on 22 Nov 2020  路  11Comments  路  Source: tsedio/tsed

Information

  • Version: 6.10.3
  • Packages:

Hey Guys,
It seems that the schema is not generated with nullable attribute (and more). (specVersion: '3.0.1',)
Consider the following examples

Example

@Any(String, null)
pollingId?: string

// Generates   
"pollingId": {
  "type": [
    "string",
    "null"
  ]
}

Not valid
Validation does "Errors: -attribute components.schemas.ProjectModel.type is not of type string"

@Required(false, null)
pollingId?: string

// Generates
"pollingId": {
  "type": "string"
}

So it can be missing but can not be null so you can not reset it if you need

Additionally

@Required(false, null)
pollingId?: string | null

// Results to
"pollingId": {
  "type": "object"
}
//  wrong. string | null is need to set correct type on prop but it affects the schema and it is not correct in this case

Just to mention, model validation also needs to be checked for the case.
At the moment if you were to POST {pollingId: null}
@BodyParams() payload: PollingModel does payload {}
but I expect pollingId to be either string or null, not just missing\undefined.

Acceptance criteria

There should be possibility to get the following output

"pollingId": {
  "type": "string",
    "nullable": true
}

and define type like pollingId?: string | null

bug released

Most helpful comment

Should be fixed :)

All 11 comments

:tada: This issue has been resolved in version 6.11.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

How would i decorate the following type?

class xy {
   public nullableModel: null | SomeComplexModel;
}

i tried @Required(false, null) as well as @Any(SomeComplexModel, null) and a bunch of others as well as combinations of them and could not figure it out...

@Romakita

@jappyjan
not sure what you mean by "declare type", but should not the following do the trick?

  @Any(Object, null)
  nullableModel?: SomeComplexModel | null

Indeed, @Romakita

  @Any(MyModel, null)
  foo?: MyModel | null


  @Any('string', null)
  bar?: string | null

outputs

          "foo": {
            "type": "object",
            "nullable": true
          },
          "bar": {
            "type": "string",
            "nullable": true
          },

v 6.13.1

exactly... so i guess this is a bug, right? or is this the way it is supposed to be?

Currently, Required doesn't support Model and null at the same type. Isn't really a bug because null type doesn't exists in OS2/OS3 (in JsonSchema, null exists). Let me see if a solution is possible.

Should be fixed :)

v6.14.2

@Romakita I think this bug came back again in 6.34.3

Arf... probably related to the latest refactoring about the RequiredGroups decorator. I'm surprised to haven't integration test on this use case :(

@freez10 Are you sure? I checked the integration test here:
https://github.com/TypedProject/tsed/tree/production/packages/schema/test/nullable.integration.spec.ts

The output is correct and the integration test works as expected. I'm not able to reproduce the bug. So I'll close this issue.

See you
Romain

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tobiasmuecksch picture tobiasmuecksch  路  6Comments

haroon407 picture haroon407  路  4Comments

revich2 picture revich2  路  4Comments

royibernthal picture royibernthal  路  3Comments

Ionaru picture Ionaru  路  5Comments