loopbackio / loopbackio/loopback-next

Invalid TS models when properties are nullable

Open
#9,024 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug CLI OpenAPI
Dominant language
TypeScript
Stars
5.1k
Forks
1.1k
Avg merge
2d 21h
Merged PRs (30d)
27

Description

Hi! It seems the openapi connector is not setting the correct TS type when setting true the nullable flag

For example, with the following JSON OpenAPI definition model:

      "TestObject": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string",
            "nullable": true,
             "default": null,
            "description": "Test property",
            "example": null
          },

It creates a model with the following property:

  @property({jsonSchema: {
  type: [
    'string',
    'null',
  ],
  default: null,
  description: 'Specific endpoint for this queue, if not specified, the generic endpoint for this service will be used',
}})
  endpoint?: string = null;

Obviously, as endpoint is set as string, it throws a TS Check error in build time:
error TS2322: Type 'null' is not assignable to type 'string | undefined'.

Why isn't setting the property type correctly, whereas in the type annotation seems to be aware of it?

Thanks!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the OpenAPI connector's TypeScript model generation for a property with nullable: true and default: null. Compare the generated property annotation with its declared type, then run the relevant model-generation checks or TypeScript build; done means nullable properties accept null without TS2322.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.