Skip to content

Commit 3090439

Browse files
v1.0.0 ✨
1 parent 05c2361 commit 3090439

80 files changed

Lines changed: 16588 additions & 23 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
strategy:
2121
fail-fast: false
2222
matrix:
23-
python-version: [3.6, 3.7, 3.8, 3.9, "3.10"]
23+
python-version: [3.7, 3.8, 3.9, "3.10"]
2424

2525
steps:
2626
- uses: actions/checkout@v1
@@ -48,7 +48,7 @@ jobs:
4848
run: |
4949
PYVER=`python -V 2>&1`
5050
51-
if [ "$PYVER" == "Python 3.10.0" ]; then
51+
if [ "${PYVER:0:-2}" == "Python 3.10" ]; then
5252
pip install -r requirements.txt
5353
else
5454
pip install -U --no-index --find-links=deps deps/*

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.0.0] - 2022-04-20 :sparkles:
9+
- Adds features and a CLI to generate artifacts from OpenAPI Documentation
10+
files (markdown for MkDocs and PyMdown extensions, PlantUML class diagrams
11+
from components schemas)
12+
- Drops support for Python 3.6
13+
814
## [0.1.6] - 2021-11-17 :gem:
915
- Adds `py.typed` file
1016
- Add `Python 3.10` to the GitHub Workflow
@@ -27,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2733

2834
- Corrects a bug forcing `camelCase` on examples objects handled as dataclasses
2935
- Adds base64 ValueFormat to the v3 enum
36+
3037
## [0.1.2] - 2021-05-03 :notes:
3138

3239
- Adds a changelog

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Neoteroi
3+
Copyright (c) 2022 Neoteroi
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

MANIFEST.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
include LICENSE
2+
include README.md
3+
recursive-include openapidocs *.html *.md
4+
5+
# Stubs
6+
include openapidocs/py.typed

README.md

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,105 @@
11
![Build](https://github.com/Neoteroi/essentials-openapi/workflows/Build/badge.svg)
22
[![pypi](https://img.shields.io/pypi/v/essentials-openapi.svg)](https://pypi.python.org/pypi/essentials-openapi)
33
[![versions](https://img.shields.io/pypi/pyversions/essentials-openapi.svg)](https://github.com/neoteroi/essentials-openapi)
4-
[![license](https://img.shields.io/github/license/neoteroi/essentials-openapi.svg)](https://github.com/neoteroi/essentials-openapi/blob/master/LICENSE)
4+
[![license](https://img.shields.io/github/license/neoteroi/essentials-openapi.svg)](https://github.com/neoteroi/essentials-openapi/blob/main/LICENSE)
55
[![codecov](https://codecov.io/gh/Neoteroi/essentials-openapi/branch/main/graph/badge.svg?token=WEZ8YECJDF)](https://codecov.io/gh/Neoteroi/essentials-openapi)
66

77
# essentials-openapi
88

9-
Classes to generate OpenAPI Documentation v3 and v2, in JSON and YAML.
9+
Classes to generate [OpenAPI Documentation](https://swagger.io/specification/)
10+
v3 and v2, in JSON and YAML, and to generate other kinds of documents from
11+
OpenAPI Documentation files.
1012

1113
```bash
1214
pip install essentials-openapi
1315
```
1416

17+
To install with dependencies to generate other kinds of artifacts from source
18+
OpenAPI Documentation files:
19+
20+
```bash
21+
pip install essentials-openapi[full]
22+
```
23+
1524
## Useful links
1625

1726
* https://swagger.io/specification/
1827
* https://editor.swagger.io
1928

2029
## Usage
21-
This library has been created to implement generation of OpenAPI Documentation
30+
This library has been originally created to implement generation of OpenAPI Documentation
2231
in the [`BlackSheep` web framework](https://github.com/RobertoPrevato/BlackSheep).
23-
This package contains only parts that belong logically to the OpenAPI specification,
24-
and can be reused for other applications.
32+
However, this package is abstracted from that web framework and can be reused for other
33+
applications. Today this library also offers functions to generate documentation from
34+
source OpenAPI Documentation files.
35+
36+
## Features to generate artifacts from Open API Documentation
37+
38+
These require the full package: install it using `pip install essentials-openapi[full]`.
39+
40+
To generate output for [MkDocs](https://www.mkdocs.org) and [PyMdown extentions](https://facelessuser.github.io/pymdown-extensions/):
41+
42+
```bash
43+
oad gen-docs -s example1-openapi.json -d output.md
44+
```
45+
46+
![Example MkDocs documentation](./docs/example-1.png)
47+
48+
_Example of MkDocs documentation generated using [Neoteroi/mkdocs-plugins](https://github.com/Neoteroi/mkdocs-plugins)._
49+
50+
---
51+
52+
To generate a [PlantUML](https://plantuml.com) [class
53+
diagram](https://plantuml.com/class-diagram) of the components schemas:
54+
55+
```bash
56+
oad gen-docs -s source-openapi.json -d schemas.wsd --style "PLANTUML_SCHEMAS"
57+
```
58+
59+
![Example schemas](./docs/example-schemas.png)
60+
61+
_Example of PlantUML diagram generated from components schemas._
62+
63+
### Goals
64+
65+
* Provide an API to generate OpenAPI Documentation files.
66+
* Providing functions to handle OpenAPI Documentation, like those to generate
67+
other kinds of documentation from source OpenAPI Documentation files.
68+
* Support enough features to be useful for the most common API scenarios,
69+
especially for OAD files that are generated automatically from web frameworks.
70+
71+
### Non-Goals
72+
73+
* To implement the whole OAD Specification.
74+
* For the features that generate artifacts: OpenAPI Documentation files are
75+
**supposed to be coming from trusted sources**. Trying to handle source files
76+
from untrusted sources and potentially causing HTML injection is out of the
77+
scope of this library.
2578

2679
## Limitations
2780

28-
* Partial support for Parameter properties: `style` , `allow_reserved` ,
29-
`explode` are not handled
30-
* Doesn't implement validation of values, currently it is only concerned in
31-
generating code from a higher level API (it might be extended in the future
32-
with classes for validation)
81+
* Partial support for Parameter properties: `style`, `allow_reserved`, `explode` are not
82+
handled.
83+
* Doesn't implement validation of values, currently it is only concerned in generating
84+
code from a higher level API (it might be extended in the future with classes for
85+
validation).
86+
* The features to generate artifacts from OpenAPI Documentation currently support only
87+
Version 3 of the specification.
88+
89+
### Styles
90+
91+
| Style | Int value | Description |
92+
| ---------------- | --------- | -------------------------------------------- |
93+
| MKDOCS | 1 | Markdown for MkDocs and PyMdown extensions. |
94+
| MARKDOWN | 2 | Basic Markdown. |
95+
| HTML | 3 | Plain HTML _(planned, not yet implemented)_. |
96+
| PLANTUML_SCHEMAS | 100 | PlantUML schema for components schemas. |
97+
98+
### Supported sources for OpenAPI Documentation
99+
100+
| Source | Example |
101+
| ------------------------------ | ---------------------------------------------------- |
102+
| YAML file | `./docs/swagger.yaml` |
103+
| JSON file | `./docs/swagger.json` |
104+
| URL returning YAML on HTTP GET | `https://example-domain.net/swagger/v1/swagger.yaml` |
105+
| URL returning JSON on HTTP GET | `https://example-domain.net/swagger/v1/swagger.json` |

docs/example-1.png

356 KB
Loading

docs/example-schemas.png

154 KB
Loading

openapidocs/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VERSION = "1.0.0"

openapidocs/commands/__init__.py

Whitespace-only changes.

openapidocs/commands/docs.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from typing import Union
2+
3+
import click
4+
5+
from openapidocs.logs import logger
6+
from openapidocs.mk.generate import generate_document
7+
from openapidocs.mk.jinja import OutputStyle
8+
9+
10+
@click.command(name="gen-docs")
11+
@click.option(
12+
"-s",
13+
"--source",
14+
help=(
15+
"Source of the OpenAPI Documentation file. "
16+
"This can be either a public URL or a path to a file."
17+
),
18+
required=True,
19+
)
20+
@click.option(
21+
"-d",
22+
"--destination",
23+
help="Destination file path.",
24+
required=True,
25+
)
26+
@click.option(
27+
"-t",
28+
"--style",
29+
help="The style of the output.",
30+
required=False,
31+
default="MKDOCS",
32+
show_default=True,
33+
)
34+
def generate_documents_command(source: str, destination: str, style: Union[int, str]):
35+
"""
36+
Generates other kinds of documents from source OpenAPI Documentation files.
37+
38+
For example, to generate Markdown for MkDocs and PyMdown:
39+
40+
$ openapidocs gen-docs -s source-openapi.json -d output.md
41+
42+
JSON and YAML sources are supported.
43+
It is also possible to fetch the specification file from a public URL:
44+
45+
$ openapidocs gen-docs -s https://.../source-openapi.json -d output.md
46+
47+
For more information, refer to the documentation at
48+
https://github.com/Neoteroi/essentials-openapi
49+
"""
50+
try:
51+
generate_document(source, destination, style)
52+
except KeyboardInterrupt: # pragma: nocover
53+
logger.info("User interrupted")
54+
exit(1)
55+
except ValueError as value_error:
56+
logger.error(value_error)
57+
exit(2)
58+
59+
60+
@click.command(name="list-styles")
61+
def list_styles_command():
62+
"""
63+
Displays the supported output styles on the screen.
64+
"""
65+
try:
66+
for value in OutputStyle:
67+
logger.info("%s: %s", value.name, value.value)
68+
except KeyboardInterrupt: # pragma: nocover
69+
logger.info("User interrupted")
70+
exit(1)

0 commit comments

Comments
 (0)