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: support generating variants from examples in response schemas #471

Open
bogi158 opened this issue May 3, 2023 · 4 comments
Open
Labels
enhancement New feature or request

Comments

@bogi158
Copy link

bogi158 commented May 3, 2023

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 bogi158 changed the title Mock generation with OpenAPI does not work if error responses have a body Mock generation with OpenAPI does not work if error responses a body May 3, 2023
@bogi158 bogi158 changed the title Mock generation with OpenAPI does not work if error responses a body Mock generation with OpenAPI does not work if responses have a body May 12, 2023
@javierbrea
Copy link
Member

javierbrea commented May 12, 2023

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 javierbrea added the enhancement New feature or request label May 12, 2023
@javierbrea javierbrea changed the title Mock generation with OpenAPI does not work if responses have a body OpenAPI: support generating variants from examples in response schemas May 12, 2023
@bogi158
Copy link
Author

bogi158 commented May 15, 2023

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!

@vonBrax
Copy link

vonBrax commented Mar 15, 2024

@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
Copy link

vonBrax commented Mar 18, 2024

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.

@javierbrea javierbrea moved this to To do in Backlog May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: To do
Development

No branches or pull requests

3 participants