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

nullable property is not nullable in generated code #2055

Open
rwb196884 opened this issue Dec 18, 2024 · 5 comments
Open

nullable property is not nullable in generated code #2055

rwb196884 opened this issue Dec 18, 2024 · 5 comments
Assignees
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library question Further information is requested

Comments

@rwb196884
Copy link

Here is a property in my swagger.json

          "Retained9": {
            "maximum": 100,
            "minimum": 0,
            "type": "integer",
            "description": "Retained9\r\nThis property is....\r\n",
            "format": "int32",
            "default": "100",
            "nullable": true
          },

Using openapi-typescript version 6.7.6 this generates

      /**
       * Format: int32
       * @description Retained9
       * This property is...
       *
       * @default 100
       */      Retained9?: number | null;

But using openapi-typescript version 7.4.4 this generates

            /**
             * Format: int32
             * @description Retained9
             *     This property is used...
             *
             * @default 100
             */
            Retained9: number;

It is good that the useless and annoying ? has gone, but in the specification the property is nullable and therefore it is an error to have removed the | null.

@rwb196884 rwb196884 added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Dec 18, 2024
@drwpow drwpow self-assigned this Jan 3, 2025
@drwpow
Copy link
Contributor

drwpow commented Jan 3, 2025

Could you run your schema through Redocly’s validator, and confirm there are no schema issues?

We do have tests in place for nullable: true so if you are experiencing an issue with a valid schema, we’ll need more information or a reproduction to tell what’s going on here.

@drwpow drwpow added the question Further information is requested label Jan 3, 2025
@rwb196884
Copy link
Author

Here's an example.
WebApplicationOpenApi.zip

C# property is

        [Column("Retained9", Order = 30)]
        [Editable(true)]
        [Display(Name = "Retained9", Description = "Retained9")]
        [DefaultValue((short)100)]
        [Range(0, 100)]
        public short? Retained9 { get; set; }

swaggergen produces

          "retained9": {
            "maximum": 100,
            "minimum": 0,
            "type": "integer",
            "format": "int32",
            "default": 100,
            "nullable": true
          }

and then openqpi-typescript produces

            /**
             * Format: int32
             * @default 100
             */
            retained9: number;

It seems to be the [DefaultValue that causes the nullability to be ignored.

@rwb196884
Copy link
Author

It is a breaking change that nullable properties with default values have their nullability changed.

@rwb196884
Copy link
Author

The option --default-non-nullable false adds the ? which allows undefined but not the | null to allow null.

@jawnothin
Copy link

The issue is that this package targets openapi 3.1, which swashbuckle doesn't support. It ignores how openapi 3.0 generates it. I commented on another similar issue a while ago about this, but it went stale.

I've discovered the cause of this, at least for my use case. My openapi spec is based off 3.0.1, and it uses the "nullable", which this package seems to ignore as it's marked as deprecated because it's not a concept in the 3.1 spec (presumably, I didn't dig into it more)

This is the patch, which I didn't open a PR as there's probably other considerations.

diff --git a/packages/openapi-typescript/src/transform/schema-object.ts b/packages/openapi-typescript/src/transform/schema-object.ts
index 0885324..e4fceaf 100644
--- a/packages/openapi-typescript/src/transform/schema-object.ts
+++ b/packages/openapi-typescript/src/transform/schema-object.ts
@@ -458,6 +458,7 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
             options.ctx.defaultNonNullable &&
             !options.path?.includes("parameters") &&
             !options.path?.includes("requestBody")) // parameters can’t be required, even with defaults
+            || !("nullable" in v) || ("nullable" in v && v.nullable === false)
             ? undefined
             : QUESTION_TOKEN;
         let type =

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants