3

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.

Evan
  • 5,975
  • 8
  • 34
  • 63
  • 1
    Do you have any knowledge to share about this topic ? I'm trying to implement an application with a NodeJS backend and I'm struggling to get how to integrate the actual business logic with the generate classes. Thanks, – Ancelot182 Nov 30 '21 at 10:28
  • as far as I know, this is a legitimate concern/issue with this tool and similar ones, leading this article to saying it can recommend the tool, but that it's NOT usable... lol https://techsparx.com/software-development/openapi/openapi-nodejs-servers.html. Our plan is to develop some workflow which will have to include moving and modifying files after code gen. Thinking we will just modify the generated files as little as possible – kmanzana Apr 03 '23 at 22:51

1 Answers1

0

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,
      ));
    }
  },
);
kmanzana
  • 1,138
  • 1
  • 13
  • 23