Skip to content

Nested object properties are generated are optional #156

Closed
@Fabiencdp

Description

@Fabiencdp

Hi ! I just noticed something :

When generating responses route types from a nested schema, like this (truncated for brevety) :

  "200": {
    "description": "Success",
    "content": {
      "application/json": {
        "schema": {
          "type": "object",
          "required": [
            "user"
          ],
          "additionalProperties": false,
          "properties": {
            "user": {
              "type": "object",
              "required": [
                "id",
                "firstName",
                "lastName",
                "email",
                "role"
              ],
              "properties": {
                "id": {
                  "type": "number",
                  "nullable": false
                },
                "firstName": {
                  "type": "string",
                  "nullable": false
                },
                "lastName": {
                  "type": "string",
                  "nullable": false
                },
                "email": {
                  "type": "string",
                  "nullable": false
                },
                "role": {
                  "type": "object",
                  "required": [
                    "id",
                    "name"
                  ],
                  "properties": {
                    "id": {
                      "type": "number",
                      "nullable": false
                    },
                    "name": {
                      "type": "string",
                      "nullable": false
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }

The result is :

export namespace UserEditGet {
  export type RequestParams = { userId: number };
  export type RequestQuery = {};
  export type RequestBody = never;
  export type RequestHeaders = {};
  export type ResponseBody = {
    user: { id?: number; firstName?: string; lastName?: string; email?: string; role: { id: number; name: string } };
  };
}

Notice the optional (?) properties inside the "user" object, but not in nested "role" object.
If i remove the "user" property from the schema, here is what happen :

  export namespace UserEditGet {
    export type RequestParams = { userId: number };
    export type RequestQuery = {};
    export type RequestBody = never;
    export type RequestHeaders = {};
    export type ResponseBody = {
      id: number;
      firstName: string;
      lastName: string;
      email: string;
      role: { id?: number; name?: string };
    };
  }

Now, "user" properties at the object root are required as needed, but "role" properties are optional.

If, for example, i add another nested object in "role", these properties are fine, it's look like it only happen on the first nested properties.

I will try to dig it if i found some free time tomorrow.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions