Skip to content

Commit

Permalink
Fix setting recursive implementations of self reference object (#1086)
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo authored Nov 14, 2024
1 parent adb168e commit aa89fb8
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 13 deletions.
6 changes: 6 additions & 0 deletions docs/content/v1.1.x-kor/release-notes/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ menu:
docs:
weight: 100
---
sectionStart
## v.1.1.2
Fix setting recursive implementations of self reference object

sectionEnd

sectionStart
## v.1.1.1
Fix set a recursive object.
Expand Down
6 changes: 6 additions & 0 deletions docs/content/v1.1.x/release-notes/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ menu:
docs:
weight: 100
---
sectionStart
## v.1.1.2
Fix setting recursive implementations of self reference object

sectionEnd

sectionStart
## v.1.1.1
Fix set a recursive object.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Fixture Monkey
*
* Copyright (c) 2021-present NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.fixturemonkey.tests.java;

import java.util.List;

import lombok.Value;

class InterfaceTestSpecs {
public interface InterfaceObject {
Object getObject();
}

@Value
public static class InterfaceStringObject implements InterfaceObject {
String object;

@Override
public String getObject() {
return this.object;
}
}

@Value
public static class InterfaceIntegerObject implements InterfaceObject {
Integer object;

@Override
public Integer getObject() {
return object;
}
}

@Value
public static class InterfaceListObject implements InterfaceObject {
List<InterfaceObject> values;

@Override
public List<InterfaceObject> getObject() {
return values;
}
}

@Value
public static class InterfaceWrapperObject {
InterfaceObject value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import com.navercorp.fixturemonkey.api.introspector.FailoverIntrospector;
import com.navercorp.fixturemonkey.api.introspector.FieldReflectionArbitraryIntrospector;
import com.navercorp.fixturemonkey.api.lazy.LazyArbitrary;
import com.navercorp.fixturemonkey.api.plugin.InterfacePlugin;
import com.navercorp.fixturemonkey.api.type.TypeReference;
import com.navercorp.fixturemonkey.customizer.InnerSpec;
import com.navercorp.fixturemonkey.customizer.Values;
Expand Down Expand Up @@ -89,6 +90,11 @@
import com.navercorp.fixturemonkey.tests.java.ImmutableRecursiveTypeSpecs.SelfRecursiveListObject;
import com.navercorp.fixturemonkey.tests.java.ImmutableRecursiveTypeSpecs.SelfRecursiveMapObject;
import com.navercorp.fixturemonkey.tests.java.ImmutableRecursiveTypeSpecs.SelfRecursiveObject;
import com.navercorp.fixturemonkey.tests.java.InterfaceTestSpecs.InterfaceIntegerObject;
import com.navercorp.fixturemonkey.tests.java.InterfaceTestSpecs.InterfaceListObject;
import com.navercorp.fixturemonkey.tests.java.InterfaceTestSpecs.InterfaceObject;
import com.navercorp.fixturemonkey.tests.java.InterfaceTestSpecs.InterfaceStringObject;
import com.navercorp.fixturemonkey.tests.java.InterfaceTestSpecs.InterfaceWrapperObject;
import com.navercorp.fixturemonkey.tests.java.MutableJavaTestSpecs.ConstantObject;
import com.navercorp.fixturemonkey.tests.java.NestedClassTestSpecs.Inner;
import com.navercorp.fixturemonkey.tests.java.NoArgsConstructorJavaTestSpecs.NestedObject;
Expand Down Expand Up @@ -1346,4 +1352,35 @@ void setExpCollectionElement() {

then(actual).isEqualTo("test");
}

@RepeatedTest(TEST_COUNT)
void setListRecursiveImplementations() {
// given
FixtureMonkey sut = FixtureMonkey.builder()
.objectIntrospector(ConstructorPropertiesArbitraryIntrospector.INSTANCE)
.plugin(new InterfacePlugin()
.interfaceImplements(
InterfaceObject.class,
Arrays.asList(
InterfaceStringObject.class,
InterfaceIntegerObject.class,
InterfaceListObject.class
)
)
)
.build();

List<InterfaceObject> element = sut.giveMeOne(new TypeReference<List<InterfaceStringObject>>() {
}).stream()
.map(InterfaceObject.class::cast)
.collect(Collectors.toList());
InterfaceListObject expected = new InterfaceListObject(element);

// when
InterfaceWrapperObject actual = sut.giveMeBuilder(InterfaceWrapperObject.class)
.set("value", expected)
.sample();

then(actual.getValue()).isEqualTo(expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public void forceExpand() {
if (this.getTreeProperty().isContainer()) {
return this.expandContainerNode(
typeDefinition,
traverseContext.withoutRecursiveTreeProperties()
traverseContext.withParentProperties()
);
}

Expand All @@ -349,7 +349,7 @@ public void forceExpand() {
typeDefinition.getPropertyGenerator()
.generateChildProperties(typeDefinition.getResolvedProperty()),
this.nullInject,
traverseContext.withoutRecursiveTreeProperties()
traverseContext.withParentProperties()
).stream();
}
).collect(Collectors.toList());
Expand All @@ -363,7 +363,7 @@ public void forceExpand(TypeDefinition typeDefinition) {
if (this.getTreeProperty().isContainer()) {
newChildren = expandContainerNode(
typeDefinition,
this.traverseContext.withoutRecursiveTreeProperties()
this.traverseContext.withParentProperties()
)
.collect(Collectors.toList());
} else {
Expand All @@ -372,7 +372,7 @@ public void forceExpand(TypeDefinition typeDefinition) {
typeDefinition.getPropertyGenerator()
.generateChildProperties(typeDefinition.getResolvedProperty()),
this.nullInject,
this.traverseContext.withoutRecursiveTreeProperties()
this.traverseContext.withParentProperties()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,11 @@ public TreeProperty getLastTreeProperty() {
return this.treeProperties.get(this.treeProperties.size() - 1);
}

public TraverseContext withoutRecursiveTreeProperties() {
List<TreeProperty> newTreeProperties = new ArrayList<>(this.treeProperties);
for (int i = 1; i < this.treeProperties.size(); i++) {
Property rootProperty = getRootTreeProperty().getObjectProperty().getProperty();
TreeProperty treeProperty = treeProperties.get(i);
if (isSameType(rootProperty, treeProperty.getObjectProperty().getProperty())) {
newTreeProperties = newTreeProperties.subList(i, newTreeProperties.size());
break;
}
public TraverseContext withParentProperties() {
List<TreeProperty> newTreeProperties = new ArrayList<>();

if (!this.treeProperties.isEmpty()) {
newTreeProperties.add(this.treeProperties.get(this.treeProperties.size() - 1));
}

return new TraverseContext(
Expand Down

0 comments on commit aa89fb8

Please sign in to comment.