MySQL 9.4.0
Source Code Documentation
openapi_object_creator.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2024, 2025 Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_REST_MRS_SRC_MRS_REST_REST_OPENAPI_OBJECT_CREATOR_H_
27#define ROUTER_SRC_REST_MRS_SRC_MRS_REST_REST_OPENAPI_OBJECT_CREATOR_H_
28
29#ifdef RAPIDJSON_NO_SIZETYPEDEFINE
30#include "my_rapidjson_size_t.h"
31#endif
32
33#include <rapidjson/document.h>
34
35#include <optional>
36#include <string_view>
37
43
45
46namespace mrs {
47namespace rest {
48
51using DbObjectPtr = std::shared_ptr<DbObject>;
52
53constexpr std::string_view k_auth_method_name = "mrs_login";
54constexpr std::string_view k_schema_version{"1.0.0"};
55constexpr std::string_view k_openapi_version{"3.1.0"};
56
57/**
58 * Create OpenAPI 'paths' section. Each path contains supported HTTP methods
59 * with HTTP responses and optional parameters.
60 *
61 * @param[in] privileges User privileges
62 * @param[in] entry DBobject entry
63 * @param[in] url path used by DBobject entry
64 * @param[in] is_async BDobject supports async operations
65 * @param[in] allocator JSON allocator that is used to create OpenAPI swagger.
66 *
67 * @return OpenAPI paths JSON.
68 */
69rapidjson::Value get_route_openapi_schema_path(
70 const std::optional<uint32_t> privileges, DbObjectPtr entry,
71 const std::string &url, const bool is_async,
72 rapidjson::Document::AllocatorType &allocator);
73
74/**
75 * Create OpenAPI components section containing security schemes and schemas
76 * (type information with constraints) for each db object passed as a parameter.
77 *
78 * @param[in] entry DBobject entry
79 * @param[in] allocator JSON allocator that is used to create OpenAPI swagger.
80 *
81 * @return OpenAPI components JSON.
82 */
83rapidjson::Value get_route_openapi_component(
84 DbObjectPtr entry, rapidjson::Document::AllocatorType &allocator);
85
86/**
87 * Create "_metadata" schema component item from a procedure call.
88 *
89 * @param[in, out] schema_properties JSON containing schema components.
90 * @param[in] allocator JSON allocator that is used to create OpenAPI swagger.
91 */
93 rapidjson::Value &schema_properties,
94 rapidjson::Document::AllocatorType &allocator);
95
96/**
97 * Create OpenAPI title, version and description.
98 *
99 * @param[in] service DBservice entry.
100 * @param[in] allocator JSON allocator that is used to create OpenAPI swagger.
101 *
102 * @return OpenAPI header JSON.
103 */
104rapidjson::Value get_header_info(std::shared_ptr<DbService> service,
105 rapidjson::Document::AllocatorType &allocator);
106
107/**
108 * Check if the given DB Object entry can be used for getting an OpenAPI
109 * description. It must be enabled, its schema must be enabled, and it have to
110 * be in appropriate type.
111 *
112 * @param[in] db_obj DB Object entry
113 * @param[in] db_schema DB Schema entry
114 *
115 * @retval true DB Object entry might be used.
116 * @retval false DB Object entry might not be used.
117 */
118bool is_supported(
119 const std::shared_ptr<mrs::database::entry::DbObject> &db_obj,
120 const std::shared_ptr<mrs::database::entry::DbSchema> &db_schema);
121
122/**
123 * Create security scheme for OpenAPI.
124 *
125 * @param[in] allocator JSON allocator that is used to create OpenAPI swagger.
126 *
127 * @return OpenAPI security scheme object.
128 */
129rapidjson::Value get_security_scheme(
130 rapidjson::Document::AllocatorType &allocator);
131
132/**
133 * Sort Endpoint children by request path.
134 *
135 * @param[in] children Endpoint children.
136 * @tparam R returned Endpoint type.
137 *
138 * @return sorted endpoints.
139 */
140template <typename R>
142 std::vector<std::shared_ptr<mrs::interface::EndpointBase>> children) {
143 std::vector<R *> result;
144 for (const auto &child : children) {
145 auto child_endpoint = std::dynamic_pointer_cast<R>(child);
146 if (!child_endpoint) continue;
147
148 result.push_back(child_endpoint.get());
149 }
150
151 std::sort(std::begin(result), std::end(result),
152 [](const auto &a, const auto &b) {
153 return a->get()->request_path < b->get()->request_path;
154 });
155
156 return result;
157}
158
159/**
160 * Check if asynchronous tasks are enabled in options.
161 *
162 * @param[in] options Options configured for the endpoint
163 *
164 * @return information if async operations are enabled
165 */
166bool async_enabled(const std::optional<std::string> &options);
167
168/**
169 * Add specification related to endpoint responsible for async operations
170 * (located at /service/schema/object/{taskId}).
171 *
172 * @param[in] privileges User privileges
173 * @param[in] entry DBobject entry
174 * @param[in] allocator JSON allocator that is used to create OpenAPI swagger.
175 *
176 * @return OpenAPI paths JSON.
177 */
178rapidjson::Value add_task_id_endpoint(
179 const std::optional<uint32_t> privileges, DbObjectPtr entry,
180 rapidjson::Document::AllocatorType &allocator);
181
182} // namespace rest
183} // namespace mrs
184
185#endif // ROUTER_SRC_REST_MRS_SRC_MRS_REST_REST_OPENAPI_OBJECT_CREATOR_H_
Logging interface for using and extending the logging subsystem.
#define IMPORT_LOG_FUNCTIONS()
convenience macro to avoid common boilerplate
Definition: logging.h:331
Define rapidjson::SizeType to be std::uint64_t.
rapidjson::Value get_route_openapi_schema_path(const std::optional< uint32_t > privileges, DbObjectPtr entry, const std::string &url, const bool is_async, rapidjson::Document::AllocatorType &allocator)
Create OpenAPI 'paths' section.
Definition: openapi_object_creator.cc:1277
bool is_supported(const std::shared_ptr< mrs::database::entry::DbObject > &db_obj, const std::shared_ptr< mrs::database::entry::DbSchema > &db_schema)
Check if the given DB Object entry can be used for getting an OpenAPI description.
Definition: openapi_object_creator.cc:1346
rapidjson::Value get_header_info(std::shared_ptr< DbService > service, rapidjson::Document::AllocatorType &allocator)
Create OpenAPI title, version and description.
Definition: openapi_object_creator.cc:69
void get_procedure_metadata_component(rapidjson::Value &schema_properties, rapidjson::Document::AllocatorType &allocator)
Create "_metadata" schema component item from a procedure call.
Definition: openapi_object_creator.cc:720
std::shared_ptr< DbObject > DbObjectPtr
Definition: openapi_object_creator.h:51
constexpr std::string_view k_openapi_version
Definition: openapi_object_creator.h:55
constexpr std::string_view k_auth_method_name
Definition: openapi_object_creator.h:53
rapidjson::Value get_route_openapi_component(DbObjectPtr entry, rapidjson::Document::AllocatorType &allocator)
Create OpenAPI components section containing security schemes and schemas (type information with cons...
Definition: openapi_object_creator.cc:714
rapidjson::Value get_security_scheme(rapidjson::Document::AllocatorType &allocator)
Create security scheme for OpenAPI.
Definition: openapi_object_creator.cc:97
mrs::database::entry::DbService DbService
Definition: openapi_object_creator.h:50
constexpr std::string_view k_schema_version
Definition: openapi_object_creator.h:54
mrs::database::entry::DbObject DbObject
Definition: openapi_object_creator.h:49
rapidjson::Value add_task_id_endpoint(const std::optional< uint32_t > privileges, DbObjectPtr entry, rapidjson::Document::AllocatorType &allocator)
Add specification related to endpoint responsible for async operations (located at /service/schema/ob...
Definition: openapi_object_creator.cc:1359
std::vector< R * > sort_children_by_request_path(std::vector< std::shared_ptr< mrs::interface::EndpointBase > > children)
Sort Endpoint children by request path.
Definition: openapi_object_creator.h:141
bool async_enabled(const std::optional< std::string > &options)
Check if asynchronous tasks are enabled in options.
Definition: openapi_object_creator.cc:60
Definition: authorize_manager.h:48
static mysql_service_status_t get(THD **thd) noexcept
Definition: mysql_current_thread_reader_all_empty.cc:31
const char * begin(const char *const c)
Definition: base64.h:44
Definition: options.cc:57
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2876
struct result result
Definition: result.h:34
Definition: completion_hash.h:35
Definition: result.h:30