-
Notifications
You must be signed in to change notification settings - Fork 116
feat: generated OpenAPI, /docs page, FAQ, and Swagger UI #968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
431dc6d
feat(openapi): generate public REST spec with deterministic guardrail
northdpole c294637
docs: add FAQ markdown and serve from /docs/faq.md
northdpole 7fc2225
feat(frontend): add /docs page with Swagger UI and nav links
northdpole ee4ece1
fix(openapi): address CodeRabbit review nits
northdpole File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,7 @@ standards_cache.sqlite | |
| ### Docs | ||
| *.md | ||
| !AGENTS.md | ||
| !docs/faq.md | ||
|
|
||
| ### Dev DBDumps | ||
| *.sql | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
application/frontend/src/components/MarkdownFromRepo/MarkdownFromRepo.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import './markdownFromRepo.scss'; | ||
|
|
||
| import DOMPurify from 'dompurify'; | ||
| import { marked } from 'marked'; | ||
| import React, { useEffect, useState } from 'react'; | ||
|
|
||
| interface MarkdownFromRepoProps { | ||
| src: string; | ||
| className?: string; | ||
| errorHref?: string; | ||
| } | ||
|
|
||
| const FETCH_TIMEOUT_MS = 10_000; | ||
|
|
||
| const githubSourceHref = (src: string, errorHref?: string): string => { | ||
| if (errorHref) { | ||
| return errorHref; | ||
| } | ||
| const repoPath = src.replace(/^\//, ''); | ||
| return `https://github.com/OWASP/OpenCRE/blob/main/${repoPath}`; | ||
| }; | ||
|
|
||
| export const MarkdownFromRepo = ({ src, className = '', errorHref }: MarkdownFromRepoProps) => { | ||
| const [html, setHtml] = useState<string>(''); | ||
| const [error, setError] = useState<string | null>(null); | ||
| const [loading, setLoading] = useState(true); | ||
|
|
||
| useEffect(() => { | ||
| const controller = new AbortController(); | ||
| const timeout = window.setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS); | ||
|
|
||
| setLoading(true); | ||
| setError(null); | ||
|
|
||
| fetch(src, { signal: controller.signal }) | ||
| .then((res) => { | ||
| if (!res.ok) { | ||
| throw new Error(`Failed to load markdown (${res.status})`); | ||
| } | ||
| return res.text(); | ||
| }) | ||
| .then((markdown) => { | ||
| const parsed = marked.parse(markdown, { async: false }); | ||
| const rendered = DOMPurify.sanitize(String(parsed), { | ||
| USE_PROFILES: { html: true }, | ||
| }); | ||
| setHtml(rendered); | ||
| }) | ||
| .catch((err: Error) => { | ||
| if (err.name !== 'AbortError') { | ||
| setError(err.message); | ||
| } | ||
| }) | ||
| .finally(() => { | ||
| window.clearTimeout(timeout); | ||
| if (!controller.signal.aborted) { | ||
| setLoading(false); | ||
| } | ||
| }); | ||
|
|
||
| return () => { | ||
| controller.abort(); | ||
| window.clearTimeout(timeout); | ||
| }; | ||
| }, [src]); | ||
|
|
||
| if (loading) { | ||
| return <p className="markdown-from-repo__status">Loading…</p>; | ||
| } | ||
|
|
||
| if (error) { | ||
| return ( | ||
| <div className="markdown-from-repo__error"> | ||
| <p>{error}</p> | ||
| <p> | ||
| View the source on{' '} | ||
| <a href={githubSourceHref(src, errorHref)} target="_blank" rel="noreferrer"> | ||
| GitHub | ||
| </a> | ||
| . | ||
| </p> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <div className={`markdown-from-repo ${className}`.trim()} dangerouslySetInnerHTML={{ __html: html }} /> | ||
| ); | ||
| }; | ||
50 changes: 50 additions & 0 deletions
50
application/frontend/src/components/MarkdownFromRepo/markdownFromRepo.scss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| .markdown-from-repo { | ||
| color: var(--muted-foreground, #cbd5e1); | ||
| line-height: 1.7; | ||
| font-size: 1.05rem; | ||
|
|
||
| h2 { | ||
| color: #fff; | ||
| font-size: 1.35rem; | ||
| margin: 2rem 0 0.75rem; | ||
| } | ||
|
|
||
| h2:first-child { | ||
| margin-top: 0; | ||
| } | ||
|
|
||
| p { | ||
| margin: 0 0 1rem; | ||
| } | ||
|
|
||
| a { | ||
| color: #93c5fd; | ||
| text-decoration: none; | ||
|
|
||
| &:hover { | ||
| color: #bfdbfe; | ||
| text-decoration: underline; | ||
| } | ||
| } | ||
|
|
||
| ul, | ||
| ol { | ||
| margin: 0 0 1rem 1.25rem; | ||
| } | ||
|
|
||
| code { | ||
| background: rgba(148, 163, 184, 0.15); | ||
| padding: 0.1rem 0.35rem; | ||
| border-radius: 0.25rem; | ||
| font-size: 0.95em; | ||
| } | ||
| } | ||
|
|
||
| .markdown-from-repo__status, | ||
| .markdown-from-repo__error { | ||
| color: var(--muted-foreground, #cbd5e1); | ||
| } | ||
|
|
||
| .markdown-from-repo__error a { | ||
| color: #93c5fd; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import 'swagger-ui-react/swagger-ui.css'; | ||
|
|
||
| import './docs.scss'; | ||
|
|
||
| import React, { useEffect } from 'react'; | ||
| import SwaggerUI from 'swagger-ui-react'; | ||
|
|
||
| import { MarkdownFromRepo } from '../../components/MarkdownFromRepo/MarkdownFromRepo'; | ||
| import { useEnvironment } from '../../hooks'; | ||
|
|
||
| const scrollToHash = () => { | ||
| const hash = window.location.hash.replace('#', ''); | ||
| if (!hash) return; | ||
| const el = document.getElementById(hash); | ||
| if (el) { | ||
| el.scrollIntoView({ behavior: 'smooth' }); | ||
| } | ||
| }; | ||
|
|
||
| export const Docs = () => { | ||
| const { apiUrl } = useEnvironment(); | ||
|
|
||
| useEffect(() => { | ||
| window.scrollTo(0, 0); | ||
| scrollToHash(); | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| window.addEventListener('hashchange', scrollToHash); | ||
| return () => window.removeEventListener('hashchange', scrollToHash); | ||
| }, []); | ||
|
|
||
| return ( | ||
| <div className="docs-page"> | ||
| <header className="docs-page__hero"> | ||
| <h1 className="docs-page__title">Documentation</h1> | ||
| <p className="docs-page__subtitle"> | ||
| Getting started with OpenCRE, the public REST API, and frequently asked questions. | ||
| </p> | ||
| <nav className="docs-page__nav" aria-label="Docs sections"> | ||
| <a href="#getting-started">Getting started</a> | ||
| <a href="#api-reference">API reference</a> | ||
| <a href="#faq">FAQ</a> | ||
| <a href="#resources">Resources</a> | ||
| </nav> | ||
| </header> | ||
|
|
||
| <section id="getting-started" className="docs-section"> | ||
| <h2 className="docs-section__title">Getting started</h2> | ||
| <div className="docs-section__body"> | ||
| <p> | ||
| OpenCRE unifies security standards and guidelines through Common Requirements (CREs). Use the | ||
| homepage search, <a href="/root_cres">Browse</a>, <a href="/map_analysis">Map Analysis</a>, or the{' '} | ||
| <a href="/chatbot">Chat</a> assistant to explore mapped content. | ||
| </p> | ||
| <p> | ||
| Integrators can use the read-only REST API under <code>{apiUrl}</code>. The OpenAPI specification | ||
| is generated from the backend route registry and validated in CI. | ||
| </p> | ||
| </div> | ||
| </section> | ||
|
|
||
| <section id="api-reference" className="docs-section"> | ||
| <h2 className="docs-section__title">API reference</h2> | ||
| <p className="docs-section__intro"> | ||
| Interactive documentation for the public API. Raw spec:{' '} | ||
| <a href={`${apiUrl}/openapi.yaml`} target="_blank" rel="noreferrer"> | ||
| {apiUrl}/openapi.yaml | ||
| </a> | ||
| </p> | ||
| <div className="docs-page__swagger"> | ||
| <SwaggerUI url={`${apiUrl}/openapi.yaml`} docExpansion="list" defaultModelsExpandDepth={0} /> | ||
| </div> | ||
| </section> | ||
|
|
||
| <section id="faq" className="docs-section"> | ||
| <h2 className="docs-section__title">FAQ</h2> | ||
| <MarkdownFromRepo src="/docs/faq.md" /> | ||
| </section> | ||
|
|
||
| <section id="resources" className="docs-section"> | ||
| <h2 className="docs-section__title">Resources</h2> | ||
| <ul className="docs-resources"> | ||
| <li> | ||
| <a href="https://github.com/OWASP/OpenCRE/blob/main/README.md" target="_blank" rel="noreferrer"> | ||
| README | ||
| </a> | ||
| </li> | ||
| <li> | ||
| <a | ||
| href="https://github.com/OWASP/OpenCRE/blob/main/docs/CONTRIBUTING.md" | ||
| target="_blank" | ||
| rel="noreferrer" | ||
| > | ||
| Contributing | ||
| </a> | ||
| </li> | ||
| <li> | ||
| <a | ||
| href="https://github.com/OWASP/OpenCRE/blob/main/docs/my-opencre-user-guide.md" | ||
| target="_blank" | ||
| rel="noreferrer" | ||
| > | ||
| MyOpenCRE user guide | ||
| </a> | ||
| </li> | ||
| <li> | ||
| <a href="https://github.com/OWASP/OpenCRE" target="_blank" rel="noreferrer"> | ||
| GitHub repository | ||
| </a> | ||
| </li> | ||
| </ul> | ||
| </section> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default Docs; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Timeout aborts leave the component stuck on “Loading…” forever.
When the 10s timeout fires (Line 30),
controller.abort()rejects the fetch with anAbortError. ThecatchskipssetErrorbecauseerr.name === 'AbortError'(Line 50), andfinallyskipssetLoading(false)becausecontroller.signal.abortedistrue(Line 56). The net effect on a slow/failed network is a permanent “Loading…” spinner with no error and no GitHub fallback link.The
signal.abortedguard is trying to suppress state updates after unmount, but it can't distinguish an unmount-abort from a timeout-abort. Use an explicitactiveflag set tofalseonly in cleanup, and surface a timeout error to the user.🐛 Proposed fix
useEffect(() => { const controller = new AbortController(); + let active = true; const timeout = window.setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS); setLoading(true); setError(null); fetch(src, { signal: controller.signal }) .then((res) => { if (!res.ok) { throw new Error(`Failed to load markdown (${res.status})`); } return res.text(); }) .then((markdown) => { const parsed = marked.parse(markdown, { async: false }); const rendered = DOMPurify.sanitize(String(parsed), { USE_PROFILES: { html: true }, }); - setHtml(rendered); + if (active) { + setHtml(rendered); + } }) .catch((err: Error) => { - if (err.name !== 'AbortError') { - setError(err.message); - } + if (!active) { + return; + } + setError(err.name === 'AbortError' ? 'Request timed out' : err.message); }) .finally(() => { window.clearTimeout(timeout); - if (!controller.signal.aborted) { + if (active) { setLoading(false); } }); return () => { + active = false; controller.abort(); window.clearTimeout(timeout); }; }, [src]);📝 Committable suggestion
🤖 Prompt for AI Agents