api icon indicating copy to clipboard operation
api copied to clipboard

Bug: `PageDto` has no `items` type in swagger spec

Open jpmcb opened this issue 10 months ago • 0 comments

Describe the bug

I've been heads down on https://github.com/open-sauced/pizza-cli/issues/28 and have been primarily investigating generating Go clients using the OpenAPI generator.

My hubris became humility in the face of some of the problems faced with this kind complex code generation.

There's one error during code generation that I'm unsure how to approach and seems to be the biggest problem with our spec:

-attribute components.schemas.PageDto.items is missing

During validation of the swagger spec, this throws an error and won't proceed due to the PageDto's abstract nature. I.e., since the PageDto array items are not explicitly typed during runtime, validation fails.

We can see that here:

https://github.com/open-sauced/api/blob/f1ff35e5072b1226065b425f60e655291a9b5cef/swagger.yaml#L4603-L4614

array is not a valid type for the items of the array in the PageDto.

I thought at first that I would create a mix-in for the PageDto type that would dynamically generate the types at runtime for each of the paged entities. Something like:

function generatePageDto<T>(TClass: new () => T): any {
    class ConcretePageDto extends PageDto<T> {
        @ApiProperty({ type: () => TClass, isArray: true })
        data: T[];
        
        @ApiProperty({ type: () => PageMetaDto })
        meta: PageMetaDto;
        
        constructor(data: T[], meta: PageMetaDto) {
            super();
            this.data = data;
            this.meta = meta;
        }
    }
    return ConcretePageDto;
}

But this led to alot of boilerplate when generating paged dtos and it still didn't solve the problem (among also complaining about any). So in short, after a few hours, I abandoned this journey in favor of doing --skip-validate-spec during generation which still generates a good enough Go client.

Maybe there's something obvious I'm missing with the @nestjs/swagger decorators?

I had also considered bumping the @nestjs/swagger NPM package to v7 and seeing if that just worked but didn't want that to conflict with #nest

Steps to reproduce

  1. Run the following docker command to execute code generation:
docker run --rm \
    -v $PWD:/local openapitools/openapi-generator-cli generate \
    -i /local/swagger.yaml \
    -g go \
    -o /local/out/go \
  1. Notice the following error during spec validation:
-attribute components.schemas.PageDto.items is missing

Browsers

No response

Additional context (Is this in dev or production?)

No response

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

Contributing Docs

  • [X] I agree to follow this project's Contribution Docs

jpmcb avatar Aug 21 '23 22:08 jpmcb