main icon indicating copy to clipboard operation
main copied to clipboard

OpenAPI: support generating variants from examples in response schemas

Open bogi158 opened this issue 2 years ago • 4 comments

Describe the bug When an response is defined, if this response have a content, the route is not generated

To Reproduce Define a simple OpenAPI spec and add some content in a response. Then try to generate the code. You can just try with the Petstore API example.

Expected behavior Code should be generated normally with a route variant that has a response as required by the specs

Logs No logs are shown

** Operating system, Node.js an npm versions, or browser version (please complete the following information):**

  • OS: Ubuntu 18.04
  • Node.js: 16.20.0
  • npm: 8.19.4
  • Browser: Chrome

Additional context

This works:

// ....
paths: {
        '/info/version': {
          get: {
            operationId: 'version',
            tags: [
              'Info',
            ],
            summary: 'Server version',
            description: 'Returns the server version running',
            responses: {
              200: {
                description: 'Success',
              },
              400: {
                description: 'Error',
              },
            },
          },
        },
      },
// ....

This does not work:

// ....
paths: {
        '/info/version': {
          get: {
            operationId: 'version',
            tags: [
              'Info',
            ],
            summary: 'Server version',
            description: 'Returns the server version running',
            responses: {
              200: {
                description: 'Success',
                content: {
                  'application/json': {
                    schema: {
                      type: 'object',
                      properties: {
                        success: {
                          type: 'boolean',
                          example: true,
                        },
                      },
                    },
                  },
                },
              },
              400: {
                description: 'Error',
                content: {
                  'application/json': {
                    schema: {
                      type: 'object',
                      properties: {
                        success: {
                          type: 'boolean',
                          example: false,
                        },
                      },
                    },
                  },
                },
              },
            },
          },
        },
      },
// ....

Also this:

      paths: {
        '/info/version': {
          get: {
            operationId: 'version',
            tags: [
              'Info',
            ],
            summary: 'Server version',
            description: 'Returns the server version running',
            responses: {
              200: {
                // This schema has all the examples setup in all the properties
                $ref: '#/components/responses/VersionResponse',
              },
              400: {
                // This schema has all the examples setup in all the properties
                $ref: '#/components/responses/BadRequest',
              },
            },
          },
        },
      },

bogi158 avatar May 03 '23 12:05 bogi158

Hi @bogi158 , may this be related to #472 ? I mean, in your first example, I suppose that it generates responses without body, it just sends a status because it does not find any content, but in the other examples, as the responses contains content, it tries to use the examples property, which is not found.

The plugin currently only generates route variants from the content[MEDIA_TYPE].examples property, and you are providing examples, but for specific properties inside the schema of the response, which is not supported.

Anyway, this is something that very would be very useful too, so it probably deserves to be added in next releases. Thanks for reporting it!

Note: If you don't mind, I will rename the issue to "OpenAPI: support generating variants from examples in response schemas"

javierbrea avatar May 12 '23 18:05 javierbrea

Hi @bogi158 , may this be related to #472 ? I mean, in your first example, I suppose that it generates responses without body, it just sends a status because it does not find any content, but in the other examples, as the responses contains content, it tries to use the examples property, which is not found.

The plugin currently only generates route variants from the content[MEDIA_TYPE].examples property, and you are providing examples, but for specific properties inside the schema of the response, which is not supported.

Anyway, this is something that very would be very useful too, so it probably deserves to be added in next releases. Thanks for reporting it!

Note: If you don't mind, I will rename the issue to "OpenAPI: support generating variants from examples in response schemas"

Hello!

Yes indeed I have example on the response properties but I do not have a full example in content[MEDIA_TYPE].examples and I rarely add examples this way. It could be extremely useful to generate examples based on the response object example indeed. Hopefully I will see it in a future release.

Thank you!

bogi158 avatar May 15 '23 11:05 bogi158

@javierbrea

I ran into this same problem today and I think this would be indeed a very useful and wanted feature.

I added some extra functionality to the openapi plugin here locally, and it seems to be working for my case.

If you are interested, I can open a PR so we can hopefully have this feature available on a future release.

vonBrax avatar Mar 15 '24 10:03 vonBrax

OK, I went ahead and created a PR.

I'm just not sure what the target branch should be (currently: release), but let me know and I'll update the PR.

vonBrax avatar Mar 18 '24 07:03 vonBrax