Skip to content

Core: Validation throws error when using fastapi.sse #3072

@BHznJNs

Description

@BHznJNs

Description

I am trying to use the new SSE function in the latest version of FastAPI, the fastapi.sse will attach itemSchema field in the generated openapi endpoint definition like this:

{
  "openapi": "3.1.0",
  "info": {
    "title": "FastAPI",
    "version": "0.1.0"
  },
  "paths": {
    "/api/events/": {
      "post": {
        "tags": [
          "stream"
        ],
        "summary": "Sse Endpoint",
        "operationId": "sse_endpoint",
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "text/event-stream": {
                "itemSchema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "string"
                    },
                    "event": {
                      "type": "string"
                    },
                    "id": {
                      "type": "string"
                    },
                    "retry": {
                      "type": "integer",
                      "minimum": 0
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "text/event-stream": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponseSchema"
                }
              }
            }
          },
          "401": {
...

And when the itemSchema appear, the orval will throw an error like this:

api - Validation failed
Cause: [
{
  "message": "if must match \"else\" schema",
  "path": "/paths/~1api~1events~1"
},                                                    
...                                                     

And I have to manually exclude the sse endpoints in the fastapi app to make orval normally generate other schemas. I have already excluded sse endpoints in the orval config.

Configuration (orval.config)

import { defineConfig, type InputOptions, type OperationOptions } from "orval";

const operationSpecificConfig: Record<string, OperationOptions> = {
  fetch_models: {
    query: {
      useInfinite: false,
      useSuspenseInfiniteQuery: false,
    },
  },
};

const input: InputOptions = {
  target: "../openapi.json",
  filters: {
    mode: "exclude",
    tags: ["stream"],
  },
};

export default defineConfig({
  api: {
    input,
    output: {
      clean: true,
      target: "./src/api/generated/endpoints/",
      schemas: "./src/api/generated/schemas/",
      operationSchemas: "./src/api/generated/schemas/params/",
      client: "react-query",
      mode: "tags-split",
      namingConvention: "kebab-case",
      override: {
        useTypeOverInterfaces: true,
        fetch: {
          includeHttpResponseReturnType: false,
        },
        mutator: {
          path: "./src/api/orval-mutator/custom-fetch.ts",
          name: "fetchApi",
        },
        query: {
          version: 5,
          useQuery: true,
          useInfinite: true,
          useMutation: true,
          useSuspenseQuery: true,
          useSuspenseInfiniteQuery: true,
          useInfiniteQueryParam: "page",
        },

        operations: operationSpecificConfig,
      },
    },
    hooks: {
      afterAllFilesWrite: ["biome format --write --config-path config/biome.format.jsonc"],
    },
  },
});

Environment

System:
OS: Windows 11 10.0.26200
CPU: (16) x64 AMD Ryzen 7 6800HS Creator Edition
Memory: 2.15 GB / 13.69 GB

Expected behavior

Orval just ignore the sse endpoints and normally resolve others.

Actual behavior

Orval immediately throws error and exit.

OpenAPI document (minimal, if applicable)

{
  "openapi": "3.1.0",
  "info": {
    "title": "FastAPI",
    "version": "0.1.0"
  },
  "paths": {
    "/api/events/": {
      "post": {
        "tags": [
          "stream"
        ],
        "summary": "Sse Endpoint",
        "operationId": "sse_endpoint",
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "text/event-stream": {
                "itemSchema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "string"
                    },
                    "event": {
                      "type": "string"
                    },
                    "id": {
                      "type": "string"
                    },
                    "retry": {
                      "type": "integer",
                      "minimum": 0
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    openapiOpenAPI related issue

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions