Skip to content

Commit 1c2b9f3

Browse files
authored
feat(openapi-parser): add application and accessCode to Swagger 2.0 upgrade (scalar#6272)
* feat: add application and accessCode to upgrader * docs(changeset): feat: add application and accessCode to swagger 2 upgrader * fix: types
1 parent 60c7bef commit 1c2b9f3

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

.changeset/stupid-years-sparkle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@scalar/openapi-parser': patch
3+
---
4+
5+
feat: add application and accessCode to swagger 2 upgrader

packages/openapi-parser/src/utils/upgrade-from-two-to-three.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,25 @@ describe('upgradeFromTwoToThree', () => {
418418
'write:pets': 'modify pets in your account',
419419
},
420420
},
421+
petstore_application: {
422+
type: 'oauth2',
423+
flow: 'application',
424+
tokenUrl: 'https://petstore.swagger.io/oauth/token',
425+
scopes: {
426+
'read:pets': 'read your pets',
427+
'write:pets': 'modify pets in your account',
428+
},
429+
},
430+
petstore_access_code: {
431+
type: 'oauth2',
432+
flow: 'accessCode',
433+
authorizationUrl: 'https://petstore.swagger.io/oauth/authorize',
434+
tokenUrl: 'https://petstore.swagger.io/oauth/token',
435+
scopes: {
436+
'read:pets': 'read your pets',
437+
'write:pets': 'modify pets in your account',
438+
},
439+
},
421440
},
422441
paths: {
423442
'/pets': {
@@ -454,6 +473,31 @@ describe('upgradeFromTwoToThree', () => {
454473
},
455474
},
456475
},
476+
petstore_application: {
477+
type: 'oauth2',
478+
flows: {
479+
clientCredentials: {
480+
tokenUrl: 'https://petstore.swagger.io/oauth/token',
481+
scopes: {
482+
'read:pets': 'read your pets',
483+
'write:pets': 'modify pets in your account',
484+
},
485+
},
486+
},
487+
},
488+
petstore_access_code: {
489+
type: 'oauth2',
490+
flows: {
491+
authorizationCode: {
492+
authorizationUrl: 'https://petstore.swagger.io/oauth/authorize',
493+
tokenUrl: 'https://petstore.swagger.io/oauth/token',
494+
scopes: {
495+
'read:pets': 'read your pets',
496+
'write:pets': 'modify pets in your account',
497+
},
498+
},
499+
},
500+
},
457501
})
458502

459503
expect(result.securityDefinitions).toBeUndefined()

packages/openapi-parser/src/utils/upgrade-from-two-to-three.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@ import type { UnknownObject } from '@scalar/types/utils'
33

44
import { traverse } from './traverse'
55

6+
/** Update the flow names to OpenAPI 3.1.0 format */
7+
const upgradeFlow = (flow: string): 'implicit' | 'password' | 'clientCredentials' | 'authorizationCode' => {
8+
switch (flow) {
9+
case 'application':
10+
return 'clientCredentials'
11+
case 'accessCode':
12+
return 'authorizationCode'
13+
case 'implicit':
14+
return 'implicit'
15+
case 'password':
16+
return 'password'
17+
default:
18+
return flow as never
19+
}
20+
}
21+
622
/**
723
* Upgrade Swagger 2.0 to OpenAPI 3.0
824
*
@@ -237,12 +253,14 @@ export function upgradeFromTwoToThree(originalSpecification: UnknownObject) {
237253
scopes?: Record<string, string>
238254
}
239255

256+
// Convert flow values to OpenAPI 3.1.0 format
257+
240258
// Assert that securitySchemes is of type OpenAPIV3.SecuritySchemeObject
241259
Object.assign((specification.components as OpenAPIV3.ComponentsObject).securitySchemes, {
242260
[key]: {
243261
type: 'oauth2',
244262
flows: {
245-
[flow as string]: Object.assign(
263+
[upgradeFlow(flow)]: Object.assign(
246264
{},
247265
authorizationUrl && { authorizationUrl },
248266
tokenUrl && { tokenUrl },

0 commit comments

Comments
 (0)