Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit 43ff6dc

Browse files
committed
doc: auto-generate the API schema in the documentation
1 parent 7675561 commit 43ff6dc

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2022 The Matrix.org Foundation C.I.C.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#![forbid(unsafe_code)]
16+
#![deny(
17+
clippy::all,
18+
clippy::str_to_string,
19+
rustdoc::broken_intra_doc_links,
20+
clippy::future_not_send
21+
)]
22+
#![warn(clippy::pedantic)]
23+
24+
use std::io::Write;
25+
26+
/// This is a dummy state, it should never be used.
27+
///
28+
/// We use it to generate the API schema, which doesn't execute any request.
29+
#[derive(Clone)]
30+
struct DummyState;
31+
32+
macro_rules! impl_from_request_parts {
33+
($type:ty) => {
34+
#[axum::async_trait]
35+
impl axum::extract::FromRequestParts<DummyState> for $type {
36+
type Rejection = std::convert::Infallible;
37+
38+
async fn from_request_parts(
39+
_parts: &mut axum::http::request::Parts,
40+
_state: &DummyState,
41+
) -> Result<Self, Self::Rejection> {
42+
unimplemented!("This is a dummy state, it should never be used")
43+
}
44+
}
45+
};
46+
}
47+
48+
macro_rules! impl_from_ref {
49+
($type:ty) => {
50+
impl axum::extract::FromRef<DummyState> for $type {
51+
fn from_ref(_input: &DummyState) -> Self {
52+
unimplemented!("This is a dummy state, it should never be used")
53+
}
54+
}
55+
};
56+
}
57+
58+
impl_from_request_parts!(mas_storage::BoxRepository);
59+
impl_from_request_parts!(mas_storage::BoxClock);
60+
impl_from_request_parts!(mas_handlers::BoundActivityTracker);
61+
impl_from_ref!(mas_keystore::Keystore);
62+
63+
fn main() -> Result<(), Box<dyn std::error::Error>> {
64+
let (api, _) = mas_handlers::admin_api_router::<DummyState>();
65+
let mut stdout = std::io::stdout();
66+
serde_json::to_writer_pretty(&mut stdout, &api)?;
67+
68+
// Make sure we end with a newline
69+
stdout.write_all(b"\n")?;
70+
71+
Ok(())
72+
}

docs/api.schema.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"openapi": "3.1.0",
3+
"info": {
4+
"title": "Matrix Authentication Service admin API",
5+
"version": ""
6+
},
7+
"servers": [
8+
{
9+
"url": "{base}",
10+
"variables": {
11+
"base": {
12+
"default": "/",
13+
"description": null
14+
}
15+
}
16+
}
17+
],
18+
"paths": {},
19+
"components": {
20+
"securitySchemes": {
21+
"oauth2": {
22+
"type": "oauth2",
23+
"flows": {
24+
"clientCredentials": {
25+
"refreshUrl": "/oauth2/token",
26+
"tokenUrl": "/oauth2/token",
27+
"scopes": {
28+
"urn:mas:admin": "Grant access to the admin API"
29+
}
30+
},
31+
"authorizationCode": {
32+
"authorizationUrl": "/authorize",
33+
"tokenUrl": "/oauth2/token",
34+
"refreshUrl": "/oauth2/token",
35+
"scopes": {
36+
"urn:mas:admin": "Grant access to the admin API"
37+
}
38+
}
39+
}
40+
}
41+
}
42+
},
43+
"security": [
44+
{
45+
"oauth2": [
46+
"urn:mas:admin"
47+
]
48+
}
49+
]
50+
}

misc/update.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ set -eu
55
export SQLX_OFFLINE=1
66
BASE_DIR="$(dirname "$0")/.."
77
CONFIG_SCHEMA="${BASE_DIR}/docs/config.schema.json"
8+
API_SCHEMA="${BASE_DIR}/docs/api.schema.json"
89
GRAPHQL_SCHEMA="${BASE_DIR}/frontend/schema.graphql"
910
POLICIES_SCHEMA="${BASE_DIR}/policies/schema/"
1011

1112
set -x
1213
cargo run -p mas-config > "${CONFIG_SCHEMA}"
1314
cargo run -p mas-handlers --bin graphql-schema > "${GRAPHQL_SCHEMA}"
15+
cargo run -p mas-handlers --bin api-schema > "${API_SCHEMA}"
1416
cargo run -p mas-i18n-scan -- --update "${BASE_DIR}/templates/" "${BASE_DIR}/translations/en.json"
1517
OUT_DIR="${POLICIES_SCHEMA}" cargo run -p mas-policy --features jsonschema
1618

0 commit comments

Comments
 (0)