Skip to content

Explicit null in json schema leads to incorrect typescript types (OA 3.1) #1005

@jordanebelanger

Description

@jordanebelanger

Hi, great library.

I don't know if this is a bug or not, given that I did not see that counterfact explicitly supports openapi 3.1

When you have a schema with a non-required, nullable type, i.e "type": ["string", "null"]

For example:

{
    "openapi": "3.1.0",
    "info": {
      "title": "API for Parsing Reproduction Bug",
      "description": "API to reproduce a bug related to parsing an OpenAPI definition.",
      "version": "1.0.0"
    },
    "paths": {
      "/exampleEndpoint": {
        "post": {
          "tags": ["Parsing Reproduction"],
          "summary": "Handles example requests for parsing reproduction.",
          "requestBody": {
            "required": true,
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "exampleProperty": {
                      "type": ["string", "null"]
                    }
                  }
                }
              }
            }
          },
          "responses": {
            "201": {
              "description": "Success",
              "content": {
                "application/json": {
                  "schema": {
                    "type": "object",
                    "properties": {
                      "resultProperty": {
                        "type": ["string", "null"]
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }

This will lead to a schema that looks like this:

// This code was automatically generated from an OpenAPI description.
// Do not edit this file. Edit the OpenAPI file instead.
// For more information, see https://github.com/pmcelhaney/counterfact/blob/main/docs/faq-generated-code.md

import type { WideOperationArgument } from "../../types.d.ts";
import type { OmitValueWhenNever } from "../../types.d.ts";
import type { Context } from "../../routes/_.context.ts";
import type { ResponseBuilderFactory } from "../../types.d.ts";

export type HTTP_POST = (
  $: OmitValueWhenNever<{
    query: never;
    path: never;
    header: never;
    body: { exampleProperty?: string; null };
    context: Context;
    response: ResponseBuilderFactory<{
      201: {
        headers: never;
        requiredHeaders: never;
        content: {
          "application/json": {
            schema: { resultProperty?: string; null };
          };
        };
      };
    }>;
    x: WideOperationArgument;
    proxy: (url: string) => "COUNTERFACT_RESPONSE";
    user: never;
  }>,
) =>
  | {
      status: 201;
      contentType?: "application/json";
      body?: { resultProperty?: string; null };
    }
  | { status: 415; contentType: "text/plain"; body: string }
  | "COUNTERFACT_RESPONSE"
  | { ALL_REMAINING_HEADERS_ARE_OPTIONAL: "COUNTERFACT_RESPONSE" };

Notice the schema typescript property being schema: { resultProperty?: string; null };

This is valid in openapi 3.1 now that json-schema is fully supported, but the generated type does not compile, it should be schema: { resultProperty?: string | null };

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions