使用OpenAPI JSON Schema Genemator從OpenAPI 3.0.0-3.1.0文檔中自動生成客戶端SDK。該項目是一個代碼生成器,專注於支持所有OpenAPI和JSON模式功能。
OpenAPI JSON模式生成器允許給定一個OpenAPI文檔(支持3.0.0.0-3.1.0*)自動生成API客戶端庫(SDK Generation)。該項目著重於使輸出100%符合OpenAPI + JSON模式規格。目標是完全支持OpenAPI +中定義的所有內容,其中包含的JSON模式規格,以便開發人員可以使用所有這些功能。
目前,支持以下語言/框架:
| 特徵 | Python | 爪哇 | 科特林 |
|---|---|---|---|
| 發電機狀態 | 穩定的 | 穩定的 | 穩定的 |
| OpenAPI v3.0.0-3.1.0攝入 | x | x | x |
| JSON模式2020-12支持(組件/模式) | x | x | x |
| 組件模式類 +生成的文檔 | x | x | x |
| 為其他組件類型生產的文檔: 標題,參數,請求bodies,resspons,SecuritySchemes | x | x | |
| 生成端點的方法 為他們生成的發送/接收JSON +文檔 | x | x |
from 1var , hi-there等我們使用Discord服務器作為提出問題並互相幫助的地方。它提供的功能與Slack非常相似。您可以在這裡加入我們:https://discord.gg/mhb8wequyq
是的;歡迎捐款!如果您想用任何語言添加新的服務器腳手架,客戶端SDK或文檔生成器,請提交PR。
自2010年最初創建以來,OpenAPI規範已進行了3個修訂。 OpenApi-Json-Schema-generator項目具有以下與OpenAPI規範的兼容性:
| OpenAPI JSON模式生成器版本 | OpenAPI規格兼容性 |
|---|---|
| 3.3.0+ | 3.0.0-3.1.0* |
| 3.1.0-3.2.1 | 3.0.0-3.1.0 |
| 1.0.0-3.0.0 | 3.0.0-3.0.3 |
OpenAPI v3.1.0規範支持包括這些新/更新的2020-12 JSON模式關鍵字:
注意:這些功能也可以在“發電機文檔架構”功能中看到
要從源構建,您需要在$PATH:
Java 11
Apache Maven 3.9.3或更高
克隆項目後,您可以使用此命令從源構建它:
mvn clean install默認構建包含最小的靜態分析(通過CheckStyle)。要使用PMD和Spotbugs運行您的構建,請使用static-analysis配置文件:
mvn -Pstatic-analysis clean installDocker映像是獨立可執行的。它可以用作通過Homebrew安裝安裝的替代方法,也可以用作無法安裝Java或升級已安裝版本的開發人員。
要使用此圖像生成代碼,您需要將本地位置作為音量安裝。
例子:
docker run --rm -v " ${PWD} :/local " openapijsonschematools/openapi-json-schema-generator-cli generate
-i https://raw.githubusercontent.com/openapi-json-schema-tools/openapi-json-schema-generator/master/src/test/resources/3_0/petstore.yaml
-g python
-o /local/out/python生成的代碼將位於當前目錄中的./out/python下。
您可以使用bin/run-in-docker.sh來完成所有開發。此腳本將您的本地存儲庫映射到Docker容器中的/gen 。它還將~/.m2/repository映射到適當的容器位置。
執行mvn package :
git clone https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
cd openapi-json-schema-generator
./bin/run-in-docker.sh mvn package現在可以在您的工作目錄中訪問構建工件。
一旦構建, run-in-docker.sh將充當Openapi-Json-Schema-generator-CLI的可執行文件。要生成代碼,您需要輸出到/gen (例如/gen/out )下的目錄。例如:
./bin/run-in-docker.sh help # Executes 'help' command for openapi-json-schema-generator-cli
./bin/run-in-docker.sh list # Executes 'list' command for openapi-json-schema-generator-cli
./bin/run-in-docker.sh /gen/bin/python-petstore.sh # Builds the Python client
./bin/run-in-docker.sh generate -i src/test/resources/3_0/petstore.yaml
-g go -o /gen/out/python-petstore -p packageName=petstore_api # generates python client, outputs locally to ./out/python-petstore 如果發生這樣的錯誤,只需執行MVN CLEAS install -U命令:
org.apache.maven.lifecycle.lifecycleexecutionExction exception:無法執行目標org.apache.maven.maven.plugins:maven-surefire-plugin:2.19.1:2.19.1:test(test(devault-test)在項目OpenApi-json-json-schema-schema-generator上執行不合時宜: org.apache.maven.plugins:maven-surefire-plugin:2.19.1:測試:java.lang.exceptionininitializererror不能被施加到java.io.io.io.io.ioexception
./run-in-docker.sh mvn clean install -U無法執行目標org.fortasoft:gradle-maven-plugin:1.0.8:project openapi-json-json-schema-gradle-plugin-plugin-mvn-mvn-wrapper上的invoke(默認) 'https://services.gradle.org/distributions/gradle-4.7-bin.zip'
現在:沒有解決方案:|
要為petstore.yaml生成一個python客戶端,請運行以下內容
git clone https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
cd openapi-json-schema-generator
mvn clean package
java -jar target/openapi-json-schema-generator-cli.jar generate
-i https://raw.githubusercontent.com/openapi-json-schema-tools/openapi-json-schema-generator/master/src/test/resources/3_0/petstore.yaml
-g python
-o /var/tmp/python_api_client (如果您在Windows上,請用java -jar targetopenapi-json-schema-generator-cli.jar generate -i https://raw.githubusercontent.com/openapi-json-schema-tools/openapi-json-schema-generator/master/src/test/resources/3_0/petstore.yaml -g python -oc:temppython_api_client )
要獲取可用java -jar target/openapi-json-schema-generator-cli.jar help generate一般選項列表
要獲取Python指定選項的列表(可以通過-c選項將其傳遞給發電機),請運行java -jar target/openapi-json-schema-generator-cli.jar config-help -g python python
您可以針對PetStore API建立客戶,如下所示:
./bin/generate-samples.sh ./bin/generate_samples_configs/python.yaml(在Windows上,請安裝Windows的Git Bash以運行上面的命令)
此腳本將使用此命令運行生成器:
java -jar target/openapi-json-schema-generator-cli.jar generate
-i https://raw.githubusercontent.com/openapijsonschematools/openapi-json-schema-generator/master/src/test/resources/3_0/petstore.yaml
-g python
-t src/main/resources/python
--additional-properties packageName=petstore_api
-o samples/client/petstore/python帶有許多選項。 python選項記錄在此處。
您還可以使用help generate命令獲得選項(下面僅顯示部分結果):
NAME
openapi-json-schema-generator-cli generate - Generate code with the specified
generator.
SYNOPSIS
openapi-json-schema-generator-cli generate
[(-a <authorization> | --auth <authorization>)]
[--api-name-suffix <api name suffix>] [--api-package <api package>]
[--artifact-id <artifact id>] [--artifact-version <artifact version>]
[(-c <configuration file> | --config <configuration file>)] [--dry-run]
[(-e <templating engine> | --engine <templating engine>)]
[--enable-post-process-file]
[(-g <generator name> | --generator-name <generator name>)]
[--git-host <git host>] [--git-repo-id <git repo id>]
[--git-user-id <git user id>] [--global-property <global properties>...]
[--group-id <group id>] [--hide-generation-timestamp]
[--http-user-agent <http user agent>]
[(-i <spec file> | --input-spec <spec file>)]
[--ignore-file-override <ignore file override location>]
[--ints-allowed-for-float-double-formats]
[--invoker-package <invoker package>] [--minimal-update]
[--model-name-prefix <model name prefix>]
[--model-name-suffix <model name suffix>]
[(-o <output directory> | --output <output directory>)] [(-p <additional properties> | --additional-properties <additional properties>)...]
[--package-name <package name>] [--release-note <release note>]
[--remove-enum-value-prefix] [--remove-operation-id-prefix]
[(-s | --skip-overwrite)] [--skip-operation-example]
[--skip-validate-spec] [--strict-spec <true/false strict behavior>]
[(-t <template directory> | --template-dir <template directory>)]
[(-v | --verbose)]
OPTIONS
-a <authorization>, --auth <authorization>
adds authorization headers when fetching the OpenAPI definitions
remotely. Pass in a URL-encoded string of name:header with a comma
separating multiple values
--api-name-suffix <api name suffix>
Suffix that will be appended to all API names ('tags'). Default:
Api. e.g. Pet => PetApi. Note: Only ruby, python, jaxrs generators
support this feature at the moment.
--api-package <api package>
package for generated api classes
--artifact-id <artifact id>
artifactId in generated pom.xml. This also becomes part of the
generated library's filename
--artifact-version <artifact version>
artifact version in generated pom.xml. This also becomes part of the
generated library's filename
-c <configuration file>, --config <configuration file>
Path to configuration file. It can be JSON or YAML. If file is JSON,
the content should have the format {"optionKey":"optionValue",
"optionKey1":"optionValue1"...}. If file is YAML, the content should
have the format optionKey: optionValue. Supported options can be
different for each language. Run config-help -g {generator name}
command for language-specific config options.
--dry-run
Try things out and report on potential changes (without actually
making changes).
-e <templating engine>, --engine <templating engine>
templating engine: "handlebars"(default) or "mustache"
--enable-post-process-file
Enable post-processing file using environment variables.
-g <generator name>, --generator-name <generator name>
generator to use (see list command for list)
--git-host <git host>
Git host, e.g. gitlab.com.
--git-repo-id <git repo id>
Git repo ID, e.g. openapi-generator.
--git-user-id <git user id>
Git user ID, e.g. openapijsonschematools.
--global-property <global properties>
sets specified global properties (previously called 'system
properties') in the format of name=value,name=value (or multiple
options, each with name=value)
--group-id <group id>
groupId in generated pom.xml
--hide-generation-timestamp
Hides the generation timestamp when files are generated.
--http-user-agent <http user agent>
HTTP user agent, e.g. codegen_csharp_api_client, default to
'OpenAPI-Generator/{packageVersion}/{language}'
-i <spec file>, --input-spec <spec file>
location of the OpenAPI spec, as URL or file (required if not loaded
via config using -c)
--ignore-file-override <ignore file override location>
Specifies an override location for the .openapi-generator-ignore
file. Most useful on initial generation.
--ints-allowed-for-float-double-formats
Integers are allowed in for type: number format:float/double
payloads
--invoker-package <invoker package>
root package for generated code
--minimal-update
Only write output files that have changed.
--model-name-prefix <model name prefix>
Prefix that will be prepended to all model names.
--model-name-suffix <model name suffix>
Suffix that will be appended to all model names.
-o <output directory>, --output <output directory>
where to write the generated files (current dir by default)
-p <additional properties>, --additional-properties <additional
properties>
sets additional properties that can be referenced by the mustache
templates in the format of name=value,name=value. You can also have
multiple occurrences of this option.
--package-name <package name>
package for generated classes (where supported)
--release-note <release note>
Release note, default to 'Minor update'.
--remove-enum-value-prefix
Remove the common prefix of enum values
--remove-operation-id-prefix
Remove prefix of operationId, e.g. config_getId => getId
-s, --skip-overwrite
specifies if the existing files should be overwritten during the
generation.
--skip-operation-example
Skip examples defined in operations to avoid out of memory errors.
--skip-validate-spec
Skips the default behavior of validating an input specification.
--strict-spec <true/false strict behavior>
'MUST' and 'SHALL' wording in OpenAPI spec is strictly adhered to.
e.g. when false, no fixes will be applied to documents which pass
validation but don't follow the spec.
-t <template directory>, --template-dir <template directory>
folder containing the template files
-v, --verbose
verbose mode
然後,您可以使用自動生成的客戶端。 readme.md是一個很好的起點。
其他發電機也有樣品。
請參閱如何自定義輸出(例如軟件包名稱,版本)的customization.md。
請參閱Integration.md,以了解如何將OpenAPI Generator與Maven,Gradle,Github和CI/CD集成。
OpenAPI JSON架構生成器項目旨在為Open API規範用戶帶來好處。該項目本身俱有指定的許可證。此外,請了解以下幾點:
當該項目生成代碼時,應按照軟件的用戶視為並擁有該代碼。沒有保證(表達或暗示)生成的代碼。您可以按照自己的意願來完成您的意願,並且一旦生成,代碼就是您的責任,並且遵守您認為適當的許可條款。
GitHub代碼搜索
此存儲庫基於OpenAPI Generator的v6.2.0。該項目著重於使輸出100%符合JSON模式,作為OpenAPI 3.1規範的一部分,重點是複雜案例(自上而下的方法)。目的是完全支持JSON模式中定義的所有內容,以便開發人員可以在其API設計中利用JSON模式以及OpenAPI規範。在這裡構建可以更快地支持OpenAPI 3.X中的新功能,而不必支持許多不使用新功能的較老的發電機。
OpenAPI JSON架構生成器基於OpenAPI Generator v6.2.0。該項目之所以創建,是因為OpenAPI-Generator Core團隊需要從其項目中刪除Python發電機。 Python Generator(@-Spacether)的作者更願意繼續在OpenAPI-Generator Repo中建造,但核心團隊拒絕考慮將Python保留在OpenAPI-Generator中。以下是這些事件的時間表及其某些原因:
版權2023 OpenAPI-JSON-SCHEMA-GENERATOR貢獻者版權所有2018 OpenAPI-Generator貢獻者(https://openapi-generator.tech)版權所有2018 SmartBear Spart
根據Apache許可證(版本2.0(“許可”)獲得許可;除了符合許可外,您不得使用此文件。您可以在apache.org/licenses/license-2.0獲得許可證的副本
除非適用法律要求或以書面形式同意,否則根據許可證分配的軟件是按照“原樣”分發的,沒有任何明示或暗示的任何形式的保證或條件。請參閱許可證,以獲取執行許可條款和限制的特定語言。