Skip to content

Commit 78e3403

Browse files
authored
Merge pull request #419 from OpenAPI-Qraft/feat/top-level-security-support
feat: root level security support
2 parents a57740d + a2327db commit 78e3403

16 files changed

Lines changed: 817 additions & 17 deletions

File tree

.changeset/swift-sheep-report.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openapi-qraft/tanstack-query-react-plugin': minor
3+
---
4+
5+
Added an optional `--root-security` flag that applies the OpenAPI document’s top-level `security` as the default for operations that omit their own `security`, while any operation-level `security` still overrides it as defined by the specification.

packages/cli/src/bin.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ describe('CLI binary help output', () => {
117117
--explicit-import-extensions [extension] All import statements will contain an explicit file extension. Ideal for projects using ECMAScript modules. (choices: ".js", ".ts", preset: ".js")
118118
--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. Default: true when --plugin openapi-typescript is used.
119119
--queryable-write-operations [bool] Enable generation of query hooks (useQuery, useSuspenseQuery, etc.) for writable HTTP methods like POST, PUT, PATCH. By default, only mutation hooks are generated for writable operations.
120+
--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.
120121
--create-api-client-fn <functionName> [options...] Configure API client creation function. Allows specifying the function name, included services, and callbacks. Can be specified multiple times to generate several different API client functions from a single OpenAPI document. (default: null)
121122
--override-import-type <pathname overrides...> Override import paths for specific types in generated files. This allows using custom type implementations instead of the default ones. Expected format: filepath originalModule:importTypeName:customImportPath
122123
--operation-parameters-type-wrapper <parameters wrappers...> Configure ParametersWrapper types for specific operation patterns. Expected format: pattern type:TypeName import:ImportPath. Can be specified multiple times for different patterns.
@@ -214,6 +215,7 @@ describe('CLI binary help output', () => {
214215
--explicit-import-extensions [extension] All import statements will contain an explicit file extension. Ideal for projects using ECMAScript modules. (choices: ".js", ".ts", preset: ".js")
215216
--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. Default: true when --plugin openapi-typescript is used. (default: true)
216217
--queryable-write-operations [bool] Enable generation of query hooks (useQuery, useSuspenseQuery, etc.) for writable HTTP methods like POST, PUT, PATCH. By default, only mutation hooks are generated for writable operations.
218+
--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.
217219
--create-api-client-fn <functionName> [options...] Configure API client creation function. Allows specifying the function name, included services, and callbacks. Can be specified multiple times to generate several different API client functions from a single OpenAPI document. (default: null)
218220
--override-import-type <pathname overrides...> Override import paths for specific types in generated files. This allows using custom type implementations instead of the default ones. Expected format: filepath originalModule:importTypeName:customImportPath
219221
--operation-parameters-type-wrapper <parameters wrappers...> Configure ParametersWrapper types for specific operation patterns. Expected format: pattern type:TypeName import:ImportPath. Can be specified multiple times for different patterns.

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

packages/openapi-plugin/src/lib/open-api/OpenAPISchemaType.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export type OpenAPISchemaType = {
44
title: string;
55
version: string;
66
};
7+
security?: Array<Record<string, string[] | undefined>>;
78
paths: {
89
[path: string]: {
910
[method: string]: {
@@ -23,7 +24,7 @@ export type OpenAPISchemaType = {
2324
required?: boolean;
2425
};
2526
responses: {
26-
[statusCode in number | 'default']: {
27+
[statusCode in number | 'default']?: {
2728
$ref?: string;
2829
description?: string;
2930
content?: {

packages/openapi-plugin/src/lib/open-api/getServices.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export const getServices = (
6666
>
6767
>(
6868
(acc, [statusCode, response]) => {
69+
if (!response) return acc;
6970
if (response.$ref) {
7071
response = resolveDocumentLocalRef(
7172
response.$ref,

0 commit comments

Comments
 (0)