Skip to content

Commit 525eba6

Browse files
committed
feat chaotic: OpenAPI handlers [1/2]
commit_hash:9bd49817481c671718a00c9fc0737410904100e2
1 parent 556daba commit 525eba6

79 files changed

Lines changed: 2044 additions & 253 deletions

File tree

Some content is hidden

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

.mapping.json

Lines changed: 62 additions & 24 deletions
Large diffs are not rendered by default.

chaotic-openapi/AGENTS.jinja.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Jinja style guide
2+
3+
`*.{hpp,cpp}.jinja` templates produce C++ source files when rendered.
4+
Mixed jinja/C++ template source is hard to perceive for a human.
5+
We use several rules to ease this task:
6+
7+
- Jinja files should be as small as possible. Duplicated code/types must be moved to static `include/**.hpp` files.
8+
- Use 4 spaces for generated C++ code:
9+
```cpp
10+
namespace ns {
11+
class X {
12+
public:
13+
void f() {
14+
g();
15+
}
16+
};
17+
} // namespace ns
18+
```
19+
- Use identation with spaces to reflect statement/expression nesting level (1 level = 2 spaces), E.g.:
20+
```jinja
21+
// In file.hpp.jinja
22+
class X {
23+
{% if flag %}
24+
{% for item in items %}
25+
{{ item.cpp_type }} {{ item.cpp_name }};
26+
{% endfor %}
27+
{% endif %}
28+
};
29+
```
30+
- Leave empty line after closing statements `end*` (e.g. endif, endfor) unless followed by closing statement, "}", ">", or ";". E.g.:
31+
```jinja
32+
class T {
33+
{% if is_enabled %}
34+
{% if f_flag %}
35+
f();
36+
{% endif %}
37+
38+
{% if g_flag %}
39+
g();
40+
{% endif %}
41+
42+
foo();
43+
44+
{% if h_flag %}
45+
h();
46+
{% endif %}
47+
{% endif %}
48+
};
49+
```

chaotic-openapi/AGENTS.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# chaotic-openapi — Agent Guide
2+
3+
## What this project is
4+
5+
OpenAPI 3.x / Swagger 2.0 → C++ HTTP client code generator for the userver framework,
6+
plus a small C++ runtime library the generated code links against.
7+
8+
Main external dependencies:
9+
- C++ code — `../chaotic`, `../core`, `../utils`
10+
- cmake — `../cmake`, `../CMakeLists.txt`
11+
- bash/python scripts, helpers — `../scripts`
12+
- documentation in markdown — `../scripts/docs`
13+
14+
15+
## References
16+
17+
Reference specification is located at github: https://github.com/OAI/OpenAPI-Specification/tree/main/versions
18+
19+
20+
## Generation pipeline
21+
22+
```
23+
OpenAPI/Swagger YAML
24+
→ ref_resolver.sort_openapis() topological sort of multi-file $refs
25+
→ front/parser.Parser pydantic objects → model.Service (language-neutral IR)
26+
→ back/cpp/client/translator model.Service → types.ClientSpec (C++-typed IR)
27+
→ back/cpp/client/renderer Jinja2 templates → 14 .hpp/.cpp files
28+
```
29+
30+
`output.py` is **not** part of this pipeline. It is a separate Yandex build-system tool
31+
that generates the `PEERDIR`/include lists for `ya.make` files. It is never called from
32+
`main.py`.
33+
34+
35+
# Tests
36+
37+
Tests are implemented at multiple levels:
38+
39+
- tests/front/ - python tests on OpenAPI/Swagger format parsing
40+
- tests/back/ - python tests on IR -> C++-specific model translator
41+
- integration_tests/ - full cycle tests (parsing -> translation -> rendering -> C++ compilation -> generated types tests)
42+
- golden_tests/ - NOT tests, but a showcase of "what's possible with chaotic" / "how chaotic output looks like",
43+
containing examples of core types, cases and features
44+
45+
46+
# Jinja style guide
47+
48+
See @AGENTS.jinja.md
49+
50+
51+
# Python style guide
52+
53+
Style guide is based on PEP8.
54+
Also:
55+
- use `abc` module for abstract classes
56+
- `from x import y` is limited to `y` modules (IMR241 check in flake8-import-restrictions)

chaotic-openapi/chaotic_openapi/back/cpp_client/__init__.py renamed to chaotic-openapi/chaotic_openapi/back/cpp/__init__.py

File renamed without changes.

chaotic-openapi/chaotic_openapi/back/cpp/client/__init__.py

Whitespace-only changes.

chaotic-openapi/chaotic_openapi/back/cpp_client/middleware.py renamed to chaotic-openapi/chaotic_openapi/back/cpp/client/middleware.py

File renamed without changes.

chaotic-openapi/chaotic_openapi/back/cpp_client/output.py renamed to chaotic-openapi/chaotic_openapi/back/cpp/client/output.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from chaotic.back.cpp import types as cpp_types
1010
from chaotic.front import parser as chaotic_parser
1111
from chaotic.front import ref
12-
from chaotic_openapi.back.cpp_client import renderer
12+
from chaotic_openapi.back.cpp.client import renderer
1313

1414
TYPES_INCLUDES = [
1515
'cstdint',

chaotic-openapi/chaotic_openapi/back/cpp_client/renderer.py renamed to chaotic-openapi/chaotic_openapi/back/cpp/client/renderer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from chaotic import cpp_format
88
from chaotic import jinja_env
99
from chaotic.back.cpp import renderer as cpp_renderer
10-
from chaotic_openapi.back.cpp_client import types
10+
from chaotic_openapi.back.cpp.client import types
1111

1212
PARENT_DIR = os.path.dirname(__file__)
1313

@@ -66,7 +66,7 @@ def cpp_comment(string: str) -> str:
6666

6767
def make_env() -> jinja2.Environment:
6868
env = jinja_env.make_env(
69-
'chaotic-openapi/chaotic_openapi/back/cpp_client',
69+
'chaotic-openapi/chaotic_openapi/back/cpp/client',
7070
os.path.join(PARENT_DIR),
7171
)
7272
env.globals['list'] = list

chaotic-openapi/chaotic_openapi/back/cpp_client/templates/client.cpp.jinja renamed to chaotic-openapi/chaotic_openapi/back/cpp/client/templates/client.cpp.jinja

File renamed without changes.

chaotic-openapi/chaotic_openapi/back/cpp_client/templates/client.hpp.jinja renamed to chaotic-openapi/chaotic_openapi/back/cpp/client/templates/client.hpp.jinja

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ public:
1515
{{ op.description | cpp_comment }}
1616
/// @throw {{ op.cpp_namespace() }}::Exception
1717
{% for response in op.responses %}
18-
{% if response.is_error() %}
19-
/// @throw {{ op.cpp_namespace() }}::Response{{ response.status }}
18+
{% if response.is_error() %}
19+
/// @throw {{ op.cpp_namespace() }}::Response{{ response.status }}
2020
{% endif %}
2121
{% endfor -%}
22+
2223
virtual {{ op.cpp_namespace() }}::Response {{ op.cpp_method_name() }}(
2324
{%- if not op.empty_request() -%}
2425
const {{ op.cpp_namespace() }}::Request& request ,

0 commit comments

Comments
 (0)