Skip to content

fix: import statements should not affect the route matching order  #1307

Open
@tillkolter

Description

@tillkolter

Description

We found in our project that a different order of Controller imports produces different results for a catch-all 404 route which blocks controllers imported earlier in the file.

A canonical sorting of imports is in my opinion common practice and it is not intuitive that the order matters in this case. Shouldn't the matching preference depend only on factors such as longest path and order inside the initializers controllers array?

Minimal code-snippet showcasing the problem

controllers/NotFoundController.ts

import { Controller, NotFoundError, All } from "routing-controllers";

@Controller()
export class NotFoundController {
  @All("/*")
  catchAll() {
    throw new NotFoundError("This route does not exist");
  }
}

controllers/ApplesController.ts

@JsonController("/apples")
export class ApplesController {
  @Get("/")
  async Apples(
     return JSON.stringify(["Granny Smith", "Braeburn"])
  )
}

app.ts

import { NotFoundController } from "./controllers/NotFoundController";
import { ApplesController } from "./controllers/ApplesController";

export const app = createExpressServer({
  controllers: [
    ApplesController,
    NotFoundController,
  ]
});

Expected behavior

GET http://localhost:3000/apples

["Granny Smith", "Braeburn"]

Actual behavior

GET http://localhost:3000/apples

404 error

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: needs triageIssues which needs to be reproduced to be verified report.type: fixIssues describing a broken feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions