openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

Design questions node-express-server

Open johandalabacka opened this issue 7 years ago • 4 comments

Description

This is not a bug report but more of a question about some design decision of the generated code

  1. The service class need to now about http result codes. I don't think a service should now anything about how it is used later. So it should only return data and perhaps throw an exception but it don't know if it is called from a rest-api controller, a cli or another service. But in the template the service functions should return data and http return codes

  2. Inside a service function do you create a promise

static someFunction({ someParameter }) {
    return new Promise(
      async (resolve) => {
        try {
          resolve(Service.successResponse(''));
        } catch (e) {
          resolve(Service.rejectResponse(
            e.message || 'Invalid input',
            e.status || 405,
          ));
        }
      },
    );
  }

why not the much simpler declaring the function as async?

static async someFunction({ someParameter }) {
  try {
    return Service.successResponse('');
  } catch (e) {
    return Service.rejectResponse(
       e.message || 'Invalid input',
       e.status || 405,
    );
  }
}

or am I missing something obvious?

openapi-generator version

4.2 (but 5.0 branch is similar)

johandalabacka avatar Nov 20 '19 08:11 johandalabacka

@johandalabacka thanks for the question.

cc @YishTish (creator of the NodeJS express generator)

wing328 avatar Nov 20 '19 08:11 wing328

@johandalabacka - thanks for pointing this out.

I think I should have done in differently - whenever I write a Promise, I make sure to return either a resolve or a reject, so whoever uses the promise knows what type of response is coming back. It seems like I forgot to declare and use a reject statement.

Will fix in next round.

Thanks!

YishTish avatar Nov 20 '19 15:11 YishTish

But what about my first question about a service and it's role? I don't really think it is the service's role to decide which http result code should be sent back to the client.

With kind regards,

Johan

johandalabacka avatar Nov 20 '19 16:11 johandalabacka

Yes, I've been debating that question myself for a while... Here's the thing - the controller can't send much. If the structure of the request doesn't comply with the OpenAPI contract, it gets rejected before hitting my code. The next place where the request can fail is in the service level - does the data exist? does it conflict with existing data? Maybe we lost connection with the database? All sorts of surprises await for us there. So we want to gracefully handle rejection from the service, and then send it back to the requestor. In a pure mode, the service would send back the rejection, and the controller would translate that rejection to a legal HTTP response, giving it the appropriate relevant code. But this requires that we have a language that exists between service and controller, only for the sake of giving the controller the ownership over the status codes. But I came half-way - the controller doesn't require a status code from the service. If no code is sent, the controller returns 200 for success and 500 for failure.

YishTish avatar Nov 21 '19 15:11 YishTish