swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.8k stars 6.02k forks source link

Custom template to add annotation for spring data #5799

Open ayushoo7 opened 7 years ago

ayushoo7 commented 7 years ago

In model class generated I want some annotation to be generated for it to persist in db using spring data. Used template but I can only put static code if want to introduce some dynamic param like {{#myanotation}} then can specify from properties or .yml file it would be great

java -jar swagger-codegen-cli-2.2.1.jar generate -i api.yml -c conf.json -l spring -o %TMP_SRC_DIR% -t C:\Users\template

api.yml --> yaml for apis as we as models %TMP_SRC_DIR% --> output directory any template folder had model.mustache and api.mustache which I want to modify

Scenario want to generate required set of files as dont want to change model classes manually

wing328 commented 7 years ago

@ayushoo7 do you mean you want to generate only certain properties defined in the Swagger/OpenAPI spec?

cbornet commented 7 years ago

I don't think you should use swagger to model your domain. Since you use spring data, you might want to have a look at jhipster (disclaimer : jhipster member talking) Note : Jhipster has swagger-ui/swagger-codegen integration

ayushoo7 commented 7 years ago

wing328 ,cbornet jhipster I gone through looks cool but we are using swagger for doc purpose I have .yml file that is for doc purpose but when using it to generate model class I want to annotate class as document as its static so able to get model annotated with documnet but not able to get fwe fields annotated as @id for id ones and @ dbref for refrential integrity ones but thats not poasible since swagger doesnt support such annotation .Please suggest .Also I need to have my API s to apport HAL the response entity need to be paged however this I can change manually but model classes are many and its complicated design of various entity.Please help me in this regard.

thomas-weinstein-bosch commented 6 years ago

I have a similar problem, we are using an API-first scenario, and we would like to add some annotations to the generated model (basically the same issue as the thread creator). In another thread the answer was to write own templates for this, but this would mean we would have a whole set of templates that we have to support for each new version upgrade, and we just dont have the capacity to do so in the project. So is there any other way, like having a new Vendor Extension, or some Plugin to add simple extensions like @Id or @Version to the generated model, other than modifying and supporting whole template structures?

wing328 commented 6 years ago

but this would mean we would have a whole set of templates that we have to support for each new version upgrade, and we just dont have the capacity to do so in the project.

@thomas-weinstein-bosch my suggestion is to give it a try. Other users told me the overhead is actually smaller than what they originally expected.

ayushoo7 commented 6 years ago

To solve my problem we have to write our own templates and as of now sticking to swagger-codegen-cli-2.2.1.But in future if we take latest jar it could be problematic.If swagger can support such annotation it will be help full to us. Thanks in advance.

thomas-weinstein-bosch commented 6 years ago

ok, thanks, so I will also give it a try.