Skip to content

Commit ffb6e2e

Browse files
authored
Fix the HTTP status code for the user creation admin endpoint (#4040)
2 parents 0d23679 + be1da26 commit ffb6e2e

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

crates/handlers/src/admin/v1/users/add.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub fn doc(operation: TransformOperation) -> TransformOperation {
109109
.id("createUser")
110110
.summary("Create a new user")
111111
.tag("user")
112-
.response_with::<200, Json<SingleResponse<User>>, _>(|t| {
112+
.response_with::<201, Json<SingleResponse<User>>, _>(|t| {
113113
let [sample, ..] = User::samples();
114114
let response = SingleResponse::new_canonical(sample);
115115
t.description("User was created").example(response)
@@ -137,7 +137,7 @@ pub async fn handler(
137137
NoApi(mut rng): NoApi<BoxRng>,
138138
State(homeserver): State<BoxHomeserverConnection>,
139139
Json(params): Json<Request>,
140-
) -> Result<Json<SingleResponse<User>>, RouteError> {
140+
) -> Result<(StatusCode, Json<SingleResponse<User>>), RouteError> {
141141
if repo.user().exists(&params.username).await? {
142142
return Err(RouteError::UserAlreadyExists);
143143
}
@@ -170,7 +170,10 @@ pub async fn handler(
170170

171171
repo.save().await?;
172172

173-
Ok(Json(SingleResponse::new_canonical(User::from(user))))
173+
Ok((
174+
StatusCode::CREATED,
175+
Json(SingleResponse::new_canonical(User::from(user))),
176+
))
174177
}
175178

176179
#[cfg(test)]
@@ -194,7 +197,7 @@ mod tests {
194197
}));
195198

196199
let response = state.request(request).await;
197-
response.assert_status(StatusCode::OK);
200+
response.assert_status(StatusCode::CREATED);
198201

199202
let body: serde_json::Value = response.json();
200203
assert_eq!(body["data"]["type"], "user");
@@ -245,7 +248,7 @@ mod tests {
245248
}));
246249

247250
let response = state.request(request).await;
248-
response.assert_status(StatusCode::OK);
251+
response.assert_status(StatusCode::CREATED);
249252

250253
let body: serde_json::Value = response.json();
251254
assert_eq!(body["data"]["type"], "user");
@@ -296,7 +299,7 @@ mod tests {
296299
}));
297300

298301
let response = state.request(request).await;
299-
response.assert_status(StatusCode::OK);
302+
response.assert_status(StatusCode::CREATED);
300303

301304
let body: serde_json::Value = response.json();
302305
let id = body["data"]["id"].as_str().unwrap();

docs/api/spec.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@
754754
"required": true
755755
},
756756
"responses": {
757-
"200": {
757+
"201": {
758758
"description": "User was created",
759759
"content": {
760760
"application/json": {

0 commit comments

Comments
 (0)