-
Notifications
You must be signed in to change notification settings - Fork 67
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
feat: add ability to define securitySchemes under components #25
Closed
viktor-ku
wants to merge
2
commits into
asteasolutions:master
from
viktor-ku:add-security-components
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,70 @@ | ||
import { OpenAPIGenerator } from '../src/openapi-generator'; | ||
import { OpenAPIRegistry } from '../src/openapi-registry'; | ||
import { SecuritySchemeObject } from 'openapi3-ts'; | ||
import { z } from 'zod'; | ||
import {extendZodWithOpenApi} from "../src"; | ||
|
||
const testDocConfig = { | ||
openapi: '3.0.0', | ||
info: { | ||
version: '1.0.0', | ||
title: 'Swagger Petstore', | ||
description: 'A sample API', | ||
termsOfService: 'http://swagger.io/terms/', | ||
license: { | ||
name: 'Apache 2.0', | ||
url: 'https://www.apache.org/licenses/LICENSE-2.0.html', | ||
}, | ||
}, | ||
servers: [{ url: 'v1' }], | ||
}; | ||
|
||
extendZodWithOpenApi(z); | ||
|
||
describe('securitySchemas', () => { | ||
const registry = new OpenAPIRegistry(); | ||
|
||
const bearerAuth = registry.registerSecurityScheme('bearerAuth', { | ||
type: 'http', | ||
scheme: 'bearer', | ||
bearerFormat: 'JWT', | ||
}) | ||
|
||
const Unit = registry.register('UnitDto', z.object({ | ||
id: z.string().uuid(), | ||
name: z.string(), | ||
})).openapi({description: 'unit'}) | ||
|
||
registry.registerPath({ | ||
path: '/units', | ||
method: 'get', | ||
security: [ | ||
bearerAuth.security(), | ||
], | ||
responses: { | ||
200: { | ||
mediaType: 'application/json', | ||
schema: Unit.array().openapi({description: 'Array of all units'}) | ||
} | ||
} | ||
}) | ||
|
||
const builder = new OpenAPIGenerator(registry.definitions) | ||
const doc = builder.generateDocument(testDocConfig); | ||
|
||
it('should have security in /units', () => { | ||
expect(doc.paths!['/units'].get.security).toStrictEqual([ | ||
{ | ||
bearerAuth: [] | ||
} | ||
]); | ||
}) | ||
|
||
it('should have securitySchemes', () => { | ||
expect(doc.components!.securitySchemes).toStrictEqual({bearerAuth: { | ||
bearerFormat: "JWT", | ||
scheme: "bearer", | ||
type: "http", | ||
}}); | ||
}) | ||
}) |
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
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.
From what I see you're aiming to provide a pure object definition for the
components.securitySchemes
property, right? If that is the case I see no reason for putting this through the registry and binding it into the generation logic. I'll try and showcase a different approach.In order to make this work we would only need to make sure the config is being merged correctly in
generateDocument
. I feel like it is not wrong to provide support for manually added components (if someone wants to do that).@viktor-ku @georgyangelov what do you think. I'd also love to here any possible benefits of getting it through the registry, since I wrote this on the fly and haven't tested particular cases.
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.
It's certainly reasonable, however I think that registry could serve a bit more than just a zod schema handler. The registry just sounds like a convenient way to just add stuff to it like security schemas and it would just know what to do with it.
Maybe it's better to rename it from
registry.registerSecuritySchema
to likeregistry.addSecuritySchema
implying that there is nothing to generate, just add to the registry?And then I don't believe it's necessary to have additional boilerplate around
generateDocument
, I believe the registry is therefore capable of handling exactly that: knowing when to add something to the components and then doing it.