Writing Node business logic behind openapi-generator-cli nodejs-express-server

1.3k Views Asked by At

Currently we are doing the following:

Using an openapitools.json that looks like the following:

{
  "$schema": "node_modules/@openapitools/openapi-generator-cli/config.schema.json",
  "spaces": 2,
  "generator-cli": {
    "version": "5.1.1",
    "generators": {
      "node_v1.0": {
        "generatorName": "nodejs-express-server",
        "output": "#{cwd}/api/gen/v1.0/#{name}",
        "glob": "openapi/*.yaml"
      }
    }
  }
}

And we run a command:

npx openapi-generator-cli generate

Which creates this gen dir which structure like:

- api
- controllers
- services
- utils

etc.

Currently, our solution for business and database logic is to go to the controllers/DefaultController.js and change the require link for Service to point to our actual src/business_logic/Service.js code. In our Service.js code we call other logic in the endpoints like for example:

static getMaterials() {
    const payload = MaterialLogic.materialFetchAll();
    const code = 200;
    return { payload, code };
}

For reference, the generated code look like this, but you can see there is no logic:

/**
* Returns list of materials
*
* returns List
* */
const getMaterials = () => new Promise(
  async (resolve, reject) => {
    try {
      resolve(Service.successResponse({
      }));
    } catch (e) {
      reject(Service.rejectResponse(
        e.message || 'Invalid input',
        e.status || 405,
      ));
    }
  },
);

The OpenAPI spec for this looks like:

/materials:
  get:
    operationId: getMaterials
    description: Returns list of materials
    responses:
      200:
        description: list of Materials
        content:
          application/json:
            schema:
              type: array
              items:
                $ref: '#/components/schemas/Material'
      401:
        description: not authorized to use this endpoint

It's obviously the wrong approach for us to have to change any code in gen to make our application work as it needs to be overridden and generated at any time (hence the whole point of using openapi gen tools). What's the correct way to do this? In our openapi spec is there a way to specify a different Service.js to use? Or is there some kind of dependency injection we are missing to get our business/database logic in there? Hopefully our problem is clear enough.

1

There are 1 best solutions below

0
On

This file and whole sample server can provide some help: https://github.com/OpenAPITools/openapi-generator/blob/master/samples/server/petstore/nodejs-express-server/services/PetService.js

Notice the service file is changed to pass through necessary params:

const addPet = ({ body }) => new Promise(
  async (resolve, reject) => {
    try {
      resolve(Service.successResponse({
        body,
      }));
    } catch (e) {
      reject(Service.rejectResponse(
        e.message || 'Invalid input',
        e.status || 405,
      ));
    }
  },
);