Skip to content

Commit

Permalink
feat(core): handle RefElement serialization in toValue serializer (#3901
Browse files Browse the repository at this point in the history
)

Refs #3894
  • Loading branch information
char0n committed Mar 7, 2024
1 parent 200031f commit f205211
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
@@ -1,9 +1,9 @@
class EphemeralArray {
type = 'EphemeralArray';
public readonly type = 'EphemeralArray';

content: any[] = [];
protected readonly content: any[] = [];

reference: any = undefined;
protected readonly reference: any = undefined;

constructor(content: any[]) {
this.content = content;
Expand Down
@@ -1,9 +1,9 @@
class EphemeralObject {
type = 'EphemeralObject';
public readonly type = 'EphemeralObject';

content: any[] = [];
protected readonly content: any[] = [];

reference: any = undefined;
protected readonly reference: any = undefined;

constructor(content: any[]) {
this.content = content;
Expand Down
10 changes: 8 additions & 2 deletions packages/apidom-core/src/transformers/serializers/value/index.ts
Expand Up @@ -85,8 +85,14 @@ class Visitor {
return null;
}

public RefElement(element: RefElement): unknown {
return element.toValue();
public RefElement(element: RefElement, ...rest: unknown[]) {
const ancestors = rest[3] as (Element | EphemeralArray | EphemeralObject)[];

if (ancestors[ancestors.length - 1]?.type === 'EphemeralObject') {
return Symbol.for('delete-node');
}

return String(element.toValue());
}

public LinkElement(element: LinkElement): string {
Expand Down
12 changes: 12 additions & 0 deletions packages/apidom-core/test/transformers/serializers/value.ts
Expand Up @@ -154,6 +154,18 @@ describe('serializers', function () {
assert.deepEqual(serialized, { ref: 'id' });
});
});

context('and included instead of MemberElement', function () {
specify('should remove from serialization', function () {
const object = new ObjectElement({
a: 'b',
});
object.content.push(new RefElement('id'));
const serialized = serializer(object);

assert.deepEqual(serialized, { a: 'b' });
});
});
});

context('given LinkElement', function () {
Expand Down

0 comments on commit f205211

Please sign in to comment.