Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model not found #36

Open
assisgui opened this issue Nov 27, 2018 · 4 comments
Open

Model not found #36

assisgui opened this issue Nov 27, 2018 · 4 comments

Comments

@assisgui
Copy link

Hello,

I've separate my models in many .ts files (all of them in a directory called models), each model in his .ts file.
when I start the server, only the models that are in first .ts file are charged.

There is any configuration to accept many files?


user.ts

@ApiModel({
    description : 'Class User Auth' ,
    name : 'UserAuth'
})
export class UserAuth {
    @ApiModelProperty({
        description : 'user email',
        required : true
    }) email: string;

    @ApiModelProperty({
        description : 'user password',
        required : false
    }) password: string;
}

@ApiModel({
    description : 'Class User' ,
    name : 'User'
})
export class User extends Typegoose{
    _id? : string;

    @ApiModelProperty({
        description : '',
        required : true
    })
    @prop({required : true})
    email: string;

    @ApiModelProperty({
        description : '',
        required : true
    })
    @prop({required : true})
    displayName: string;

    @ApiModelProperty({
        description : '',
        required : true
    })
    @prop({required : true})
    givenName: string;

    @ApiModelProperty({
        description : '',
        required : true
    })
    @prop({required : true})
    sn : boolean;

    @ApiModelProperty({
        description : '',
        required : false
    })
    @prop({required : false})
    groupName?: string;

    @ApiModelProperty({
        description : '',
        required : false
    })
    @prop({required : false})
    firstLogin? : boolean;

    @ApiModelProperty({
        description : '',
        required : false
    })
    @prop({required : false})
    lastLogin? : boolean;

    @ApiModelProperty({
        description : '',
        required : false
    })
    @prop({required : false})
    manager?: string;

    @ApiModelProperty({
        description : '',
        required : false,
        itemType : SwaggerDefinitionConstant.Model.Property.Type.STRING,
    })
    @arrayProp({itemsRef : {name : 'Permission'}})
    permissions?: Ref<Permission>[];
}

permission.ts

@ApiModel({
    description : 'Class Permission' ,
    name : 'Permission'
})
export class Permission extends Typegoose {
    _id?: string;

    @ApiModelProperty({
        description : '',
        required : true
    })
    @prop({required : true})
    name: string;
}

app.ts

this.app.use(swagger.express({
            definition : {
                info : {
                    title : 'Example' ,
                    version : '1.0'
                } ,
                externalDocs : {
                    url : 'localhost:3000/api-docs/swagger'
                },
                securityDefinitions : {
                    Bearer : {
                        type : 'apiKey',
                        in : 'header',
                        name : 'Authorization'
                    }
                },
                basePath: '/api/v3'
            }
        }));

image

Can you help me?

@bradeac
Copy link

bradeac commented Feb 20, 2019

I'm facing something similar. I have multiple .ts files in a folder, each containing a model.

Just one of the .ts files gets recognized as a model and appears in swagger.json.

Does anyone have a fix for this ?

Later edit:
A quick fix would be creating an index.ts file and exporting both files there:

import { component1} from './component1'
import { component2 } from './component2'

export { component1, component2 }

@Mantissa-23
Copy link

Bumping this- any resolution? Exporting from an index file didn't help.

@fhbonfante
Copy link

fhbonfante commented Nov 10, 2021

this problem seems to be intermittent and has some scenarios were it recognizes the models in different files and some times it did not.
We find a workaround by importing the files where the api models are declared and import it near the begnning of the server file (the file where you start your express server).
It is something like this:
swagger-models.registration.ts
// the file that will hold all models references

import 'path/to/file/with/api-model1.ts'
import 'path/to/file/with/api-model2.ts'
import 'path/to/file/with/api-model3.ts'
...

server.ts
// the file with your express server setup

import 'swagger-models.registration.ts'

I'm not sure if this is a good or secure solution, but seens to fix the issue. Any thoughts on this?

@assisgui
Copy link
Author

To be honest I started using [nestjs] (https://nestjs.com/) a long time ago.. A lot of these issues have been resolved haha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants