Generate and merge client/server stubs using openapi-generator
Project description
openapi
This repo contains helper-scripts to generate API client/server code from an OpenAPI specification using openapi-generator.
It solves a common problem: merging newly generated versions of an API with existing implementations.
Workflow
- use
openapi-generator
to generate client/server code - merge or replace new files with existing ones
- replace content of generated files (based on config)
Repo structure
examples/ example configurations
src/ source files
Examples
Some examples for the configuration and usage can be found here.
Installation
The script expect a local installation of the openapi-generator tool. For details on the installation, se here. We recommend installing it via brew:
brew install openapi-generator
To execute our wrapper script, you must install python (3.7+) and the required modules:
brew install python3
cd src/
pip3 install -r requirements.txt
Execution
To execute the script and generate all components specified in the configuration file, simply call the python script as follows:
python openapi.py [openapi.json]
Note that the specification of the path to the configuration file is optional and defaults to ./openapi.json
.
Configuration
The configuration is stored in a .json
file.
It is used to specify paths and client/server stubs to be generated using the openapi-generator
.
Structure
A basic configuration file must contain the following fields:
{
"defaults": {},
"generators": []
}
generators
is a list of generator configurations.
defaults
contains the default configuration shared by all generators.
These default properties are merged with specific ones such that values are replaced, lists are appended, and dicts are merged.
Generator
Each generator configuration must contain the following fields, specified directly or via defaults
:
{
"spec": "../backend.yml",
"tempDir": "temp",
"tempSuffix": ".tmp",
"updatedFileSuffix": ".updated",
"updatedDirSuffix": "-updated",
"generator": "python-flask",
"srcSubDir": "openapi_server",
"outputDir": "backend",
"replaceInFiles": {},
"mode": "merge",
"filesToKeep": [],
"dirsToKeep": []
}
The fields have the following meaning:
spec
: openapi specification to usetempDir
: path to use for the temporary code generationtempSuffix
: suffix to use for temporary filesupdatedFileSuffix
: suffix for updated filesupdatedDirSuffix
: suffix for updated dirsgenerator
: the generator to use (see the official documentation for a complete listsrcSubDir
: the sub dir of generated code to use as outputoutputDir
: the target path where to move the generated filesreplaceInFiles
: dicts of in-file replacements (see below)mode
: how to proceed ifoutputDir
is not empty (overwrite
ormerge
)filesToKeep
: files that should not be overwritten inmode
merge
dirsToKeep
: dirs that should not be overwritten inmode
merge
Before processing, each configuration is merged with the default values (defaults
) as described above.
replaceInFiles
The keys of this dict are regular expressions (see here, here, and here for documentation) that describe the filenames of files to be processed. The values are dicts which map strings to their replacement in the corresponding file(s).
An example of this field is:
{
"(.*\\.)(yml|yaml|py)": {
"openapi_server": "backend"
},
"__main__.py": {
"import connexion": "import connexion, os",
"app\\.run\\(port=8080\\)": "app.run(port=int(os.getenv('PORT', 8080)), debug=bool(os.getenv('DEBUG', False)))",
"openapi_server": "backend"
}
}
This results in the replacement of openapi_server
with backend
in all .py
and .yaml
files as well as three replacements in __main__.py
.
mode
The mode
specifies how to proceed with the generated files if the target dir outputDir
is not empty, i.e., the respective component has been generated before.
So far, two modes are available:
overwrite
: delete all files and dirs inoutputDir
and move content fromtempDir
/srcSubDir
theremerge
: replace all files/dirs not infilesToKeep
ordirsToKeep
(handle those as described below)
All newly created files are processed as specified in replaceInFiles
before replacing or being merged with existing ones.
mode
: overwrite
In this mode, all existing files and dirs in outputDir
are deleted before the generated files from tempDir
/srcSubDir
are moved to outputDir
.
mode
: merge
In this mode, all files in outputDir
are replaced by the files in tempDir
/srcSubDir
except for those specifeid in dirsToKeep
and filesToKeep
.
For those firs/files, the newly generated ones are suffixed by updatedDirSuffix
or updatedFileSuffix
resp. and moved next to their existing version in targetDir
.
As an example, consider the following configuration and file structure after generation:
{
"mode": "merge",
"filesToKeep": [
"a/a1.txt",
"a/a2.txt"
],
"dirsToKeep": [
"b",
"c",
]
}
backend/ # the existing files (targetDir)
a/
a1.txt
b/
b1.txt
temp/ # the newly generated files (tempDir)
relevant/ # the files to consider (srcSubDir)
a/
a1.txt
a2.txt
b/
b1.txt
b2.txt
c/
c1.txt
c2.txt
This would result in the following file structure:
backend/
a/
a1.txt # old
a1.txt.updated # new
a2.txt # new
b/
b1.txt # old
b-updated/
b1.txt # new
b2.txt # new
c/
c1.txt # new
c2.txt # new
Note that temp/relevant/b/b2.txt
is not moved to the new files from baclend//b/b2.txt
because the whole dir is specified in dirsToKeep
and it exists already in backend/
.
In contrast, c/
does not exist in backend/
yet and is therefore moved to the target position.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file evonik-openapi-0.0.7.tar.gz
.
File metadata
- Download URL: evonik-openapi-0.0.7.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 133dd0acf1711b506ef31392890d2b20a248b88fd34014c1a02b754c8183f499 |
|
MD5 | 54d685f5aacd32e9f5c6949318285fdb |
|
BLAKE2b-256 | e33c4aaaa2ed32d59a00a8dccba036e049d80dce55160cf57bbf65fcc031dfc2 |
File details
Details for the file evonik_openapi-0.0.7-py2.py3-none-any.whl
.
File metadata
- Download URL: evonik_openapi-0.0.7-py2.py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e280521cd3953227a88c2f6c30a89083767572249a9b65d1578495c8b109d3b6 |
|
MD5 | ee0a6fd9d3ae318054366f8138f98735 |
|
BLAKE2b-256 | 568e982e573557425f8d9e7bbe2fccd7fb9ed46d84c4e5150882328cd54187af |