astahmer / openapi-zod-client

Generate a zodios (typescript http client with zod validation) from an OpenAPI spec (json/yaml)

Home Page:openapi-zod-client.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

additionalProperties with nullable: true results in unknown schema

austins opened this issue · comments

I have a couple schemas that are producing z.unknown() if they have additionalProperties: { "nullable": true }.

    "openapi": "3.0.0",
    ...
    "components": {
        "schemas": {
            "HttpValidationProblemDetails": {
                "allOf": [
                    {
                        "$ref": "#/components/schemas/ProblemDetails"
                    },
                    {
                        "type": "object",
                        "additionalProperties": {
                            "nullable": true
                        },
                        "required": [
                            "errors"
                        ],
                        "properties": {
                            "errors": {
                                "type": "object",
                                "additionalProperties": {
                                    "type": "array",
                                    "items": {
                                        "type": "string"
                                    }
                                }
                            }
                        }
                    }
                ]
            },
            "ProblemDetails": {
                "type": "object",
                "additionalProperties": {
                    "nullable": true
                },
                "properties": {
                    "type": {
                        "type": "string",
                        "nullable": true
                    },
                    "title": {
                        "type": "string",
                        "nullable": true
                    },
                    "status": {
                        "type": "integer",
                        "format": "int32",
                        "nullable": true
                    },
                    "detail": {
                        "type": "string",
                        "nullable": true
                    },
                    "instance": {
                        "type": "string",
                        "nullable": true
                    }
                }
            },
        }
    }

This produces:

const ProblemDetails = z.record(z.unknown());
const HttpValidationProblemDetails = ProblemDetails.and(z.record(z.unknown()));

Same if nullable was set to false in the schemas above.

If both schemas have set additionalProperties: false:

const ProblemDetails = z
  .object({
    type: z.string().nullable(),
    title: z.string().nullable(),
    status: z.number().int().nullable(),
    detail: z.string().nullable(),
    instance: z.string().nullable(),
  })
  .partial();
const HttpValidationProblemDetails = ProblemDetails.and(
  z.object({ errors: z.record(z.array(z.string())) })
);

If additionalProperties: {} (empty object), it will produce passthrough schemas:

const ProblemDetails = z
  .object({
    type: z.string().nullable(),
    title: z.string().nullable(),
    status: z.number().int().nullable(),
    detail: z.string().nullable(),
    instance: z.string().nullable(),
  })
  .partial()
  .passthrough();
const HttpValidationProblemDetails = ProblemDetails.and(
  z.object({ errors: z.record(z.array(z.string())) }).passthrough()
);

Is having additionalProperties: { "nullable": true } valid? If so, then should openapi-zod-client support this type of value?

I did a quick search and couldn't find that "nullable": true for additionalProperties, the https://editor.swagger.io/ also throws an error when using it, I don't think this is valid ? and therefore will not be implemented

Thanks, turns out the API generated an incorrect spec via NSwag. I've changed the error response model and it now outputs a valid schema. Closing the issue.