Skip to content

[openapi-types] OperationObject cannot be assigned to PathItem #910

@canassa

Description

@canassa

I am unable to assign an OperationObject to a PathItem. This is a minimal reproducible example:

import type { OpenAPIV3_1 } from 'openapi-types'

let schema: OpenAPIV3_1.Document = {
    openapi: '3.1.0',
    info: {
        title: 'Test',
        version: '1.0.0',
    },
    paths: {},
}

schema.paths['/hello'] = {}
schema.paths['/hello2'] = {}

// Works
schema.paths['/hello2']['get'] = {
    responses: {
        200: {
            description: 'Hello World',
        },
    },
}

const operation: OpenAPIV3_1.OperationObject = {
    responses: {
        200: {
            description: 'Hello World',
        },
    },
}

// Doesn't work
schema.paths['/hello']['get'] = operation

Notice that if I don’t type cast the operation to OperationObject, the code works. However, when I cast it, it fails with the following error:

test.ts:33:1 - error TS2322: Type 'OperationObject<{}>' is not assignable to type '({ tags?: string[] | undefined; summary?: string | undefined; description?: string | undefined; externalDocs?: ExternalDocumentationObject | undefined; ... 7 more ...; servers?: ServerObject[] | undefined; } & Omit<...> & { ...; }) | undefined'.
  Type 'OperationObject<{}>' is not assignable to type '{ tags?: string[] | undefined; summary?: string | undefined; description?: string | undefined; externalDocs?: ExternalDocumentationObject | undefined; ... 7 more ...; servers?: ServerObject[] | undefined; } & Omit<...> & { ...; }'.
    Type 'OperationObject<{}>' is not assignable to type '{ tags?: string[] | undefined; summary?: string | undefined; description?: string | undefined; externalDocs?: ExternalDocumentationObject | undefined; ... 7 more ...; servers?: ServerObject[] | undefined; }'.
      Types of property 'requestBody' are incompatible.
        Type 'import("project/node_modules/openapi-types/dist/index").OpenAPIV3_1.ReferenceObject | import("project/node_modules/openapi-types/dist/index").OpenAPIV3_1.RequestBodyObje...' is not assignable to type 'import("project/node_modules/openapi-types/dist/index").OpenAPIV3.ReferenceObject | import("project/node_modules/openapi-types/dist/index").OpenAPIV3.RequestBodyObject |...'.
          Type 'RequestBodyObject' is not assignable to type 'ReferenceObject | RequestBodyObject | undefined'.
            Type 'import("project/node_modules/openapi-types/dist/index").OpenAPIV3_1.RequestBodyObject' is not assignable to type 'import("project/node_modules/openapi-types/dist/index").OpenAPIV3.RequestBodyObject'.
              Types of property 'content' are incompatible.
                Type '{ [media: string]: import("project/node_modules/openapi-types/dist/index").OpenAPIV3_1.MediaTypeObject; }' is not assignable to type '{ [media: string]: import("project/node_modules/openapi-types/dist/index").OpenAPIV3.MediaTypeObject; }'.
                  'string' index signatures are incompatible.
                    Type 'import("project/node_modules/openapi-types/dist/index").OpenAPIV3_1.MediaTypeObject' is not assignable to type 'import("project/node_modules/openapi-types/dist/index").OpenAPIV3.MediaTypeObject'.
                      Types of property 'schema' are incompatible.
                        Type 'import("project/node_modules/openapi-types/dist/index").OpenAPIV3_1.ReferenceObject | import("project/node_modules/openapi-types/dist/index").OpenAPIV3_1.SchemaObject | ...' is not assignable to type 'import("project/node_modules/openapi-types/dist/index").OpenAPIV3.ReferenceObject | import("project/node_modules/openapi-types/dist/index").OpenAPIV3.SchemaObject | unde...'.
                          Type 'ArraySchemaObject' is not assignable to type 'ReferenceObject | SchemaObject | undefined'.
                            Type 'import("project/node_modules/openapi-types/dist/index").OpenAPIV3_1.ArraySchemaObject' is not assignable to type 'import("project/node_modules/openapi-types/dist/index").OpenAPIV3.ArraySchemaObject'.
                              Types of property 'items' are incompatible.
                                Type 'import("project/node_modules/openapi-types/dist/index").OpenAPIV3_1.ReferenceObject | import("project/node_modules/openapi-types/dist/index").OpenAPIV3_1.SchemaObject' is not assignable to type 'import("project/node_modules/openapi-types/dist/index").OpenAPIV3.ReferenceObject | import("project/node_modules/openapi-types/dist/index").OpenAPIV3.SchemaObject'.
                                  Type 'ArraySchemaObject' is not assignable to type 'ReferenceObject | SchemaObject'.

33 schema.paths['/hello']['get'] = operation
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The error is somewhat hard to read, but I believe the relevant part is:

Type 'OpenAPIV3_1.ReferenceObject OpenAPIV3_1.RequestBodyObje' is not assignable to type 'OpenAPIV3.ReferenceObject | OpenAPIV3.RequestBodyObject |...'.

It seems to think that my Document is a 3.0 API spec environment, even though it was defined as a 3.1 document.

The example works with a 3.0 document:

import type { OpenAPIV3_1, OpenAPIV3 } from 'openapi-types'

let schema: OpenAPIV3.Document = {
    openapi: '3.1.0',
    info: {
        title: 'Test',
        version: '1.0.0',
    },
    paths: {},
}

schema.paths['/hello'] = {}

const operation: OpenAPIV3.OperationObject = {
    responses: {
        200: {
            description: 'Hello World',
        },
    },
}

schema.paths['/hello']['get'] = operation

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions