Skip to content

Commit f3115e3

Browse files
committed
feat chaotic: OpenAPI generator improvements
- add golden tests for handlers - generate config.chaotic.yaml, merge it into the final config.yaml - documentation on chaotic commit_hash:8beea42a31d289d5695fbf3cd868a14b281fea41
1 parent 64d5b74 commit f3115e3

71 files changed

Lines changed: 1162 additions & 643 deletions

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: 41 additions & 36 deletions
Large diffs are not rendered by default.

chaotic-openapi/chaotic_openapi/back/cpp/handler/renderer.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import re
44

55
import jinja2
6+
import yaml
67

78
from chaotic import cpp_format
89
from chaotic import jinja_env
@@ -135,6 +136,30 @@ def render(spec: ServerSpec, context: Context, userver_namespace: str) -> list[C
135136
return output
136137

137138

139+
def render_config_yaml(spec: ServerSpec) -> str:
140+
components = {
141+
component_name(op): {
142+
'path': op.path,
143+
'method': op.method.upper(),
144+
}
145+
for op in spec.operations
146+
}
147+
config = {
148+
'components_manager': {
149+
'components': components,
150+
},
151+
}
152+
return yaml.dump(config, default_flow_style=False, sort_keys=False)
153+
154+
155+
def save_config_yaml(content: str, output_dir: str) -> None:
156+
path = os.path.join(output_dir, 'config.chaotic.yaml')
157+
os.makedirs(os.path.dirname(path), exist_ok=True)
158+
159+
with open(path, 'w') as ofile:
160+
ofile.write(content)
161+
162+
138163
def render_views(spec: ServerSpec, context: Context, userver_namespace: str) -> list[CppOutput]:
139164
output = []
140165
for op in spec.operations:

chaotic-openapi/chaotic_openapi/back/cpp/handler/templates/responses.hpp.jinja

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ struct Response{{ response.status }} final {
5858
{% endfor %}
5959

6060
/// All possible responses for this operation.
61+
{% if op.iter_error_responses() | list %}
6162
/// Error types (4xx/5xx) may also be thrown from Handle().
63+
{% endif %}
6264
{% if op.responses | length == 1 %}
6365
using Response = Response{{ op.responses[0].status }};
6466
{% else %}

chaotic-openapi/chaotic_openapi/back/cpp/handler/templates/view.cpp.jinja

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,31 @@ Response View::Handle(Request&& /*request*/, Deps&& /*deps*/) {
1111
{% if op.request_bodies %}
1212
{% if op.request_bodies[0].content_type == 'application/json' %}
1313
std::string View::GetRequestBodyForLogging(
14-
const {{ userver }}::formats::json::Value& /*body*/) {
14+
const {{ userver }}::formats::json::Value& body) {
15+
(void)body;
1516
return {};
1617
}
1718

1819
std::string View::GetInvalidRequestBodyForLogging(
19-
const {{ userver }}::server::http::HttpRequest& /*request*/) {
20+
const {{ userver }}::server::http::HttpRequest& request) {
21+
(void)request;
2022
return {};
2123
}
2224
{% else %}
23-
std::string View::GetRequestBodyForLogging(const std::string& /*body*/) { return {}; }
25+
std::string View::GetRequestBodyForLogging(const std::string& body) {
26+
(void)body;
27+
return {};
28+
}
2429
{% endif %}
2530
{% endif %}
2631

2732
std::string View::GetResponseForLogging(
28-
const Response& /*response*/,
29-
const std::string& /*serialized_response*/,
30-
{{ userver }}::server::request::RequestContext& /*context*/) {
33+
const Response& response,
34+
const std::string& serialized_response,
35+
{{ userver }}::server::request::RequestContext& context) {
36+
(void)response;
37+
(void)serialized_response;
38+
(void)context;
3139
return {};
3240
}
3341
*/

chaotic-openapi/chaotic_openapi/main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,13 @@ def do_main():
7474
).spec()
7575

7676
if args.gen in ('handlers', 'handlers+views'):
77+
# handlers
7778
outputs = handler_renderer.render(spec, ctx, args.userver)
7879
client_renderer.CppOutput.save(outputs, args.output_dir)
80+
81+
# config.yaml
82+
config_content = handler_renderer.render_config_yaml(spec)
83+
handler_renderer.save_config_yaml(config_content, args.output_dir)
7984
if args.gen in ('handlers+views', 'views'):
8085
view_outputs = handler_renderer.render_views(spec, ctx, args.userver)
8186
handler_renderer.save_views(view_outputs, args.src_dir)

chaotic-openapi/golden_tests/CMakeLists.txt

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,30 @@ include(GoogleTest)
44
include(ChaoticGen)
55

66
file(GLOB_RECURSE SCHEMAS "${CMAKE_CURRENT_SOURCE_DIR}/schemas/*.yaml")
7+
78
userver_target_generate_openapi_client(
8-
${PROJECT_NAME}-chgen
9+
${PROJECT_NAME}-chgen-client
10+
NAME test
11+
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/src/client"
12+
SCHEMAS ${SCHEMAS}
13+
)
14+
15+
userver_target_generate_openapi_handlers(
16+
${PROJECT_NAME}-chgen-handlers
917
NAME test
10-
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/src"
18+
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/src/handlers"
19+
SRC_DIR "${CMAKE_CURRENT_BINARY_DIR}/src/handlers"
1120
SCHEMAS ${SCHEMAS}
1221
)
1322

14-
add_test(NAME chaotic-openapi-golden
15-
COMMAND # Diff returns 0 if files are the same, 1 if they differ
16-
diff -uNrpB "${CMAKE_CURRENT_SOURCE_DIR}/output" "${CMAKE_CURRENT_BINARY_DIR}/src"
23+
add_test(
24+
NAME chaotic-openapi-golden
25+
# Diff returns 0 if files are the same, 1 if they differ
26+
COMMAND diff -uNrpB "${CMAKE_CURRENT_SOURCE_DIR}/output" "${CMAKE_CURRENT_BINARY_DIR}/src"
1727
)
28+
1829
add_custom_target(
1930
update-openapi-golden-tests
20-
DEPENDS ${PROJECT_NAME}-chgen
31+
DEPENDS ${PROJECT_NAME}-chgen-client ${PROJECT_NAME}-chgen-handlers
2132
COMMAND "${CMAKE_COMMAND}" -E copy_directory "${CMAKE_CURRENT_BINARY_DIR}/src" "${CMAKE_CURRENT_SOURCE_DIR}/output"
2233
)

chaotic-openapi/golden_tests/output/include/clients/test/client.hpp renamed to chaotic-openapi/golden_tests/output/client/include/clients/test/client.hpp

File renamed without changes.

chaotic-openapi/golden_tests/output/include/clients/test/client_fwd.hpp renamed to chaotic-openapi/golden_tests/output/client/include/clients/test/client_fwd.hpp

File renamed without changes.

chaotic-openapi/golden_tests/output/include/clients/test/client_impl.hpp renamed to chaotic-openapi/golden_tests/output/client/include/clients/test/client_impl.hpp

File renamed without changes.

chaotic-openapi/golden_tests/output/include/clients/test/component.hpp renamed to chaotic-openapi/golden_tests/output/client/include/clients/test/component.hpp

File renamed without changes.

0 commit comments

Comments
 (0)