Use swagger codegen cli to generate Node.js server

2019-09-20 04:27发布

问题:

I have a swagger.yaml and just installed the swagger-codegen on my Mac. I want to generate a Node.js server including the models described in the swagger.yaml from the command line. I tried:

java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i swagger.yaml -l nodejs -o export/nodejs -Dmodels

and

java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i swagger.yaml -l nodejs -o export/nodejs -Dmodels -DsupportingFiles

But the output is always the same WITHOUT the models:

What is wrong?

回答1:

As opposed to previous versions of swagger-node (fka swagger-node-express), the swagger definition is used as the source of truth. There is no model file generated for each model in the application. So the -Dmodels will have no effect.

You can generate the response payload and send it back, and even configure the framework to validate that it follows the definition.