MySQL 9.3.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 <string_view>
36
42
44
45namespace mrs {
46namespace rest {
47
50using DbObjectPtr = std::shared_ptr<DbObject>;
51
52constexpr std::string_view k_auth_method_name = "mrs_login";
53constexpr std::string_view k_schema_version{"1.0.0"};
54constexpr std::string_view k_openapi_version{"3.1.0"};
55
56/**
57 * Create OpenAPI 'paths' section. Each path contains supported HTTP methods
58 * with HTTP responses and optional parameters.
59 *
60 * @param[in] privileges User privileges
61 * @param[in] entry DBobject entry
62 * @param[in] url path used by DBobject entry
63 * @param[in] allocator JSON allocator that is used to create OpenAPI swagger.
64 *
65 * @return OpenAPI paths JSON.
66 */
67rapidjson::Value get_route_openapi_schema_path(
68 const std::optional<uint32_t> privileges, DbObjectPtr entry,
69 const std::string &url, rapidjson::Document::AllocatorType &allocator);
70
71/**
72 * Create OpenAPI components section containing security schemes and schemas
73 * (type information with constraints) for each db object passed as a parameter.
74 *
75 * @param[in] entry DBobject entry
76 * @param[in] allocator JSON allocator that is used to create OpenAPI swagger.
77 *
78 * @return OpenAPI components JSON.
79 */
80rapidjson::Value get_route_openapi_component(
81 DbObjectPtr entry, rapidjson::Document::AllocatorType &allocator);
82
83/**
84 * Create "_metadata" schema component item from a procedure call.
85 *
86 * @param[in, out] schema_properties JSON containing schema components.
87 * @param[in] allocator JSON allocator that is used to create OpenAPI swagger.
88 */
90 rapidjson::Value &schema_properties,
91 rapidjson::Document::AllocatorType &allocator);
92
93/**
94 * Create OpenAPI title, version and description.
95 *
96 * @param[in] service DBservice entry.
97 * @param[in] allocator JSON allocator that is used to create OpenAPI swagger.
98 *
99 * @return OpenAPI header JSON.
100 */
101rapidjson::Value get_header_info(std::shared_ptr<DbService> service,
102 rapidjson::Document::AllocatorType &allocator);
103
104/**
105 * Check if the given DB Object entry can be used for getting an OpenAPI
106 * description. It must be enabled, its schema must be enabled, and it have to
107 * be in appropriate type.
108 *
109 * @param[in] db_obj DB Object entry
110 * @param[in] db_schema DB Schema entry
111 *
112 * @retval true DB Object entry might be used.
113 * @retval false DB Object entry might not be used.
114 */
115bool is_supported(
116 const std::shared_ptr<mrs::database::entry::DbObject> &db_obj,
117 const std::shared_ptr<mrs::database::entry::DbSchema> &db_schema);
118
119/**
120 * Create security scheme for OpenAPI.
121 *
122 * @param[in] allocator JSON allocator that is used to create OpenAPI swagger.
123 *
124 * @return OpenAPI security scheme object.
125 */
126rapidjson::Value get_security_scheme(
127 rapidjson::Document::AllocatorType &allocator);
128
129/**
130 * Sort Endpoint children by request path.
131 *
132 * @param[in] children Endpoint children.
133 * @tparam R returned Endpoint type.
134 *
135 * @return sorted endpoints.
136 */
137template <typename R>
139 std::vector<std::shared_ptr<mrs::interface::EndpointBase>> children) {
140 std::vector<R *> result;
141 for (const auto &child : children) {
142 auto child_endpoint = std::dynamic_pointer_cast<R>(child);
143 if (!child_endpoint) continue;
144
145 result.push_back(child_endpoint.get());
146 }
147
148 std::sort(std::begin(result), std::end(result),
149 [](const auto &a, const auto &b) {
150 return a->get()->request_path < b->get()->request_path;
151 });
152
153 return result;
154}
155
156} // namespace rest
157} // namespace mrs
158
159#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:323
Define rapidjson::SizeType to be std::uint64_t.
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:1275
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:59
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:707
rapidjson::Value get_route_openapi_schema_path(const std::optional< uint32_t > privileges, DbObjectPtr entry, const std::string &url, rapidjson::Document::AllocatorType &allocator)
Create OpenAPI 'paths' section.
Definition: openapi_object_creator.cc:1210
std::shared_ptr< DbObject > DbObjectPtr
Definition: openapi_object_creator.h:50
constexpr std::string_view k_openapi_version
Definition: openapi_object_creator.h:54
constexpr std::string_view k_auth_method_name
Definition: openapi_object_creator.h:52
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:701
rapidjson::Value get_security_scheme(rapidjson::Document::AllocatorType &allocator)
Create security scheme for OpenAPI.
Definition: openapi_object_creator.cc:87
mrs::database::entry::DbService DbService
Definition: openapi_object_creator.h:49
constexpr std::string_view k_schema_version
Definition: openapi_object_creator.h:53
mrs::database::entry::DbObject DbObject
Definition: openapi_object_creator.h:48
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:138
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
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