Skip to content

Commit

Permalink
fix(resolver): returned cached item for OpenAPI 3.1 normalization (#3281
Browse files Browse the repository at this point in the history
)

Co-authored-by: Vladimír Gorej <[email protected]>
  • Loading branch information
jux61 and char0n authored Dec 14, 2023
1 parent dc9626d commit c90fa37
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/resolver/strategies/openapi-3-1-apidom/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const normalize = (element) => {
*/
export const pojoAdapter = (normalizeFn) => (spec) => {
if (spec?.$$normalized) return spec;
if (pojoAdapter.cache.has(spec)) return spec;
if (pojoAdapter.cache.has(spec)) return pojoAdapter.cache.get(spec);

const openApiElement = OpenApi3_1Element.refract(spec);
const normalized = normalizeFn(openApiElement);
Expand Down
15 changes: 14 additions & 1 deletion test/resolver/strategies/openapi-3-1-apidom/normalize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import path from 'node:path';
import { toValue, StringElement } from '@swagger-api/apidom-core';
import { OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1';

import normalize from '../../../../../src/resolver/strategies/openapi-3-1-apidom/normalize.js';
import normalize, {
pojoAdapter,
} from '../../../../../src/resolver/strategies/openapi-3-1-apidom/normalize.js';

const fixturesPath = path.join(__dirname, '__fixtures__');

Expand Down Expand Up @@ -99,5 +101,16 @@ describe('helpers', () => {
});
});
});

describe('pojoAdapter', () => {
test('should returned cached normalized POJO', () => {
const spec = globalThis.loadJsonFile(path.join(fixturesPath, 'parameters.json'));
const openApiElement = OpenApi3_1Element.refract(spec);
const normalized1 = pojoAdapter(normalize)(openApiElement);
const normalized2 = toValue(pojoAdapter(normalize)(openApiElement));

expect(normalized1).toEqual(normalized2);
});
});
});
/* eslint-enable camelcase */

0 comments on commit c90fa37

Please sign in to comment.