Skip to content

Commit b0f404d

Browse files
committed
Fix: Operation security should override top-level
REQUIRES wolfadex/elm-open-api#11
1 parent e7805e4 commit b0f404d

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
openapi: 3.0.1
2+
info:
3+
title: "Overriding global security"
4+
version: 1.0.0
5+
components:
6+
schemas:
7+
Data:
8+
description: "Data"
9+
type: string
10+
securitySchemes:
11+
Token:
12+
type: apiKey
13+
in: header
14+
name: X-API-Key
15+
security:
16+
- Token: []
17+
paths:
18+
"/api/protected":
19+
summary: Endpoint requiring token
20+
description: Description of protected endpoint
21+
get:
22+
operationId: GetProtectedData
23+
responses:
24+
200:
25+
$ref: "#/components/schemas/Data"
26+
27+
"/api/unprotected":
28+
summary: Unprotected (public) endpoint
29+
description: "Description of unprotected endpoint"
30+
get:
31+
security: []
32+
operationId: GetUnprotectedData
33+
responses:
34+
200:
35+
$ref: "#/components/schemas/Data"

src/OpenApi/Generate.elm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1494,7 +1494,9 @@ operationToAuthorizationInfo : OpenApi.Operation.Operation -> CliMonad Authoriza
14941494
operationToAuthorizationInfo operation =
14951495
CliMonad.andThen2
14961496
(\globalSecurity components ->
1497-
(OpenApi.Operation.security operation ++ globalSecurity)
1497+
-- If present, the operation's security overrides globalSecurity.
1498+
OpenApi.Operation.security operation
1499+
|> Maybe.withDefault globalSecurity
14981500
|> List.concatMap
14991501
(Dict.toList << OpenApi.SecurityRequirement.requirements)
15001502
|> CliMonad.foldl

src/TestGenScript.elm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ run =
2020
recursiveAllofRefs =
2121
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/recursive-allof-refs.yaml")
2222

23+
overridingGlobalSecurity : OpenApi.Config.Input
24+
overridingGlobalSecurity =
25+
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/overriding-global-security.yaml")
26+
2327
singleEnum : OpenApi.Config.Input
2428
singleEnum =
2529
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/single-enum.yaml")
@@ -70,6 +74,7 @@ run =
7074
OpenApi.Config.init "./generated"
7175
|> OpenApi.Config.withAutoConvertSwagger True
7276
|> OpenApi.Config.withInput recursiveAllofRefs
77+
|> OpenApi.Config.withInput overridingGlobalSecurity
7378
|> OpenApi.Config.withInput singleEnum
7479
|> OpenApi.Config.withInput patreon
7580
|> OpenApi.Config.withInput realworldConduit

0 commit comments

Comments
 (0)