Skip to content

Commit 221bac3

Browse files
committed
fix(ns-openapi-3-1): fix bug in replace empty el. plugin
Bug manifested itself in missing source maps after using the refractorPluginReplaceEmptyElements plugin. Refs #812
1 parent 2d4f8a4 commit 221bac3

File tree

6 files changed

+310
-200
lines changed

6 files changed

+310
-200
lines changed

packages/apidom-ns-asyncapi-2/src/refractor/plugins/replace-empty-element.ts

Lines changed: 215 additions & 176 deletions
Large diffs are not rendered by default.

packages/apidom-ns-asyncapi-2/test/refractor/plugins/replace-empty-element.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { expect } from 'chai';
1+
import { assert, expect } from 'chai';
22
import dedent from 'dedent';
3-
import { sexprs } from '@swagger-api/apidom-core';
3+
import { sexprs, SourceMapElement } from '@swagger-api/apidom-core';
44
import { parse } from '@swagger-api/apidom-parser-adapter-yaml-1-2';
55

66
import { refractorPluginReplaceEmptyElement, AsyncApi2Element } from '../../../src';
@@ -91,6 +91,27 @@ describe('refractor', function () {
9191
expect(sexprs(asyncApiElement)).toMatchSnapshot();
9292
});
9393
});
94+
95+
context('given AsyncAPI definition with empty values', function () {
96+
specify('should generate proper source maps', async function () {
97+
const yamlDefinition = dedent`
98+
asyncapi: 2.2.0
99+
info:
100+
`;
101+
const apiDOM = await parse(yamlDefinition, { sourceMap: true });
102+
const asyncApiElement = AsyncApi2Element.refract(apiDOM.result, {
103+
plugins: [refractorPluginReplaceEmptyElement()],
104+
}) as AsyncApi2Element;
105+
const { info: infoValue } = asyncApiElement;
106+
const sourceMap = infoValue?.meta.get('sourceMap');
107+
const { positionStart, positionEnd } = sourceMap;
108+
const expectedPosition = [1, 5, 21];
109+
110+
expect(infoValue?.meta.get('sourceMap')).to.be.an.instanceof(SourceMapElement);
111+
assert.isTrue(positionStart.equals(expectedPosition));
112+
assert.isTrue(positionEnd.equals(expectedPosition));
113+
});
114+
});
94115
});
95116
});
96117
});

packages/apidom-parser-adapter-asyncapi-yaml-2/test/adapter.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
1+
import fs from 'fs';
2+
import path from 'path';
13
import { assert } from 'chai';
4+
import dedent from 'dedent';
5+
import { isParseResultElement, SourceMapElement } from '@swagger-api/apidom-core';
6+
import { isAsyncApi2Element } from '@swagger-api/apidom-ns-asyncapi-2';
27

38
import * as adapter from '../src/adapter';
49

10+
const spec = fs.readFileSync(path.join(__dirname, 'fixtures', 'sample-api.yaml')).toString();
11+
512
describe('adapter', function () {
13+
it('should detect proper media type', function () {
14+
assert.isTrue(adapter.detect(spec));
15+
});
16+
17+
it('should parse', async function () {
18+
const parseResult = await adapter.parse(spec, { sourceMap: true });
19+
20+
assert.isTrue(isParseResultElement(parseResult));
21+
assert.isTrue(isAsyncApi2Element(parseResult.api));
22+
});
23+
624
context('given zero byte empty file', function () {
725
specify('should return empty parse result', async function () {
826
const parseResult = await adapter.parse('', { sourceMap: true });
@@ -26,4 +44,24 @@ describe('adapter', function () {
2644
assert.isTrue(parseResult.isEmpty);
2745
});
2846
});
47+
48+
context('given YAML with empty node', function () {
49+
specify('should generate source maps', async function () {
50+
const yamlSource = dedent`
51+
asyncapi: 2.2.0
52+
info:
53+
`;
54+
55+
const { result } = await adapter.parse(yamlSource, { sourceMap: true });
56+
// @ts-ignore
57+
const infoValue = result.get('info');
58+
const sourceMap = infoValue.meta.get('sourceMap');
59+
const { positionStart, positionEnd } = sourceMap;
60+
const expectedEmptyPosition = [1, 5, 21];
61+
62+
assert.instanceOf(sourceMap, SourceMapElement);
63+
assert.isTrue(positionStart.equals(expectedEmptyPosition));
64+
assert.isTrue(positionEnd.equals(expectedEmptyPosition));
65+
});
66+
});
2967
});

packages/apidom-parser-adapter-asyncapi-yaml-2/test/index.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

packages/apidom-parser-adapter-yaml-1-2/test/adapter-browser.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { assert } from 'chai';
2+
import dedent from 'dedent';
3+
import { SourceMapElement } from '@swagger-api/apidom-core';
24

35
import * as adapter from '../src/adapter-node';
46

@@ -26,4 +28,19 @@ describe('adapter-browser', function () {
2628
assert.isTrue(parseResult.isEmpty);
2729
});
2830
});
31+
32+
context('given YAML with empty node', function () {
33+
specify('should generate source maps', async function () {
34+
const yamlSource = dedent`
35+
mapping:
36+
sub-mapping:
37+
`;
38+
39+
const { result } = await adapter.parse(yamlSource, { sourceMap: true });
40+
// @ts-ignore
41+
const subMappingValue = result.get('mapping').get('sub-mapping');
42+
43+
assert.instanceOf(subMappingValue.meta.get('sourceMap'), SourceMapElement);
44+
});
45+
});
2946
});

packages/apidom-parser-adapter-yaml-1-2/test/adapter-node.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { assert } from 'chai';
2+
import dedent from 'dedent';
3+
import { SourceMapElement } from '@swagger-api/apidom-core';
24

35
import * as adapter from '../src/adapter-node';
46

@@ -26,4 +28,19 @@ describe('adapter-node', function () {
2628
assert.isTrue(parseResult.isEmpty);
2729
});
2830
});
31+
32+
context('given YAML with empty node', function () {
33+
specify('should generate source maps', async function () {
34+
const yamlSource = dedent`
35+
mapping:
36+
sub-mapping:
37+
`;
38+
39+
const { result } = await adapter.parse(yamlSource, { sourceMap: true });
40+
// @ts-ignore
41+
const subMappingValue = result.get('mapping').get('sub-mapping');
42+
43+
assert.instanceOf(subMappingValue.meta.get('sourceMap'), SourceMapElement);
44+
});
45+
});
2946
});

0 commit comments

Comments
 (0)