-
Notifications
You must be signed in to change notification settings - Fork 761
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(normalization): introduce normalization for OpenAPI 3.1.0
Refs #2743
- Loading branch information
Showing
10 changed files
with
427 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { dispatchRefractorPlugins, isObjectElement } from '@swagger-api/apidom-core'; | ||
import { | ||
refractorPluginNormalizeOperationIds, | ||
refractorPluginNormalizeParameters, | ||
refractorPluginNormalizeSecurityRequirements, | ||
refractorPluginNormalizeServers, | ||
refractorPluginNormalizeParameterExamples, | ||
refractorPluginNormalizeHeaderExamples, | ||
createToolbox, | ||
keyMap, | ||
getNodeType, | ||
} from '@swagger-api/apidom-ns-openapi-3-1'; | ||
|
||
import opId from '../op-id.js'; | ||
|
||
const normalize = (element) => { | ||
if (!isObjectElement(element)) return element; | ||
if (element.hasKey('$$normalized')) return element; | ||
|
||
const plugins = [ | ||
refractorPluginNormalizeOperationIds({ | ||
operationIdNormalizer: (operationId, path, method) => | ||
opId({ operationId }, path, method, { v2OperationIdCompatibilityMode: false }), | ||
}), | ||
refractorPluginNormalizeParameters(), | ||
refractorPluginNormalizeSecurityRequirements(), | ||
refractorPluginNormalizeServers(), | ||
refractorPluginNormalizeParameterExamples(), | ||
refractorPluginNormalizeHeaderExamples(), | ||
]; | ||
|
||
const normalized = dispatchRefractorPlugins(element, plugins, { | ||
toolboxCreator: createToolbox, | ||
visitorOptions: { keyMap, nodeTypeGetter: getNodeType }, | ||
}); | ||
|
||
normalized.set('$$normalized', true); | ||
return normalized; | ||
}; | ||
|
||
export default normalize; |
28 changes: 28 additions & 0 deletions
28
test/helpers/normalize/openapi-3-1/__fixtures__/header-examples.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"openapi": "3.1.0", | ||
"paths": { | ||
"/": { | ||
"get": { | ||
"responses": { | ||
"200": { | ||
"headers": { | ||
"content-type": { | ||
"schema": { | ||
"type": "number", | ||
"examples": [ | ||
1 | ||
] | ||
}, | ||
"examples": { | ||
"example1": { | ||
"value": 2 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
test/helpers/normalize/openapi-3-1/__fixtures__/operation-ids.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"openapi": "3.1.0", | ||
"paths": { | ||
"/": { | ||
"get": { | ||
"operationId": "get operation ^" | ||
} | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
test/helpers/normalize/openapi-3-1/__fixtures__/parameter-examples.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"openapi": "3.1.0", | ||
"paths": { | ||
"/": { | ||
"parameters": [ | ||
{ | ||
"name": "param1", | ||
"in": "query", | ||
"schema": { | ||
"type": "number", | ||
"examples": [ | ||
1 | ||
] | ||
}, | ||
"examples": { | ||
"example1": { | ||
"value": 2 | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
test/helpers/normalize/openapi-3-1/__fixtures__/parameters.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"openapi": "3.1.0", | ||
"paths": { | ||
"/": { | ||
"parameters": [ | ||
{ | ||
"name": "param1", | ||
"in": "query" | ||
}, | ||
{ | ||
"name": "param2", | ||
"in": "query" | ||
} | ||
], | ||
"get": {} | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
test/helpers/normalize/openapi-3-1/__fixtures__/security-requirements.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"openapi": "3.1.0", | ||
"security": [ | ||
{ | ||
"petstore_auth": [ | ||
"write:pets", | ||
"read:pets" | ||
] | ||
} | ||
], | ||
"paths": { | ||
"/": { | ||
"get": {} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
test/helpers/normalize/openapi-3-1/__fixtures__/servers.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"openapi": "3.1.0", | ||
"servers": [ | ||
{ | ||
"url": "https://example.com/", | ||
"description": "production server" | ||
} | ||
], | ||
"paths": { | ||
"/": { | ||
"get": {} | ||
} | ||
} | ||
} |
169 changes: 169 additions & 0 deletions
169
test/helpers/normalize/openapi-3-1/__snapshots__/index.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`helpers normalize OpenAPI 3.1 given denormalized Header Object examples should normalize 1`] = ` | ||
{ | ||
"$$normalized": true, | ||
"openapi": "3.1.0", | ||
"paths": { | ||
"/": { | ||
"get": { | ||
"responses": { | ||
"200": { | ||
"headers": { | ||
"content-type": { | ||
"examples": { | ||
"example1": { | ||
"value": 2, | ||
}, | ||
}, | ||
"schema": { | ||
"examples": [ | ||
2, | ||
], | ||
"type": "number", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`helpers normalize OpenAPI 3.1 given denormalized Operation.id fields should normalize 1`] = ` | ||
{ | ||
"$$normalized": true, | ||
"openapi": "3.1.0", | ||
"paths": { | ||
"/": { | ||
"get": { | ||
"__originalOperationId": "get operation ^", | ||
"operationId": "get_operation__", | ||
}, | ||
}, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`helpers normalize OpenAPI 3.1 given denormalized Parameter Object examples should normalize 1`] = ` | ||
{ | ||
"$$normalized": true, | ||
"openapi": "3.1.0", | ||
"paths": { | ||
"/": { | ||
"parameters": [ | ||
{ | ||
"examples": { | ||
"example1": { | ||
"value": 2, | ||
}, | ||
}, | ||
"in": "query", | ||
"name": "param1", | ||
"schema": { | ||
"examples": [ | ||
2, | ||
], | ||
"type": "number", | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`helpers normalize OpenAPI 3.1 given denormalized Parameter Objects should normalize 1`] = ` | ||
{ | ||
"$$normalized": true, | ||
"openapi": "3.1.0", | ||
"paths": { | ||
"/": { | ||
"get": { | ||
"parameters": [ | ||
{ | ||
"in": "query", | ||
"name": "param1", | ||
}, | ||
{ | ||
"in": "query", | ||
"name": "param2", | ||
}, | ||
], | ||
}, | ||
"parameters": [ | ||
{ | ||
"in": "query", | ||
"name": "param1", | ||
}, | ||
{ | ||
"in": "query", | ||
"name": "param2", | ||
}, | ||
], | ||
}, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`helpers normalize OpenAPI 3.1 given denormalized Security Requirements Objects should normalize 1`] = ` | ||
{ | ||
"$$normalized": true, | ||
"openapi": "3.1.0", | ||
"paths": { | ||
"/": { | ||
"get": { | ||
"security": [ | ||
{ | ||
"petstore_auth": [ | ||
"write:pets", | ||
"read:pets", | ||
], | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
"security": [ | ||
{ | ||
"petstore_auth": [ | ||
"write:pets", | ||
"read:pets", | ||
], | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`helpers normalize OpenAPI 3.1 given denormalized Servers Objects should normalize 1`] = ` | ||
{ | ||
"$$normalized": true, | ||
"openapi": "3.1.0", | ||
"paths": { | ||
"/": { | ||
"get": { | ||
"servers": [ | ||
{ | ||
"description": "production server", | ||
"url": "https://example.com/", | ||
}, | ||
], | ||
}, | ||
"servers": [ | ||
{ | ||
"description": "production server", | ||
"url": "https://example.com/", | ||
}, | ||
], | ||
}, | ||
}, | ||
"servers": [ | ||
{ | ||
"description": "production server", | ||
"url": "https://example.com/", | ||
}, | ||
], | ||
} | ||
`; |
Oops, something went wrong.