Skip to content

Commit a35b226

Browse files
committed
feat: add --root-security option to support root-level OpenAPI security in generated schemas
1 parent d99e92d commit a35b226

3 files changed

Lines changed: 22 additions & 5 deletions

File tree

packages/openapi-cli/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Options:
3636
example: "post /**:[A-Za-z]+Id ==> createOne"
3737
--postfix-services <string> Postfix to be added to the generated service name (eg: Service)
3838
--service-name-base <endpoint[<index>] | tags> Use OpenAPI Operation `endpoint[<index>]` path part (e.g.: "/0/1/2") or `tags` as the base name of the service. (default: "endpoint[0]")
39+
--root-security Use root-level OpenAPI security as the default for operations without their own security. Operation-level security overrides it according to OpenAPI semantics.
3940
--file-header <string> Header to be added to the generated file (eg: /* eslint-disable */)
4041
--redocly [config] Use the Redocly configuration to generate multiple API clients
4142
If the [config] parameter is not specified, the default Redocly configuration will be used: [redocly.yaml | redocly.yml | .redocly.yaml |
@@ -210,6 +211,9 @@ The following plugins are currently supported:
210211
- `--openapi-types-import-path '@/api/openapi.d.ts'`
211212
- `--openapi-types-import-path '@external-package-types'`
212213
- **`--export-openapi-types [bool]`:** Add an export statement of the generated OpenAPI document types from the `./index.ts` file. Useful for sharing types within your project. _(optional, default: `true`, if `--plugin openapi-typescript` is used)_
214+
- **`--root-security`**: Use root-level OpenAPI `security` as the default for operations that do not define their own `security`. Operation-level `security` overrides it according to OpenAPI semantics. _(optional, disabled by default)_
215+
- Use this option when your OpenAPI document defines `security` on the top level and you want generated operation schemas to include those security requirements automatically.
216+
- Without this option, only operation-level `security` is emitted into generated schemas.
213217

214218
### `--plugin openapi-typescript` options
215219

website/docs/authorization/qraft-secure-request-fn.mdx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ See the [OpenAPI Authentication 🔗](https://swagger.io/docs/specification/auth
1717
This feature is still in the experimental stage 🧪 and may change in the future.
1818
:::
1919

20+
:::tip
21+
`QraftSecureRequestFn` relies on generated operation `schema.security`.
22+
23+
If your OpenAPI document defines `security` on the top level, generate the client with `--root-security`
24+
so those root-level security requirements are included in generated operation schemas.
25+
Without `--root-security`, only operation-level `security` is emitted by the generator.
26+
:::
27+
2028
```tsx
2129
import { createAPIClient } from './api'; // generated by OpenAPI Qraft CLI
2230
import { requestFn } from '@openapi-qraft/react';
@@ -100,11 +108,12 @@ In this example, the `mySecurityScheme` security scheme handler is used to obtai
100108
<TabItem value="1. Open API Document">
101109
```yaml
102110
openapi: 3.1.0
103-
securitySchemes:
104-
mySecurityScheme: # ⬅︎ Define security scheme
105-
type: http
106-
scheme: bearer
107-
bearerFormat: JWT
111+
components:
112+
securitySchemes:
113+
mySecurityScheme: # ⬅︎ Define security scheme
114+
type: http
115+
scheme: bearer
116+
bearerFormat: JWT
108117
paths:
109118
/pet:
110119
post:

website/docs/codegen/openapi/index.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@ It is possible to use multiple plugins at the same time. For example, `--plugin
226226
instead of command-line parameters. See the [Redocly configuration documentation](../cli/redocly-config.mdx#generating-multiple-api-client-functions)
227227
for more details on how to configure multiple _create API client functions_ in a YAML file.
228228
::::
229+
- **`--root-security`**: Use root-level OpenAPI `security` as the default for operations that do not define their own `security`. Operation-level `security` overrides it according to OpenAPI semantics. _(optional, disabled by default)_
230+
- Use this option when your OpenAPI document defines `security` on the top level and you want generated operation schemas to include those security requirements automatically.
231+
- Without this option, Qraft only includes operation-level `security` in generated schemas.
232+
- This is especially useful together with [`QraftSecureRequestFn`](../../authorization/qraft-secure-request-fn.mdx), because secure handlers rely on generated operation `schema.security`.
229233

230234
## `--plugin openapi-typescript` options
231235

0 commit comments

Comments
 (0)