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.

How do I set response models to not be optional as typescript is saying a value can be undefined when it can never be the case.

See original GitHub issue

I’m using fast endpoints with a typescript client automatically generated from the Open API specification generated by Fast Endpoints.

I have response models which I know they will always have a value, however, currently the response models are being created with all fields being marked as nullable, for example:

    id?: string;
    name?: string;
    headerImage?: string;

my understanding is that this is happening because the Open API definition is created as per the below:

    "schemas": {
      "Response": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "headerImage": {
            "type": "string"
          }
        }
      },

I’d like to generate this definition instead:

    "schemas": {
      "Response": {
        "type": "object",
        "required": [
          "id",
          "name",
        ],
        "additionalProperties": false,
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "headerImage": {
            "type": "string"
          }
        }
      },

Is it possible to create this OpenAPI definition?

Issue Analytics

  • State:closed
  • Created 7 months ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
dj-nitehawkcommented, Apr 21, 2023

added an extension method in 5.8.1.14-beta

builder.Services.AddSwaggerDoc(x =>
{
    x.MarkNonNullablePropsAsRequired();
});
2reactions
rubenmamocommented, Feb 24, 2023

Thanks for the update. I can confirm that the code above is a valid workaround, however, I think there will be some cases where this wouldn’t work, particularly if you don’t enable the nullable reference types feature of C#8. From what I can gather, the IsNullable function listed above doesn’t return true for arrays, strings or reference types which means that unless you enable nullable reference types this functionality will probably cause issues down the line in clients. I will do some further investigation and will update the issue with any information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Deal with Optional Things and "Undefined" in TypeScript
First, if you don't tell TypeScript that a property is optional, it will expect it to be set. TypeScript. type Foo = {...
Read more >
Why a non-optional variable can be undefined in this case
if I create a non-optional variable of type "data". Why the value in the next case can be undefined and how can I...
Read more >
Documentation - Do's and Don'ts
Don't ever use the types Number , String , Boolean , Symbol , or Object These ... Don't use the return type any...
Read more >
When to use typescript optional "?" parameter vs explicit ...
Undefined means that parameter must be passed in but its value may be undefined.
Read more >
How To Fix Object is Possibly Undefined In TypeScript
Non-null assertions can be used to avoid the "object is possibly undefined" error by explicitly telling TypeScript that a variable or property ...
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