Repository navigation
Allow generating a list of strict operation IDs #2414
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
The generated code presently passes a strict operation ID to every `StrictMiddlewareFunc` you have configured for a server. This is great for writing auth-like middleware: you can have a unified function that handles authorization on the *operational* level, and test it by itself. This is not so great if you're forgetful (like the author of this MR) and sometimes forget to write those tests... or to update your handler function. By generating a list of all operation handlers, we make it much easier to write a test that asserts that some `StrictMiddlewareFunc` has proper behavior for *all* operation IDs.
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # yaml-language-server: $schema=../../../configuration-schema.json | ||
| package: operationlist | ||
| generate: | ||
| strict-operation-list: true | ||
| output: operation_list.gen.go |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| package operationlist | ||
|
|
||
| //go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.yaml spec.yaml |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package operationlist | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestStrictOperationIDs(t *testing.T) { | ||
| expected := []string{"ListUsers", "CreateUser", "GetUser", "DeleteUser"} | ||
| assert.ElementsMatch(t, expected, StrictOperationIDs) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| openapi: "3.0.1" | ||
| info: | ||
| version: 1.0.0 | ||
| title: Operation List Test | ||
| paths: | ||
| /users: | ||
| get: | ||
| summary: List users | ||
| operationId: listUsers | ||
| responses: | ||
| '200': | ||
| description: OK | ||
| post: | ||
| summary: Create user | ||
| operationId: createUser | ||
| responses: | ||
| '201': | ||
| description: Created | ||
| /users/{id}: | ||
| get: | ||
| summary: Get user | ||
| operationId: getUser | ||
| parameters: | ||
| - name: id | ||
| in: path | ||
| required: true | ||
| schema: | ||
| type: string | ||
| responses: | ||
| '200': | ||
| description: OK | ||
| delete: | ||
| summary: Delete user | ||
| operationId: deleteUser | ||
| parameters: | ||
| - name: id | ||
| in: path | ||
| required: true | ||
| schema: | ||
| type: string | ||
| responses: | ||
| '204': | ||
| description: No Content |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // StrictOperationIDs lists the operation name string passed as the second | ||
| // argument to every StrictMiddlewareFunc call. Each entry is the normalized | ||
| // Go identifier for the operation (e.g. "ListUsers"), matching exactly what | ||
| // the strict handler passes when it invokes the middleware chain. | ||
| var StrictOperationIDs = []string{ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| {{- range .OperationIDs}} | ||
| "{{.}}", | ||
| {{- end}} | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.