Digital-Tvilling / node-red-openapi-generator

A set of tools for generating OpenAPI documentation based on the HTTP nodes deployed in a flow
Apache License 2.0
1 stars 5 forks source link

Node-RED OpenAPI3 Documentation Generator

This package provides a set of tools for generating OpenAPI3 documentation based on the HTTP nodes deployed in a flow.

It is a fork of the https://github.com/node-red/node-red-node-swagger repo with some significant updates of dependencies and restructuring due to the OpenAPI3-update. The package is not a complete implementation of all features of OpenAPI3 but some basics to generate correct .json files and Swagger-UI details.

Usage

  1. Install the node module

  2. If you want to provide a template OpenAPI file in settings.js:

    openapi: {
      template: {
        openapi: "3.0.0",
        info: {
          title: "My Node-RED API",
          version: "0.0.1",
          description: "API documentation generated by Node-RED"
        },
        servers: [
          {
            url: "http://localhost:1880/",
            description: "Local server"
          }
        ],
        paths: {},
        components: {
          schemas: {},
          responses: {},
          parameters: {},
          securitySchemes: {}
          // Add other components here
        }
      }
    }
  3. This template will remain unchanged and serve as the basis for the OpenAPI documentation.

    Note: You may additionally add components like schemas, responses, parameters, securitySchemes, etc., to the OpenAPI file in settings.js to have those components available for reuse in the generated OpenAPI documentation.

    openapi: {
      template: {
        // ... (rest of your OpenAPI template)
      },
      components: {
        schemas: {
          MySchema: {
            type: "object",
            properties: {
              name: {
                type: "string"
              }
            }
          }
        },
        securitySchemes: {
          ApiKeyAuth: {
            type: "apiKey",
            in: "header",
            name: "X-API-Key"
          }
        }
        // Add other reusable components here
      }
    }
  4. After installing the package, you have the option to identify metadata for each HTTP-In node that will be used in the OpenAPI doc generation.

  5. The generated OpenAPI documentation is then available at http://localhost:1880/http-api/swagger.json.

Path OpenAPI Generation

Via the editor, you can define metadata for each particular HTTP-In node to be used in OpenAPI generation.

To do so:

  1. Select an HTTP-In node in the editor. HTTP-In Node

  2. From the config panel, you can select a user-defined OpenAPI doc from the dropdown. You may create a new metadata definition by selecting "Add new swagger-doc..." and clicking the edit button. Config Panel

  3. This will launch the OpenAPI config panel, where you have three distinct tabs that make up the OpenAPI documentation.

Info

Info Tab

This tab allows you to provide the basic information about the attached paths.

Parameters

Parameters Tab

This tab allows you to configure the parameters that can be used with the particular operation.

If a body parameter is selected, the user will provide properties included in the body object, rather than specifying a type.

Responses

Responses Tab

This tab allows you to define the applicable responses that a user may receive back from the operation.

If no responses are provided, a default response with the reply "success" will be used.

Request Body

This tab allows you to define the request body for the operation.



## Swagger-UI

![Swagger-UI](readme_images/Capture_Swagger_UI.PNG?raw=true)

Swagger-UI is included in the plugin. Once loaded, the plugin will show a Swagger tab in the Node-RED sidebar. From here, you can see the dynamically generated OpenAPI documentation for the current flow. Additionally, you can use the test function to try out your API directly from the editor, providing any parameters you have defined in the docs for the HTTP-In nodes. The Swagger-UI will automatically refresh any time the flow is redeployed.

## Notes

- The `paths` entry of the OpenAPI documentation is generated based on the `HTTP In` nodes present in the flow.
- If an OpenAPI template is not provided, the example above is used as the default.
- If `basePath` is not set in the template, it is set to the value of `httpNodeRoot` if that value is something other than `/`.

###### Attribute definitions provided come from the [OpenAPI Specification Version 3.1.0](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md)