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

Redocly lint error for nullable object properties #3274

Open
2 of 4 tasks
drewish opened this issue Jan 26, 2025 · 1 comment
Open
2 of 4 tasks

Redocly lint error for nullable object properties #3274

drewish opened this issue Jan 26, 2025 · 1 comment

Comments

@drewish
Copy link
Contributor

drewish commented Jan 26, 2025

Is there an existing issue for this?

  • I have searched the existing issues

#2948 seems similar but was closed as fixed and I'm presuming it was incorporated into the 8.x branch.

Current behavior

With the plugin enabled and looking at the Typescript code a property like:

  @ApiProperty({
    description: 'The user that owns the cat',
  })
  nullableOwner: UserDto | null;

, it generates the schema's property like:

          "nullableOwner": {
            "nullable": true,
            "description": "The user that owns the cat",
            "allOf": [
              {
                "$ref": "#/components/schemas/UserDto"
              }
            ]
          }

Redocly's linter flags as an error:

[4] dist/api-spec.json:84:25 at #/components/schemas/CreateCatDto/properties/nullableOwner/nullable

The `type` field must be defined when the `nullable` field is used.

82 | },
83 | "nullableOwner": {
84 |   "nullable": true,
85 |   "description": "The user that owns the cat",
86 |   "allOf": [

Error was generated by the struct rule.

Minimum reproduction code

https://github.com/drewish/swagger-nullable-poc

Steps to reproduce

Setup the repro repo:

git clone https://github.com/drewish/swagger-nullable-poc
cd swagger-nullable-poc
npm i

Start it up to generate the swagger file (main.ts is saving a copy)

npm start

Then use redocly to lint the file:

npx @redocly/cli@latest lint dist/api-spec.json

There are unrelated errors I didn't bother cleaning up. The relevant one is:

The `type` field must be defined when the `nullable` field is used.

Expected behavior

One workaround I've see for this (microsoft/typespec#4584) was to just add type: 'object' as a sibling:

          "nullableOwner": {
            "type": "object",
            "nullable": true,
            "description": "The user that owns the cat",
            "allOf": [
              {
                "$ref": "#/components/schemas/UserDto"
              }
            ]
          }

That renders nicely in Redocly's UI:
Image
And looks the same in SwaggerUI:
Image

It seems like in ModelClassVisitor.createTypePropertyAssignments(), when we create the nullable property:

return [
...propertyAssignments,
factory.createPropertyAssignment(
'nullable',
createBooleanLiteral(factory, true)
)
];

We could also add type: 'object'.

But, we'd also need a change in SchemaObjectFactory.extractPropertiesFromType() to keep it from deleting the type:

delete schemaObjectMetadata.type;

Package version

11.0.3

NestJS version

11.0.1

Node.js version

v20.16.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@drewish
Copy link
Contributor Author

drewish commented Jan 30, 2025

I'm going to try out a PR for this. If nothing else we can run a patch/fork.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant