0

如何使用 OAS 2.0 生成 nodejs-server 作为 openapi-generator 的输入?

openapi-generator 在 3.3.4 和 4.0.0 两个版本上运行。我的执行结果如下所示。

■在 3.3.4 中

java -jar openapi-generator-cli-3.3.4.jar generate -i petstore.json -g nodejs-server -o stub
[main] WARN  o.o.c.ignore.CodegenIgnoreProcessor - Output directory does not exist, or is inaccessible. No file (.openapi-generator-ignore) will be evaluated.
[main] WARN  o.o.c.languages.NodeJSServerCodegen -

=======================================================================================
Currently, Node.js server doesn't work as its dependency doesn't support OpenAPI Spec3.
For further details, see https://github.com/OpenAPITools/openapi-generator/issues/34
=======================================================================================


[main] INFO  o.o.codegen.DefaultGenerator - Model Pets not generated since it's an alias to array (without property)
Exception in thread "main" java.lang.RuntimeException: Could not generate api file for 'Pets'
        at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:651)
        at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:891)
        at org.openapitools.codegen.cmd.Generate.run(Generate.java:355)
        at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:62)
Caused by: java.lang.IllegalArgumentException: character to be escaped is missing
        at java.util.regex.Matcher.appendReplacement(Matcher.java:809)
        at java.util.regex.Matcher.replaceAll(Matcher.java:955)
        at java.lang.String.replaceAll(String.java:2223)
        at org.openapitools.codegen.languages.NodeJSServerCodegen.apiFilename(NodeJSServerCodegen.java:192)
        at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:595)
        ... 3 more

■在4.0.0

java -jar openapi-generator-cli-4.0.0.jar generate -i petstore.json -g nodejs-server -o stub
[main] WARN  o.o.c.ignore.CodegenIgnoreProcessor - Output directory does not exist, or is inaccessible. No file (.openapi-generator-ignore) will be evaluated.
[main] INFO  o.o.codegen.DefaultGenerator - OpenAPI Generator: nodejs-server (server)
[main] INFO  o.o.codegen.DefaultGenerator - Generator 'nodejs-server' is considered stable.
[main] WARN  o.o.c.languages.NodeJSServerCodegen -

=======================================================================================
Currently, Node.js server doesn't work as its dependency doesn't support OpenAPI Spec3.
For further details, see https://github.com/OpenAPITools/openapi-generator/issues/34
=======================================================================================


[main] INFO  o.o.codegen.DefaultGenerator - Model Pets not generated since it's an alias to array (without property) and `generateAliasAsModel` is set to false (default)
Exception in thread "main" java.lang.RuntimeException: Could not generate api file for 'Pets'
        at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:666)
        at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:922)
        at org.openapitools.codegen.cmd.Generate.run(Generate.java:396)
        at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:60)
Caused by: java.lang.IllegalArgumentException: character to be escaped is missing
        at java.util.regex.Matcher.appendReplacement(Matcher.java:809)
        at java.util.regex.Matcher.replaceAll(Matcher.java:955)
        at java.lang.String.replaceAll(String.java:2223)
        at org.openapitools.codegen.languages.NodeJSServerCodegen.apiFilename(NodeJSServerCodegen.java:181)
        at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:611)
        ... 3 more

输入 OAS 使用以下内容。

https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v2.0/json/petstore.json

执行环境如下。

・windows10

・java 1.8.0_202

4

1 回答 1

0

这个对我有用:

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g nodejs-server -i https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v2.0/json/petstore.json -o /tmp/nodejs-server
[main] INFO  o.o.codegen.DefaultGenerator - OpenAPI Generator: nodejs-server (server)
[main] INFO  o.o.codegen.DefaultGenerator - Generator 'nodejs-server' is considered stable.
[main] WARN  o.o.c.languages.NodeJSServerCodegen -

=======================================================================================
Currently, Node.js server doesn't work as its dependency doesn't support OpenAPI Spec3.
For further details, see https://github.com/OpenAPITools/openapi-generator/issues/34
=======================================================================================


[main] INFO  o.o.codegen.DefaultCodegen - Skipped overwriting README.md as the file already exists in /tmp/java2//README.md
[main] INFO  o.o.codegen.DefaultGenerator - Model Pets not generated since it's an alias to array (without property) and `generateAliasAsModel` is set to false (default)
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/java2/service/PetsService.js
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/java2/controllers/Pets.js
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/java2/utils/writer.js
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/java2/api/openapi.yaml
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/java2/index.js
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/java2/package.json
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/java2/.openapi-generator/VERSION

但正如警告中提到的,nodejs-server 生成器不再按预期工作,因为它的依赖项之一不支持 OpenAPI 规范 v3。

有关创建新 NodeJS Express 生成器的最新进展,请参阅https://github.com/OpenAPITools/openapi-generator/issues/2828 。

更新(2019/09):我们添加了一个新的nodejs-express-server生成器。请参阅https://twitter.com/oas_generator/status/1160000504455319553了解更多信息。

于 2019-05-21T05:26:15.573 回答