Tsoa: Error, Multiple matching models found

Created on 12 Mar 2017  路  12Comments  路  Source: lukeautry/tsoa

I have this error when generating routes or swagger

There was a problem resolving type of 'FindForLocationResponse'.
Error: Multiple matching models found for referenced type FindForLocationResponse; please make model names unique.

It's true, i have two interfaces with the same name, but they are in different packages,
one is used internally by the service,
and the other in the controller (for tsoa router and swagger generation)

Is there a way to fis this error other then rename the interface ?

bug in progress workaround available

Most helpful comment

Check out the latest release (2.0.1+). While there isn't a true fix, there's now a way to ignore directories for TypeScript scanning, e.g. in tsoa.json:

{
  "swagger": {
       ...
  },
  "routes": {
      ...
  },
  "ignore": [
    "**/node_modules/**"
  ]
}

My guess is that most folks only want to expose domain-specific models, not interfaces/classes from external libs. That may not be totally true. In any case, this will help you resolve the above issue.

Let me know what you think.

All 12 comments

@myflowpl I had the same issue with Account model. And the only way to workaround it was renaming the model.
The tricky thing for me was that i had only 1 Account in the project, but debugging helped me to figure out that the duplication happened because of Account model in TypeScript library.

Issue happens because TSOA uses TypeScript's Program.getSourceFiles() to generate metadata, which gives all the source files involved, including libraries.
see https://github.com/lukeautry/tsoa/blob/master/src/metadataGeneration/metadataGenerator.ts#L21
And when TSOA is trying to resolve model type and it finds more than 1 with the same name - it throws an error https://github.com/lukeautry/tsoa/blob/master/src/metadataGeneration/resolveType.ts#L304

I don't see an easy way to resolve this issue.

@amozh thanks for deep explanation of the issue.
I also resolved the problem by renaming the model, right now as a good practice, i call all my models with suffix Model

Location => LocationModel LocationModelRequest, LocationModelResponse

After some time, when the model list got quite big, i found it easier to maintain and read the project structure with suffixes, so it was god workaround for me.

I am also getting with @typings

Error: Multiple matching models found for referenced type Response; please make model names unique. Conflicts found: "/usr/local/lib/node_modules/tsoa/node_modules/typescript/lib/lib.d.ts"; "/Users/xxx/node_modules/@types/express-serve-static-core/index.d.ts".

How to fix this?

@isman-usoh I noticed you tagged this in progress - did you have a solution in the works?

I'm running into the same issue as @Cidaas above, "Multiple matching models found" for basic typing definitions (String in this case). I had tsoa package version 1.3.3 but removed and replaced it with version 2.0.1 but still see the same error. Can anyone share suggestions for a workaround, please?

@isman-usoh

There was a problem resolving type of 'String'.
Error: Multiple matching models found for referenced type String; please make model names unique.
Conflicts found: "/mnt/s/Development/complex-reserve-tsoa/node_modules/typescript/lib/lib.d.ts"; "/mnt/s/Development/complex-reserve-tsoa/node_modules/typescript/lib/lib.d.ts".

Check out the latest release (2.0.1+). While there isn't a true fix, there's now a way to ignore directories for TypeScript scanning, e.g. in tsoa.json:

{
  "swagger": {
       ...
  },
  "routes": {
      ...
  },
  "ignore": [
    "**/node_modules/**"
  ]
}

My guess is that most folks only want to expose domain-specific models, not interfaces/classes from external libs. That may not be totally true. In any case, this will help you resolve the above issue.

Let me know what you think.

@lukeautry Thanks for the update. That is a step closer and may fully fix the issue for some. In my case I have another bug that has came up, probably need it's own ticket - the error is regarding use of String types in my models. Looks like we need to add support for String Type in resolveType.js.

Error: No matching model found for referenced type String.

This is thrown from src/metadataGeneration/resolveType.ts (my models have String types in them). This issue is similar to the Buffer issue reported here: https://github.com/lukeautry/tsoa/issues/38.

I appear to have solved the issue by mapping String to string in resolveType.ts around line 76. Is this the best solution?

    if (typeReference.typeName.text === 'String') {
      return { dataType: 'string' } as Tsoa.Type;
    }

In the mean time I've forked the module for my own use with this fix, but I think this should be handled in tsoa. Edit - I've submitted PR #172

I am also getting error for Request object

Error: Multiple matching models found for referenced type Request; please make model names 
unique. Conflicts found: 
"/usr/local/lib/node_modules/tsoa/node_modules/typescript/lib/lib.d.ts";
 "/Users/../Projects/.../node_modules/@types/express-serve-static-core/index.d.ts".

My config file. I am using "tsoa": "^2.0.1"

{
    "swagger": {
        "outputDirectory": "./dist",
        "entryFile": "./src/Routers/entryfile.ts",
        "basePath": "/v1",
        "name": "vimal",
        "description": "vimal"
    },
    "ignore": [
        "**/node_modules/**"
    ]
}

See the @tsoamodel feature (new): https://github.com/lukeautry/tsoa#dealing-with-duplicate-model-names

@lukeautry

Hi, I saw your feature is only to resolve the same model name, but how can i resolve the same namespace definition in different files ?

Thanks in advance.

Any solution to this? I've tried ignoring the paths in the tsoa config as well as using the @tsoaModel decorator.

UnhandledPromiseRejectionWarning: Error: Multiple matching models found for referenced type Model; please make model names unique. Conflicts found: "/Users/..../node_modules/sequelize/types/lib/model.d.ts"; "/Users/...../node_modules/sequelize-typescript/dist/model/model/model.d.ts".

See the @tsoamodel feature (new): https://github.com/lukeautry/tsoa#dealing-with-duplicate-model-names

Link moved to:
https://tsoa-community.github.io/docs/faq.html#dealing-with-duplicate-model-names

Was this page helpful?
0 / 5 - 0 ratings