Skip to content

Commit

Permalink
fix: Fix Swagger urls not visible on route analysis (#935)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutnik committed May 25, 2021
1 parent 1ce9220 commit e858197
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ export type RouteAnalyzerFunction = (route: RouteMetadata, allRoutes: RouteMetad
// --------------------------------------------------------------------- //

export interface Facility {
generateRoutes(app: Readonly<PlumierApplication>): Promise<RouteMetadata[]>
generateRoutes(app: Readonly<PlumierApplication>, routes:RouteMetadata[]): Promise<RouteMetadata[]>
setup(app: Readonly<PlumierApplication>): void
preInitialize(app: Readonly<PlumierApplication>): Promise<void>
initialize(app: Readonly<PlumierApplication>, routes: RouteMetadata[]): Promise<void>
}

export class DefaultFacility implements Facility {
async generateRoutes(app: Readonly<PlumierApplication>): Promise<RouteMetadata[]> { return [] }
async generateRoutes(app: Readonly<PlumierApplication>, routes:RouteMetadata[]): Promise<RouteMetadata[]> { return [] }
setup(app: Readonly<PlumierApplication>): void { }
async preInitialize(app: Readonly<PlumierApplication>) { }
async initialize(app: Readonly<PlumierApplication>, routes: RouteMetadata[]) { }
Expand Down
2 changes: 1 addition & 1 deletion packages/plumier/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class Plumier implements PlumierApplication {
//generate routes
const routes: RouteMetadata[] = []
for (const facility of this.config.facilities) {
const genRoutes = await facility.generateRoutes(this)
const genRoutes = await facility.generateRoutes(this, routes)
routes.push(...genRoutes)
}
//run initialize
Expand Down
20 changes: 20 additions & 0 deletions packages/swagger/src/facility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
PlumierApplication,
response,
RouteMetadata,
VirtualRoute,
} from "@plumier/core"
import { ServeStaticMiddleware } from "@plumier/serve-static"
import { InfoObject } from "openapi3-ts"
Expand Down Expand Up @@ -119,6 +120,25 @@ export class SwaggerFacility extends DefaultFacility {
}, {} as ResultGroup)
}

async generateRoutes(app: Readonly<PlumierApplication>, routes: RouteMetadata[]): Promise<RouteMetadata[]> {
const group = this.groupRoutes(routes)
return Object.keys(group).map(key => {
return <VirtualRoute>{
kind: "VirtualRoute",
method: "get",
provider: SwaggerFacility,
url: key === this.defaultGroup ? this.opt.endpoint : appendRoute(this.opt.endpoint, key),
access: "Public",
openApiOperation: {
description: "Host the SwaggerUI",
tags: ["Swagger"],
parameters: [],
responses: { "200": { content: { "text/html": {} } } }
}
}
})
}

async initialize(app: Readonly<PlumierApplication>, routes: RouteMetadata[]): Promise<void> {
const path = dist.getAbsoluteFSPath()
const group = this.groupRoutes(routes)
Expand Down
48 changes: 48 additions & 0 deletions tests/behavior/swagger/__snapshots__/open-api.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,22 @@ Object {
},
"openapi": "3.0.0",
"paths": Object {
"/swagger": Object {
"get": Object {
"description": "Host the SwaggerUI",
"parameters": Array [],
"responses": Object {
"200": Object {
"content": Object {
"text/html": Object {},
},
},
},
"tags": Array [
"Swagger",
],
},
},
"/users": Object {
"get": Object {
"parameters": Array [],
Expand Down Expand Up @@ -1260,6 +1276,22 @@ Object {
},
"openapi": "3.0.0",
"paths": Object {
"/swagger": Object {
"get": Object {
"description": "Host the SwaggerUI",
"parameters": Array [],
"responses": Object {
"200": Object {
"content": Object {
"text/html": Object {},
},
},
},
"tags": Array [
"Swagger",
],
},
},
"/users": Object {
"get": Object {
"parameters": Array [],
Expand Down Expand Up @@ -1309,6 +1341,22 @@ Object {

exports[`Open API 3.0 Generation Path Should convert path properly 1`] = `
Object {
"/swagger": Object {
"get": Object {
"description": "Host the SwaggerUI",
"parameters": Array [],
"responses": Object {
"200": Object {
"content": Object {
"text/html": Object {},
},
},
},
"tags": Array [
"Swagger",
],
},
},
"/users/{id}/{type}/{index}": Object {
"get": Object {
"parameters": Array [
Expand Down
24 changes: 24 additions & 0 deletions tests/behavior/swagger/__snapshots__/swagger.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@ Array [
]
`;

exports[`Swagger Swagger UI Hosting Grouping Should host groups properly 1`] = `
Array [
Array [],
Array [
"Route Analysis Report",
],
Array [
"1. UsersController.get() -> GET /api/v1/users",
],
Array [],
Array [
"1. UsersController.get() -> GET /api/v2/users",
],
Array [],
Array [
"1. SwaggerFacility -> Public GET /swagger/v1",
],
Array [
"2. SwaggerFacility -> Public GET /swagger/v2",
],
Array [],
]
`;

exports[`Swagger Swagger UI Hosting Should able to render string option 1`] = `
"<!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
Expand Down
18 changes: 13 additions & 5 deletions tests/behavior/swagger/swagger.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Class, route } from "@plumier/core"
import { Class, Configuration, route } from "@plumier/core"
import supertest from "supertest"
import { SwaggerDisplayOption, SwaggerFacility } from "@plumier/swagger"

import { expectError, fixture } from "../helper"
import Plumier, { WebApiFacility, ControllerFacility } from 'plumier'
import { cleanupConsole } from "@plumier/testing"



Expand Down Expand Up @@ -93,15 +94,22 @@ describe("Swagger", () => {
})

describe("Grouping", () => {
function createApp(ctl: Class | Class[]) {
function createApp(ctl: Class | Class[], opt?: Partial<Configuration>) {
return new Plumier()
.set({ mode: "production" })
.set({ mode: "production", ...opt })
.set(new WebApiFacility())
.set(new SwaggerFacility())
.set(new ControllerFacility({ group: "v1", controller: ctl, rootPath: "api/v1" }))
.set(new ControllerFacility({ group: "v2", controller: ctl, rootPath: "api/v2" }))
.set(new SwaggerFacility())
}

it("Should host groups properly", async () => {
const mock = console.mock()
const app = await createApp(UsersController, { mode: "debug" }).initialize()
expect(cleanupConsole(mock.mock.calls)).toMatchSnapshot()
console.mockClear()
})

it("Should redirect to proper swagger UI", async () => {
const app = await createApp(UsersController).initialize()
const respV1 = await supertest(app.callback())
Expand Down Expand Up @@ -210,7 +218,7 @@ describe("Swagger", () => {
})
it("Should able disable from facility", async () => {
process.env.NODE_ENV = "production"
delete process.env.PLUM_ENABLE_SWAGGER
delete process.env.PLUM_ENABLE_SWAGGER
const app = await createApp(UsersController, "json")
await supertest(app.callback())
.get("/swagger/index")
Expand Down

0 comments on commit e858197

Please sign in to comment.