moleculerjs / moleculer-web

:earth_africa: Official API Gateway service for Moleculer framework
http://moleculer.services/docs/moleculer-web.html
MIT License
291 stars 119 forks source link

Aliases for REST with mix of variables and fixed path names don't map properly #278

Closed synologic closed 2 years ago

synologic commented 2 years ago

Consider the following code:

API GW:

"use strict";

const _ = require("lodash");
const ApiGateway = require("moleculer-web");

module.exports = {
    name: "api",
    mixins: [ApiGateway],

    settings: {
        port: process.env.PORT || 3000,
        path : "/",

        routes: [
            {
                whitelist : ["test.*"],

                authorization: false,
                autoAliases: true,

                // Set CORS headers
                cors: true,

                // Parse body content
                bodyParsers: {
                    json: {
                        strict: false
                    },
                    urlencoded: {
                        extended: false
                    }
                },
                mappingPolicy: "all",
                logging: true
            }
        ]
    },

};

Service:

module.exports = {
    name : "test",
    actions : {
        func2 : {
            rest : "GET /fixed/:param1/:param2",
            visibility : "published",
            async handler(ctx) {
                return Promise.resolve("func2");
            }
        },

        func1 : {
            rest : "GET /:param1/:param2/:param3/:param4?",
            visibility : "published",
            async handler(ctx) {
                return Promise.resolve("func1");
            }
        }
    }
}

In the console, it looks that the mapping is right :

[2021-09-25T12:45:09.646Z] INFO  micro1-497396/API: ♻ Generate aliases for '/' route...
[2021-09-25T12:45:09.649Z] INFO  micro1-497396/API:      GET /test/fixed/:param1/:param2 => test.func2
[2021-09-25T12:45:09.649Z] INFO  micro1-497396/API:      GET /test/:param1/:param2/:param3/:param4? => test.func1

But when calling:

[2021-09-25T12:45:14.131Z] INFO  micro1-497396/API: => GET /test/fixed/param1/param2
[2021-09-25T12:45:14.132Z] DEBUG micro1-497396/API:   Alias:    GET /test/:param1/:param2/:param3/:param4? => test.func1
[2021-09-25T12:45:14.133Z] INFO  micro1-497396/API:    Call 'test.func1' action`

whenever a GET /test/fixed/param1/param2 is issued, func1 is hit instead of func2 Same happens if parameters is passed instead of REST and autoAlias is set to off and manual aliases are inserted in the apigw.

Any suggestions ?

icebob commented 2 years ago

Try to turn off the optimizeOrder: false in service settings.