Skip to content

Commit

Permalink
feat(normalization): introduce normalization for OpenAPI 3.1.0
Browse files Browse the repository at this point in the history
Refs #2743
  • Loading branch information
char0n committed Jan 23, 2023
1 parent a432487 commit 35e53d5
Show file tree
Hide file tree
Showing 10 changed files with 427 additions and 4 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@
},
"dependencies": {
"@babel/runtime-corejs3": "^7.11.2",
"@swagger-api/apidom-core": "^0.61.0",
"@swagger-api/apidom-reference": "^0.61.0",
"@swagger-api/apidom-ns-openapi-3-1": "^0.61.0",
"@swagger-api/apidom-json-pointer": "^0.61.0",
"@swagger-api/apidom-core": "^0.62.0",
"@swagger-api/apidom-reference": "^0.62.0",
"@swagger-api/apidom-ns-openapi-3-1": "^0.62.0",
"@swagger-api/apidom-json-pointer": "^0.62.0",
"cookie": "~0.5.0",
"cross-fetch": "^3.1.5",
"deepmerge": "~4.2.2",
Expand Down
41 changes: 41 additions & 0 deletions src/helpers/normalize/openapi-3-1.js
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;
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 test/helpers/normalize/openapi-3-1/__fixtures__/operation-ids.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"openapi": "3.1.0",
"paths": {
"/": {
"get": {
"operationId": "get operation ^"
}
}
}
}
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 test/helpers/normalize/openapi-3-1/__fixtures__/parameters.json
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": {}
}
}
}
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 test/helpers/normalize/openapi-3-1/__fixtures__/servers.json
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 test/helpers/normalize/openapi-3-1/__snapshots__/index.js.snap
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/",
},
],
}
`;
Loading

0 comments on commit 35e53d5

Please sign in to comment.