nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Update response body with properties to the generated OpenAPI Specification

Open nickspaargaren opened this issue 3 months ago • 0 comments

Describe the feature

Context

In addition to the response code "200" and description "OK" in the OpenAPI Specification I would love to see the return types of a handler as properties as well. I've included an example below.

Notes

  • For now, the OpenAPI Specification is available with an experimental config for now.
  • The OpenAPI specification is available on http://localhost:3000/_nitro/openapi.json

Documentation

  • https://swagger.io/docs/specification/describing-responses/#body

Technical details

The (hardcoded for now) commented out lines are what I like to see implemented:

function getPaths(): PathsObject {
  const paths: PathsObject = {};

  for (const h of handlersMeta) {
    const { route, parameters } = normalizeRoute(h.route);
    const tags = defaultTags(h.route);
    const method = (h.method || "get").toLowerCase();

    const item: PathItemObject = {
      [method]: <OperationObject>{
        tags,
        parameters,
        responses: {
          200: {
            description: "OK",
            // content: {
            //   "application/json": {
            //     schema: {
            //       properties: {
            //         success: {
            //           type: "boolean",
            //         },
            //       },
            //     },
            //   },
            // },
          },
        },
      },
    };

    if (paths[route] === undefined) {
      paths[route] = item;
    } else {
      Object.assign(paths[route], item);
    }
  }

  return paths;
}

File: https://github.com/unjs/nitro/blob/main/src/runtime/routes/openapi.ts

This results in an addition to Swagger with an Example Value as well: Screenshot 2024-03-17 at 19 29 21 http://localhost:3000/_nitro/swagger

Acceptance criteria

  • The properties, return object of an eventHandler should be included and displayed in:
    • The OpenAPI Specification
    • The Swagger OpenAPI Specification

Additional information

  • [X] Would you be willing to help implement this feature?

nickspaargaren avatar Mar 17 '24 18:03 nickspaargaren