-
-
Notifications
You must be signed in to change notification settings - Fork 214
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
Allow optional use of req.url
#857
Merged
Merged
Changes from all commits
Commits
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 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 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 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 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 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,272 @@ | ||
import { expect } from 'chai'; | ||
import type { | ||
Express, | ||
IRouter, | ||
Response, | ||
NextFunction, | ||
Request, | ||
} from 'express'; | ||
import * as express from 'express'; | ||
import { OpenAPIV3 } from '../src/framework/types'; | ||
import * as request from 'supertest'; | ||
import { createApp } from './common/app'; | ||
|
||
import * as OpenApiValidator from '../src'; | ||
import { Server } from 'http'; | ||
|
||
interface HTTPError extends Error { | ||
status: number; | ||
text: string; | ||
method: string; | ||
path: string; | ||
} | ||
|
||
describe('when useRequestUrl is set to "true" on the child router', async () => { | ||
let app: Express & { server?: Server }; | ||
|
||
before(async () => { | ||
const router = makeRouter({ useRequestUrl: true }); | ||
app = await makeMainApp(); | ||
app.use(router); | ||
}); | ||
|
||
after(() => app?.server?.close()); | ||
|
||
it('should apply parent app schema to requests', async () => { | ||
const result = await request(app).get('/api/pets/1'); | ||
const error = result.error as HTTPError; | ||
expect(result.statusCode).to.equal(400); | ||
expect(error.path).to.equal('/api/pets/1'); | ||
expect(error.text).to.contain( | ||
'Bad Request: request/params/petId must NOT have fewer than 3 characters', | ||
); | ||
}); | ||
|
||
it('should apply child router schema to requests', async () => { | ||
const result = await request(app).get('/api/pets/not-uuid'); | ||
const error = result.error as HTTPError; | ||
expect(result.statusCode).to.equal(400); | ||
expect(error.path).to.equal('/api/pets/not-uuid'); | ||
expect(error.text).to.contain( | ||
'Bad Request: request/params/petId must match format "uuid"', | ||
); | ||
}); | ||
|
||
it('should return a reponse if request is valid', async () => { | ||
const validId = 'f627f309-cae3-46d2-84f7-d03856c84b02'; | ||
const result = await request(app).get(`/api/pets/${validId}`); | ||
expect(result.statusCode).to.equal(200); | ||
expect(result.body).to.deep.equal({ | ||
id: 'f627f309-cae3-46d2-84f7-d03856c84b02', | ||
name: 'Mr Sparky', | ||
tag: "Ain't nobody tags me", | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when useRequestUrl is set to "false" on the child router', async () => { | ||
let app: Express & { server?: Server }; | ||
|
||
before(async () => { | ||
const router = makeRouter({ useRequestUrl: false }); | ||
app = await makeMainApp(); | ||
app.use(router); | ||
}); | ||
|
||
after(() => app?.server?.close()); | ||
|
||
it('should throw not found', async () => { | ||
const result = await request(app).get('/api/pets/valid-pet-id'); | ||
const error = result.error as HTTPError; | ||
expect(result.statusCode).to.equal(404); | ||
expect(error.path).to.equal('/api/pets/valid-pet-id'); | ||
expect(error.text).to.contain('Not Found'); | ||
}); | ||
}); | ||
|
||
function defaultResponse(): OpenAPIV3.ResponseObject { | ||
return { | ||
description: 'unexpected error', | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'object', | ||
required: ['code', 'message'], | ||
properties: { | ||
code: { | ||
type: 'integer', | ||
format: 'int32', | ||
}, | ||
message: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
/* | ||
represents spec of the "public" entrypoint to our application e.g gateway. The | ||
type of id in path and id in the response here defined as simple string | ||
with minLength | ||
*/ | ||
const gatewaySpec: OpenAPIV3.Document = { | ||
openapi: '3.0.0', | ||
info: { version: '1.0.0', title: 'test bug OpenApiValidator' }, | ||
servers: [{ url: 'http://localhost:3000/api' }], | ||
paths: { | ||
'/pets/{petId}': { | ||
get: { | ||
summary: 'Info for a specific pet', | ||
operationId: 'showPetById', | ||
tags: ['pets'], | ||
parameters: [ | ||
{ | ||
name: 'petId', | ||
in: 'path', | ||
required: true, | ||
description: 'The id of the pet to retrieve', | ||
schema: { | ||
type: 'string', | ||
minLength: 3, | ||
}, | ||
}, | ||
], | ||
responses: { | ||
'200': { | ||
description: 'Expected response to a valid request', | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'object', | ||
required: ['id', 'name'], | ||
properties: { | ||
id: { | ||
type: 'string', | ||
}, | ||
name: { | ||
type: 'string', | ||
}, | ||
tag: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
default: defaultResponse(), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
/* | ||
represents spec of the child router. We route request from main app (gateway) to this router. | ||
This router has its own schema, routes and validation formats. In particular, we force id in the path and id in the response to be uuid. | ||
*/ | ||
const childRouterSpec: OpenAPIV3.Document = { | ||
openapi: '3.0.0', | ||
info: { version: '1.0.0', title: 'test bug OpenApiValidator' }, | ||
servers: [{ url: 'http://localhost:3000/' }], | ||
paths: { | ||
'/internal/api/pets/{petId}': { | ||
get: { | ||
summary: 'Info for a specific pet', | ||
operationId: 'showPetById', | ||
tags: ['pets'], | ||
parameters: [ | ||
{ | ||
name: 'petId', | ||
in: 'path', | ||
required: true, | ||
description: 'The id of the pet to retrieve', | ||
schema: { | ||
type: 'string', | ||
format: 'uuid', | ||
}, | ||
}, | ||
], | ||
responses: { | ||
'200': { | ||
description: 'Expected response to a valid request', | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'object', | ||
required: ['id', 'name'], | ||
properties: { | ||
id: { | ||
type: 'string', | ||
format: 'uuid', | ||
}, | ||
name: { | ||
type: 'string', | ||
}, | ||
tag: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
function redirectToInternalService( | ||
req: Request, | ||
_res: Response, | ||
next: NextFunction, | ||
): void { | ||
req.url = `/internal${req.originalUrl}`; | ||
next(); | ||
} | ||
|
||
function makeMainApp(): ReturnType<typeof createApp> { | ||
return createApp( | ||
{ | ||
apiSpec: gatewaySpec, | ||
validateResponses: true, | ||
validateRequests: true, | ||
}, | ||
3000, | ||
(app) => { | ||
app | ||
.get( | ||
'/api/pets/:petId', | ||
function (_req: Request, _res: Response, next: NextFunction) { | ||
next(); | ||
}, | ||
) | ||
.use(redirectToInternalService); | ||
}, | ||
false, | ||
); | ||
} | ||
|
||
function makeRouter({ useRequestUrl }: { useRequestUrl: boolean }): IRouter { | ||
return express | ||
.Router() | ||
.use( | ||
OpenApiValidator.middleware({ | ||
apiSpec: childRouterSpec, | ||
validateRequests: true, | ||
validateResponses: true, | ||
useRequestUrl, | ||
}), | ||
) | ||
.get('/internal/api/pets/:petId', function (req, res) { | ||
res.json({ | ||
id: req.params.petId, | ||
name: 'Mr Sparky', | ||
tag: "Ain't nobody tags me", | ||
}); | ||
}); | ||
} |
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.
can you help me understand why this is an option controlled by the client? what is the downside using this behavior i.e.
req.path
andreq.url
for all requests? for instance, can we make both paths work withour requiring the client to configure the behaviorThere 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.
req.path
andreq.url
are localized by express to be router specific, so if we nest router A in router Breq.path
/req.url
will only have router A data (/test
), no router B data (/api/
). #211 introduced the current default behavior as the desired use case was that users were validating against a single spec that included router B data. I don't think there's an easy way without naive searching of the spec to handle both.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.
@cdimascio what @sennyeya said - using

req.path
orreq.url
is scoped to the router using it and is unaware of the url defined by the parent router. The flip side of this is that you can't use separate schemas in the parent router and child router, becauseoriginalUrl
is always set to the one defined by the parent router i.e/api
. But usingreq.url
in the child router allows us to decouple child and parent routers and schemas. Alsoreq.url
is intended to be used like so by the express framework I believe.