Skip to content

Commit 2bece62

Browse files
Type: Improved type for SecurityObject to include oauth2 & apiKey (#253)
* Updated SecurityObject to contain more than basic type * Fixed SecurityObject scheme being interpreted as type and vice versa * Fixed SecurityObject type being written from wrong documentation --------- Co-authored-by: Kevin Julián Martínez Escobar <[email protected]>
1 parent 1d35593 commit 2bece62

File tree

4 files changed

+175
-14
lines changed

4 files changed

+175
-14
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"ecmaVersion": 11
1818
},
1919
"rules": {
20+
"linebreak-style": ["error", "windows"],
2021
"arrow-parens": [
2122
"error",
2223
"as-needed",

index.d.ts

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,67 @@ interface InfoObject {
2929
license?: LicenseObject;
3030
}
3131

32-
interface SecurityObject {
33-
type: string;
34-
scheme: string;
32+
33+
interface FlowObjectDefaults {
34+
refreshUrl?: string
35+
scopes: { [key: string]: string }
3536
}
37+
type SecurityObject =
38+
| {
39+
description?: string
40+
} & (
41+
| {
42+
type: "mutualTLS"
43+
}
44+
| {
45+
type: "apiKey"
46+
name: string
47+
in: "query" | "header" | "cookie"
48+
}
49+
| {
50+
type: "http"
51+
scheme:
52+
| "basic"
53+
| "digest"
54+
| "dpop"
55+
| "hoba"
56+
| "mutual"
57+
| "negotiate"
58+
| "oauth"
59+
| "scram-sha-1"
60+
| "scram-sha-256"
61+
| "vapid"
62+
}
63+
| {
64+
type: "http"
65+
scheme: "bearer"
66+
bearerFormat: string
67+
}
68+
| {
69+
type: "oauth2"
70+
flows: {
71+
implicit?: FlowObjectDefaults & {
72+
authorizationUrl: string
73+
}
74+
password?: FlowObjectDefaults & {
75+
tokenUrl: string
76+
}
77+
clientCredentials?: FlowObjectDefaults & {
78+
tokenUrl: string
79+
}
80+
authorizationCode?: FlowObjectDefaults & {
81+
authorizationUrl: string
82+
tokenUrl: string
83+
}
84+
}
85+
}
86+
| {
87+
type: "openIdConnect"
88+
openIdConnectUrl: string
89+
}
90+
)
91+
92+
3693

3794
interface Security {
3895
[key: string]: SecurityObject;

package-lock.json

Lines changed: 53 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/transforms/security/index.test.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('Transform security schemas', () => {
3434
expect(result).toEqual(expected);
3535
});
3636

37-
it('Should return a security array and securitySchemes within components field', () => {
37+
it('Should return a security array and securitySchemes within components field for http', () => {
3838
const input = {
3939
security: {
4040
BasicAuth: {
@@ -62,6 +62,66 @@ describe('Transform security schemas', () => {
6262
expect(result).toEqual(expected);
6363
});
6464

65+
it('Should return a security array and securitySchemes within components field for http', () => {
66+
const input = {
67+
security: {
68+
HTTPAuth: {
69+
type: 'http',
70+
scheme: "basic",
71+
bearerFormat: 'JWT'
72+
},
73+
},
74+
};
75+
const expected = {
76+
security: [
77+
{
78+
HTTPAuth: [],
79+
},
80+
],
81+
components: {
82+
securitySchemes: {
83+
HTTPAuth: {
84+
type: 'http',
85+
scheme: "basic",
86+
bearerFormat: 'JWT'
87+
},
88+
},
89+
},
90+
};
91+
const result = parseSecuritySchemas(input);
92+
expect(result).toEqual(expected);
93+
});
94+
95+
it('Should return a security array and securitySchemes within components field for apiKey', () => {
96+
const input = {
97+
security: {
98+
ApiKeyAuth: {
99+
type: "apiKey",
100+
in: "header",
101+
name: "api_key",
102+
},
103+
},
104+
};
105+
const expected = {
106+
security: [
107+
{
108+
ApiKeyAuth: [],
109+
},
110+
],
111+
components: {
112+
securitySchemes: {
113+
ApiKeyAuth: {
114+
type: "apiKey",
115+
in: "header",
116+
name: "api_key",
117+
},
118+
},
119+
},
120+
};
121+
const result = parseSecuritySchemas(input);
122+
expect(result).toEqual(expected);
123+
});
124+
65125
it('Should return a security array and securitySchemes, both with each security type', () => {
66126
const input = {
67127
security: {

0 commit comments

Comments
 (0)