-
Notifications
You must be signed in to change notification settings - Fork 0
feat(api): server swagger docs #102
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
6 commits
Select commit
Hold shift + click to select a range
1211dad
feat(api): serve swagger docs
meteorqz6 2499303
Merge branch 'main' into feature/swagger-docs-deploy
meteorqz6 90c2e74
fix(api): address swagger docs review
meteorqz6 49ee4c1
fix(api): document error response examples
meteorqz6 c5ef9be
Merge branch 'develop' into feature/swagger-docs-deploy
meteorqz6 ddbc50a
test(api): assert openapi response refs resolve
meteorqz6 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
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,40 @@ | ||
| import { readFileSync } from "node:fs"; | ||
| import { resolve } from "node:path"; | ||
| import { parse } from "yaml"; | ||
|
|
||
| type OpenApiServer = { | ||
| description?: string; | ||
| url: string; | ||
| }; | ||
|
|
||
| export type OpenApiDocument = { | ||
| servers?: OpenApiServer[]; | ||
| [key: string]: unknown; | ||
| }; | ||
|
|
||
| const OPENAPI_DOCUMENT_PATH = resolve(__dirname, "../../../docs/openapi.yaml"); | ||
|
|
||
| const trimTrailingSlash = (value: string) => value.replace(/\/+$/, ""); | ||
|
|
||
| export const getOpenApiDocument = (): OpenApiDocument => { | ||
| const document = parse(readFileSync(OPENAPI_DOCUMENT_PATH, "utf8")) as OpenApiDocument; | ||
| const publicBaseUrl = process.env.API_PUBLIC_BASE_URL?.trim(); | ||
|
|
||
| if (!publicBaseUrl) { | ||
| return document; | ||
| } | ||
|
|
||
| const normalizedPublicBaseUrl = trimTrailingSlash(publicBaseUrl); | ||
| const existingServers = document.servers ?? []; | ||
|
|
||
| return { | ||
| ...document, | ||
| servers: [ | ||
| { | ||
| url: normalizedPublicBaseUrl, | ||
| description: "Configured deployment", | ||
| }, | ||
| ...existingServers.filter((server) => server.url !== normalizedPublicBaseUrl), | ||
| ], | ||
| }; | ||
| }; |
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,115 @@ | ||
| import { beforeEach, describe, expect, it } from "@jest/globals"; | ||
| import request from "supertest"; | ||
|
|
||
| import { createApp } from "@/app"; | ||
| import { createFakePrisma } from "./helpers/fake-prisma"; | ||
|
|
||
| const resolveOpenApiRef = (document: any, ref: string) => | ||
| ref | ||
| .slice(2) | ||
| .split("/") | ||
| .reduce((value: any, key) => value?.[key], document); | ||
|
|
||
| const expectResponseExamplesToMatchStatusCodes = (document: any) => { | ||
| for (const pathItem of Object.values<any>(document.paths)) { | ||
| for (const operation of Object.values<any>(pathItem)) { | ||
| const responses = operation.responses; | ||
|
|
||
| if (!responses) { | ||
| continue; | ||
| } | ||
|
|
||
| for (const [statusCode, responseDefinition] of Object.entries<any>(responses)) { | ||
| const response = | ||
| typeof responseDefinition.$ref === "string" | ||
| ? resolveOpenApiRef(document, responseDefinition.$ref) | ||
| : responseDefinition; | ||
|
|
||
| if (typeof responseDefinition.$ref === "string") { | ||
| expect(response).toBeDefined(); | ||
| } | ||
|
|
||
| const jsonContent = response?.content?.["application/json"]; | ||
| const expectedStatusCode = Number(statusCode); | ||
|
|
||
| if (!Number.isInteger(expectedStatusCode) || !jsonContent) { | ||
| continue; | ||
| } | ||
|
|
||
| if (jsonContent.example?.statusCode !== undefined) { | ||
| expect(jsonContent.example.statusCode).toBe(expectedStatusCode); | ||
| } | ||
|
|
||
| for (const example of Object.values<any>(jsonContent.examples ?? {})) { | ||
| if (example.value?.statusCode !== undefined) { | ||
| expect(example.value.statusCode).toBe(expectedStatusCode); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| describe("docs routes", () => { | ||
| beforeEach(() => { | ||
| delete process.env.API_PUBLIC_BASE_URL; | ||
| delete process.env.WEB_APP_ORIGIN; | ||
| }); | ||
|
|
||
| it("serves Swagger UI", async () => { | ||
| const { prisma } = createFakePrisma(); | ||
| const app = createApp({ prisma }); | ||
|
|
||
| const response = await request(app).get("/api/docs/"); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(response.headers["content-type"]).toContain("text/html"); | ||
| expect(response.text).toContain("swagger-ui"); | ||
| }); | ||
|
|
||
| it("does not persist Swagger UI authorization in browser storage", async () => { | ||
| const { prisma } = createFakePrisma(); | ||
| const app = createApp({ prisma }); | ||
|
|
||
| const response = await request(app).get("/api/docs/swagger-ui-init.js"); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(response.text).not.toContain("persistAuthorization"); | ||
| }); | ||
|
|
||
| it("serves OpenAPI JSON with the configured public server first", async () => { | ||
| process.env.API_PUBLIC_BASE_URL = "https://api.example.com/api"; | ||
|
|
||
| const { prisma } = createFakePrisma(); | ||
| const app = createApp({ prisma }); | ||
|
|
||
| const response = await request(app).get("/api/openapi.json"); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(response.body.openapi).toBe("3.1.0"); | ||
| expect(response.body.servers[0]).toEqual({ | ||
| url: "https://api.example.com/api", | ||
| description: "Configured deployment", | ||
| }); | ||
| expect(response.body.paths["/staffing-rules"]).toBeDefined(); | ||
| expect( | ||
| response.body.components.responses.MinimumStaffingRuleBadRequest.content["application/json"] | ||
| .examples.closedDay.value.errorCode, | ||
| ).toBe("CLOSED_DAY"); | ||
| expect( | ||
| response.body.components.responses.MinimumStaffingRuleBadRequest.content["application/json"] | ||
| .examples.invalidTimeRange.value.errorCode, | ||
| ).toBe("INVALID_TIME_RANGE"); | ||
| expect( | ||
| response.body.components.responses.DuplicateEmail.content["application/json"].example, | ||
| ).toEqual({ | ||
| statusCode: 409, | ||
| errorCode: "DUPLICATE_EMAIL", | ||
| message: "이미 사용 중인 이메일입니다.", | ||
| }); | ||
| expect(response.body.paths["/auth/signup"].post.responses["409"].$ref).toBe( | ||
| "#/components/responses/DuplicateEmail", | ||
| ); | ||
| expectResponseExamplesToMatchStatusCodes(response.body); | ||
| }); | ||
| }); | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.