backstage-plugins icon indicating copy to clipboard operation
backstage-plugins copied to clipboard

feat(ocm): add openApi schema and OpenApiRouter

Open dzemanov opened this issue 1 year ago • 1 comments

Description:

Introduces openapi schema for ocm-backend. Introduces autogeneration of openapi documentation via yarn build:api-docs Introduces openapi-utils dependency that adds:

  • A typed express router Some examples: ocm-1 ocm-2 (When openapi.yaml specifies endpoint /example that requires name query param.)
  • Validation and verification tooling For example when calling /example endpoint without name:
 curl -X GET http://localhost:7007/api/ocm/hello                                                                                                                          ─╯
{"error":{"name":"InputError","message":"request/query must have required property 'name'","stack":"InputError: 
...
2024-08-13T14:48:35.814Z backstage error Request failed with status 400 request/query must have required property 'name' type=errorHandler cause=undefined name=InputError stack=InputError: request/query must have required property 'name'
    at /home/dzemanov/Projects/janus/backstage-plugins/node_modules/@backstage/backend-openapi-utils/dist/index.cjs.js:23:10
    at handleReturn (/home/dzemanov/Projects/janus/backstage-plugins/node_modules/express-promise-router/lib/express-promise-router.js:24:23)
...
  • option to autogenerate client code like Apis via: yarn backstage-repo-tools package schema openapi generate client --client-package <directory> For ocm, this command could be run when inside ocm-backend folder: yarn backstage-repo-tools package schema openapi generate client --client-package plugins/ocm-common

  • this could be used to generate catalog entities with swagger openapi docs (haven't tried this one yet) https://www.npmjs.com/package/@backstage/plugin-catalog-backend-module-backstage-openapi

The openapi.generated.ts needs to be regenerated when openapi schema changes via running: yarn build:api-server . "@backstage/repo-tools"` must be installed in root workspace for it to run.

Reference: https://backstage.io/docs/openapi/01-getting-started/

Fixes:

RHIDP-3570

dzemanov avatar Aug 13 '24 17:08 dzemanov

One thing I would mention is that I didn't add security to openapi.yaml schema because that way openApiRouter checks that requests contain it and for dev development it was giving Error: Authorization header required.

Update: fixed

dzemanov avatar Aug 22 '24 16:08 dzemanov