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

Swagger lack of some meta parameters #922

Closed
robertstrz opened this issue May 20, 2024 · 5 comments
Closed

Swagger lack of some meta parameters #922

robertstrz opened this issue May 20, 2024 · 5 comments

Comments

@robertstrz
Copy link

Hi,
I am wondering why Swagger is missing some of the parameters from meta property. In decorator there are only 2 properties in meta object defined. I am using OpenAPI generator to generate client so those are missing in a client definition.

    meta: {
        itemsPerPage: number
        totalItems: number
        currentPage: number
        totalPages: number
        sortBy: SortBy<T>
        searchBy: Column<T>[]
        search: string
        select: string[]
        filter?: {
            [column: string]: string | string[]
        }
    }

Swagger decorator

       ApiExtraModels(PaginatedDocumented, dataDto),
       ApiOkResponse({
           schema: {
               allOf: [
                   { $ref: getSchemaPath(PaginatedDocumented) },
                   {
                       properties: {
                           data: {
                               type: 'array',
                               items: { $ref: getSchemaPath(dataDto) },
                           },
                           meta: {
                               properties: {
                                   select: {
                                       type: 'array',
                                       items: {
                                           type: 'string',
                                           enum: paginatedConfig?.select,
                                       },
                                   },
                                   filter: {
                                       type: 'object',
                                       properties: Object.keys(cols).reduce(
                                           (acc, key) => {
                                               acc[key] = {
                                                   oneOf: [
                                                       {
                                                           type: 'string',
                                                       },
                                                       {
                                                           type: 'array',
                                                           items: {
                                                               type: 'string',
                                                           },
                                                       },
                                                   ],
                                               }
                                               return acc
                                           },
                                           {} as Record<string, SchemaObject | ReferenceObject>
                                       ),
                                   },
                               },
                           },
                       },
                   },
               ],```
@Helveg
Copy link
Collaborator

Helveg commented May 20, 2024

I'm not sure how that decorator is supposed to work, but there seems to be additional properties in the { $ref: getSchemaPath(PaginatedDocumented) } part, there is a PaginatedMetaDocumented with all the meta properties. Can you show with a reproducer when these meta properties are missing?

@robertstrz
Copy link
Author

Sure,
so as I mentionted I am generating client with openapi-tools

import { OpenApiNestFactory } from 'nest-openapi-tools'
...

		await OpenApiNestFactory.configure(
			app,
			new DocumentBuilder()
				.setTitle('Core API')
				.addServer('NEXT_PUBLIC_BACKED_API_URL', 'staging', {
					variable: { default: '3.0' },
				})
				.addBearerAuth(),
			{
				webServerOptions: {
					enabled: true,
					path: 'docs',
				},
				fileGeneratorOptions: {
					enabled: true,
					outputFilePath: './openapi.yaml', // or ./openapi.json
				},
				clientGeneratorOptions: {
					enabled: false,
					type: 'typescript-fetch',
					outputFolderPath: '../../web/typescript-api-client/src',
					additionalProperties:
						'apiPackage=clients,modelPackage=models,withoutPrefixEnums=true,withSeparateModelsAndApi=true',
					openApiFilePath: './openapi.yaml',
					skipValidation: true, // optional, false by default
				},
			},
			{
				operationIdFactory: (c: string, method: string) => method,
			},
		)

If I open swagger documentation in browser then it shows that indeed totalItems is in response

image

But when I am generating client from openapi.yaml some properties are missing from generated client and only 2 of them are shown filter and select.

Quick workaround for that for me right now is that I have taken your decorator and added missing property by hand

	return applyDecorators(
		ApiExtraModels(PaginatedDocumented, dataDto),
		ApiOkResponse({
			schema: {
				allOf: [
					{ $ref: getSchemaPath(PaginatedDocumented) },
					{
						properties: {
							data: {
								type: 'array',
								items: { $ref: getSchemaPath(dataDto) },
							},
							meta: {
								properties: {
									select: {
										type: 'array',
										items: {
											type: 'string',
											enum: paginatedConfig?.select,
										},
									},
									totalItems: {
										type: 'number',
									},
									filter: {
										type: 'object',
										properties: Object.keys(cols).reduce(
											(acc, key) => {
												acc[key] = {
													oneOf: [
														{
															type: 'string',
														},
														{
															type: 'array',
															items: {
																type: 'string',
															},
														},
													],
												}
												return acc
											},
											{} as Record<string, SchemaObject | ReferenceObject>,
										),
									},
								},
							},
						},
					},
				],
			},
		}),
	)

Below is part of openapi.yaml that is generated automatically when decorator from you package is used. Based on this file I am generating typescript-fetch client.

      responses:
        "200":
          description: ""
          content:
            application/json:
              schema:
                allOf:
                  - $ref: "#/components/schemas/PaginatedDocumented"
                  - properties:
                      data:
                        type: array
                        items:
                          $ref: "#/components/schemas/MyEntity"
                      meta:
                        properties:
                          select:
                            type: array
                            items:
                              type: string
                          filter:
                            type: object
                            properties:
                              name:
                                oneOf:
                                  - type: string
                                  - type: array
                                    items:
                                      type: string

@Helveg
Copy link
Collaborator

Helveg commented May 21, 2024

Hmm thanks for you detailed report. Do you think you could create a quick repository with some steps for me to follow to generate the same things you're speaking of? That way I can follow the steps and make changes to arrive at the same outcome as your workaround!

@vsamofal what do you think is going wrong here?

@vsamofal
Copy link
Collaborator

I didn't get an issue tbh, as far as I understand swagger UI looks ok, but your code that exports Swagger and generates a client, doesn't generate a proper one. ?

if yes, can you download a spec file from swagger UI and compare it

@robertstrz

@Helveg
Copy link
Collaborator

Helveg commented Jun 5, 2024

Closing due to lack of reproducer and author response. Feel free to reopen so we can pick this back up :)

@Helveg Helveg closed this as completed Jun 5, 2024
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

3 participants