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.

Non-nullable properties are marked as optional in generated TypeScript

See original GitHub issue

I have the YAML below. Two properties are explicit nullable: true (completedBy, completedTime) while the others are implicit nullable: false. When generating a TypeScript client I get the following interface:

export interface ITask {
    plantId?: string;
    hoursInterval?: number;
    deadline?: Date;
    completedBy?: string | null;
    completedTime?: Date | null;
    taskId?: string;
}

I would expect all properties to not have the ? as they are not nullable. Except of course completedBy and completedTime.

openapi: 3.0.1
info:
  title: Non-nullable demo
  description: Non-nullable properties are treated as nullable in generated TypeScript
  version: v1
servers:
  - url: /v1
    description: Api version 1.0
paths:
  '/tasks':
    get:
      summary: ''
      tags: []
      operationId: getTasks
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Tasks'
components:
  schemas:
    Tasks:
      description: A list of tasks.
      type: array
      items:
        $ref: '#/components/schemas/Task'
      x-tags:
        - Tasks
      title: Tasks
    Task:
      title: Task
      type: object
      description: A task.
      properties:
        plantId:
          type: string
        hoursInterval:
          type: integer
        deadline:
          type: string
          format: date-time
        completedBy:
          nullable: true
          type: string
        completedTime:
          nullable: true
          type: string
          format: date-time
        taskId:
          type: string

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:12
  • Comments:26 (3 by maintainers)

github_iconTop GitHub Comments

12reactions
kev-andrewscommented, Aug 17, 2022

Thank you for great suggestion!

@Liwoj Our schema processor looks like this:

public class MarkAsRequiredIfNonNullableSchemaProcessor : ISchemaProcessor
{
    public void Process(SchemaProcessorContext context)
    {
        foreach (var (propName, prop) in context.Schema.Properties)
        {
            if (!prop.IsNullable(SchemaType.OpenApi3))
            {
                prop.IsRequired = true;
            }
        }
    }
}

And registering it looks like this:

options.SchemaProcessors.Add(new MarkAsRequiredIfNonNullableSchemaProcessor());

I agree that the default behaviour seems strange here.

If anyone has problems with the current workaround not functioning correctly for schemas involving inheritance, this seems to be fixed by looping over Schema.ActualProperties instead of Schema.Properties

9reactions
jgilchristcommented, Mar 15, 2021

@Liwoj Our schema processor looks like this:

public class MarkAsRequiredIfNonNullableSchemaProcessor : ISchemaProcessor
{
    public void Process(SchemaProcessorContext context)
    {
        foreach (var (propName, prop) in context.Schema.Properties)
        {
            if (!prop.IsNullable(SchemaType.OpenApi3))
            {
                prop.IsRequired = true;
            }
        }
    }
}

And registering it looks like this:

options.SchemaProcessors.Add(new MarkAsRequiredIfNonNullableSchemaProcessor());

I agree that the default behaviour seems strange here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to make nullable properties optional in TypeScript?
I created two utility types which pick all nullable not non-nullable properties from a type respectively. We can use these to construct a ......
Read more >
Making optional properties nullable in TypeScript - rbardini.com
Your types are defined with non-nullable optional properties (e.g., x?: number ), but the data coming from the API returns null instead.
Read more >
Documentation - TypeScript 3.7
First there's the optional element access which acts similarly to optional property accesses, but allows us to access non-identifier properties (e.g. ...
Read more >
Using Optional and Nullable Properties in API Requests
Learn how optional and nullable properties can be used flexibly in combinations in each parameter of your API requests made from a SDK....
Read more >
Documentation - TypeScript 2.0
Optional parameters and properties automatically have undefined added to their types, even when their type annotations don't specifically include undefined ...
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