Swagger-codegen: -Dapis option generates models for all api's

Created on 22 Nov 2016  路  9Comments  路  Source: swagger-api/swagger-codegen

When setting the -Dapis parameter (along with -Dmodels and others), models get created for all API's. I would expect it to just create the models solely associated to the API's being generated rather than the models for all APIs.
Using swagger-codegen version 2.2.1

Question

Most helpful comment

@jimschubert I'm passing -Dapis=AnAPI -Dmodels -DsupportingFiles. So I'm specifying that I want specific endpoints and want everything created. But I would think that from that the generator can figure out which models are actually necessary for those endpoints and only generate those or at least only output those relevant models.. It doesn't logistically make sense if I'm "filtering" on APIs to show all models if they aren't pertinent. Otherwise you can end up with a ton of extra models that provide no value in context of the generated API documentation.

All 9 comments

That's not the current behaviour. -Dapis will generate the API classes/files while -Dmodels will generate the model files. These 2 options work independently.

Can you tell us more about your use cases? Do you want to skip certain files (models, apis) during code generation?

Right, but if I limit the API's to specific ones to generate, I would think the models generated would only relate to the API's created.
So for example, if I have 2 api's - A and B. A has a User model and B has a Car model. If I generate the A api, I would expect only the User model to be created and not the Car model. And vice versa if I generate the other api. Only if I generate A and B would I expect to see both the User and Car models. Does that make sense?

If those models are not referenced/used by the endpoints, why not remove those from the spec?

This particular spec contains the API endpoints for the entire application. Had thought of splitting them out into separate files, but this particular app isn't too large, so splitting them out seemed like overkill. And since the codegen project allows filtering using the -D parameters, it seemed worthwhile to use those.

@atdiff are you just passing -Dapis -Dmodels? These parameters without values mean "generate all apis" and "generate all models". If you want the apis and models limited to certain types, you have to pass the type names such as -Dapis=A -Dmodels=AModel,ASubModel. There's not currently any model association per API.

Here's the relevant code: https://github.com/swagger-api/swagger-codegen/blob/v2.2.1/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java#L59

I'd think that changing -Dapis and -Dmodels to create a subset of models based on the selected APIs would be a pretty significant breaking change. If this is picked up as a new feature, it should probably be an addition switch.

@atdiff given that you don't want to remove unused models from the spec, another way is to leverage .swagger-codegen-ignore to skip certain files during code generation: https://github.com/swagger-api/swagger-codegen/wiki/FAQ#how-to-skip-certain-files-during-code-generation

@jimschubert I'm passing -Dapis=AnAPI -Dmodels -DsupportingFiles. So I'm specifying that I want specific endpoints and want everything created. But I would think that from that the generator can figure out which models are actually necessary for those endpoints and only generate those or at least only output those relevant models.. It doesn't logistically make sense if I'm "filtering" on APIs to show all models if they aren't pertinent. Otherwise you can end up with a ton of extra models that provide no value in context of the generated API documentation.

@wing328 thanks for the info but I don't think that would apply in this case since what we would skip could change each time. Sometimes it might models for one endpoint, the next time it could be a different one and so on. So the ignore file would have to change every time which seems like more of a hassle and not very adaptable and dynamic.

Yes, that's not the intended behavior. The -Dapis and -Dmodels are for filtering away, or including _only_ specific values. By the time that filtering happens, it's too late for dependency reduction.

Was this page helpful?
0 / 5 - 0 ratings