feat(api-docs): reintroduce API doc generation (#289)

- reintroduces API doc generation via API_DOCS_ENABLED
- accessible publicly via /api/docs and /api/openapi.json
This commit is contained in:
Zach
2026-04-22 17:26:58 -04:00
committed by Zach Yale
parent 351074fcb6
commit 5ac7b5553e
9 changed files with 102 additions and 4 deletions
+9
View File
@@ -24,6 +24,15 @@ just api run
just api check
```
## API Docs (`API_DOCS_ENABLED`)
- `application.yaml` binds `app.api-docs.enabled` to `API_DOCS_ENABLED` (default `false`).
- `application.yaml` binds `springdoc.api-docs.enabled` to `app.api-docs.enabled`.
- `application-dev.yml` enables docs for local dev profile runs by default.
- For runtime/container profiles, set `API_DOCS_ENABLED=true` to expose:
- `/api/openapi.json`
- `/api/docs`
## Backend Conventions
- Use Spring Data JPA repository methods or JPQL. Do not introduce native queries unless a maintainer has explicitly approved them.
+1 -1
View File
@@ -97,7 +97,7 @@ dependencies {
annotationProcessor("org.mapstruct:mapstruct-processor:1.6.3")
// --- API Documentation ---
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:3.0.2")
implementation("org.springdoc:springdoc-openapi-starter-webmvc-api:3.0.2")
implementation("org.apache.commons:commons-compress:1.28.0")
implementation("org.tukaani:xz:1.12") // Required by commons-compress for 7z support
implementation("org.apache.commons:commons-text:1.15.0")
@@ -50,6 +50,8 @@ public class SecurityConfig {
private static final String[] COMMON_PUBLIC_ENDPOINTS = {
"/ws/**", // WebSocket connections (auth handled in WebSocketAuthInterceptor)
"/kobo/**", // Kobo API requests (auth handled in KoboAuthFilter)
"/api/docs", // API documentation UI
"/api/openapi.json", // OpenAPI spec (used by API documentation UI)
"/api/v1/auth/**", // Login and token refresh endpoints (must remain public)
"/api/v1/public-settings", // Public endpoint for checking OIDC or other app settings
"/api/v1/setup/**", // Setup wizard endpoints (must remain accessible before initial setup)
@@ -0,0 +1,15 @@
package org.booklore.controller;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
@ConditionalOnProperty(name = "app.api-docs.enabled", havingValue = "true", matchIfMissing = false)
public class ScalarController {
@GetMapping("/api/docs")
public String scalar() {
return "forward:/scalar.html";
}
}
@@ -2,6 +2,8 @@ app:
path-config: '/app/data'
bookdrop-folder: '/bookdrop'
version: ${APP_VERSION:development}
api-docs:
enabled: ${API_DOCS_ENABLED:false}
cors:
# Comma-separated list of allowed CORS origins.
@@ -93,9 +95,8 @@ spring:
springdoc:
api-docs:
enabled: false
swagger-ui:
enabled: false
enabled: ${app.api-docs.enabled:false}
path: /api/openapi.json
logging:
level:
@@ -0,0 +1,19 @@
<!doctype html>
<html lang="en">
<head>
<title>Grimmory API Reference</title>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1"
/>
</head>
<body>
<script
id="api-reference"
data-url="/api/openapi.json"
data-configuration='{"theme":"default","hideDownloadButton":false,"telemetry":false}'
></script>
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference@1.49.7/dist/browser/standalone.js"></script>
</body>
</html>
@@ -0,0 +1,30 @@
package org.booklore.controller;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import static org.assertj.core.api.Assertions.assertThat;
class ScalarControllerTest {
private final ScalarController controller = new ScalarController();
@Test
void shouldForwardToStaticScalarPage() {
assertThat(controller.scalar()).isEqualTo("forward:/scalar.html");
}
@Test
void staticScalarPageShouldDisableTelemetryAndPointToApiDocs() throws IOException {
String html = StreamUtils.copyToString(
new ClassPathResource("static/scalar.html").getInputStream(),
StandardCharsets.UTF_8
);
assertThat(html).contains("data-url=\"/api/openapi.json\"");
assertThat(html).contains("\"telemetry\":false");
}
}