Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
canassa opened this issue Oct 3, 2024 · 0 comments
Open

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

canassa opened this issue Oct 3, 2024 · 0 comments

Comments

@canassa
Copy link

canassa commented Oct 3, 2024

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant