Skip to content

Restore set value type checks and per-element setLazy - #1398

Merged
seongahjo merged 1 commit into
mainfrom
fix/restore-set-value-handling
Nov 20, 2026
Merged

seongahjo merged 1 commit into
mainfrom
fix/restore-set-value-handling

Conversation

@seongahjo

@seongahjo seongahjo commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

1.2.x lost two 1.1.x behaviors around set values. A value that is not assignable to the property type was ignored, and a per-element setLazy called its supplier once per sample. This PR restores both.

It also handles Optional, Map.Entry and registered setLazy values the same way as other set values, so their contents are checked and rebuilt in each sample. This includes values set where the declared type does not say they are an Optional or a Map.Entry, such as an Object property.

This is the third of four stacked PRs, based on #1395:

  1. Do not take supertype type arguments for JDK value types and enums #1397 Do not take supertype type arguments for JDK value types and enums
  2. Fix setting JDK internal containers into Object properties #1395 Fix setting JDK internal containers into Object properties
  3. This PR
  4. Promote container elements from generator candidates #1399 Promote container elements from generator candidates

(Optional): Description

Set value type check (1.1.x behavior restored)

  • A value given by set, setLazy or register that is not assignable to the property type throws IllegalArgumentException on sample():
    The value is not assignable to the property type.
    path: $.value, property type: java.lang.String, value type: java.lang.Integer
    
    1.2.0 to 1.2.4 ignored such values without an error: the property became null, was generated anew, or held elements of the wrong type.
  • The check is subtype-based and accepts boxing, so Dog into Animal and Integer into int still pass.
  • It runs in one place, ValueProjectionAssembler.assembleNode, when a value is fixed on a node. Collections, maps, arrays, Optional, Map.Entry and objects are decomposed, so every element and field is checked against its own declared type at any depth.
  • Values.just(...) values and value generators such as Values.unique(...) are not checked, as in 1.1.x.

Consistent decomposition of set values

  • A set Optional or Map.Entry is decomposed like other containers. Its content, key and value are rebuilt on their own nodes, and nested container sizes are kept (ValueAnalyzer, ObjectValueExtractor, ValueDecomposer). Before, the instance was used as is, so its content was neither checked nor rebuilt for each sample.
  • Types.isSingleElementWrapper and Types.hasFixedChildren decide which containers have a fixed set of children (single-element wrappers and Map.Entry). ContainerDetector.getContainerSizeAtPath gives the size registered at a path whose content shares it, such as the list inside an Optional.
  • This also works when the declared type does not tell, such as an Object property, an Object container element or Optional<Object> content:
    • the type arguments of a Map.Entry or Optional value are inferred from its contents (GenericTypeResolverConverter)
    • final generic classes such as Optional and KeyValueHolder (from Map.entry(...)) get their type arguments, and Map.Entry values are resolved from Object (ValueAnalyzer)
    • a node whose type becomes Map.Entry from its set value is promoted from the candidate JavaNodeCandidateFactory creates for that type, the same way as a declared Map.Entry property. JvmNodeCandidateFactory.create accepts a null name, the same as the candidates it creates.
    • KotlinNodePromoter leaves Map.Entry candidates to JavaMapEntryNodePromoter, so an entry set into a Kotlin Any property is built as an entry too
    • the content that shares its container's path is resolved without the container's path-based generic resolver either, so the content of a set Optional<Child> stays Child instead of getting the Optional's type arguments
  • setLazy values of a registered builder are decomposed, checked and rebuilt like set values. A supplier that returns Values.just(...) keeps the instance, and one that returns an Arbitrary is sampled, the same as in the builder being sampled.

Per-element setLazy (1.1.x behavior restored)

  • setLazy("values[*]", supplier) calls the supplier for each element, so numbering works again. With size("values", 3) and two samples, the result is [[1, 2, 3], [4, 5, 6]] instead of [[1, 1, 1], [2, 2, 2]].
  • A lazy value is evaluated once per path per sample, so assembling a node again does not call the supplier again.
  • The elements produced this way keep their container sizes and merge with values set below the wildcard path.

Behavior changes

  • A set value that is not assignable to the property type throws instead of being ignored.
  • The content of a set Optional or Map.Entry, and an object returned by a registered setLazy, is a new instance in each sample instead of the given instance. Use Values.just(...) to keep the instance.
  • Per-element setLazy calls the supplier once per element instead of once per sample.

Known issue

The contents of Supplier, Function, Iterator and Stream values are not checked, because looking inside would call user code or consume the value. For example, set("value", () -> inner) on a Supplier<Supplier<String>> property, where inner is () -> "x", puts inner itself into the property, so value.get() returns a String. 1.1.x failed here with given type is not supported container. Workaround: set("value", Values.just(() -> inner)).

How Has This Been Tested?

  • Added SetValueAssignabilityTest in fixture-monkey-tests/java-tests (15 tests):
    • these throw: an Integer into a String property, a String into a POJO property, a lambda into a Supplier<String> property (its "x" result becomes the value), and a wrong-typed element or content in a list, nested list, map, Optional, Map.Entry, and a registered setLazy list or Optional
    • these pass: subtype values, boxed values into primitives, subtype elements, and assignable registered setLazy content
  • Added SetLazyEvaluationTest in fixture-monkey-tests/java-tests (10 tests):
    • the supplier is called once per sample for sample(), sampleList() and a registered builder
    • a per-element supplier is called per element, also in a registered builder and for object elements
    • per-element values keep element and nested container sizes and merge with values set below
    • a registered setLazy returning an Arbitrary is sampled
  • Added SetOptionalAndEntryDecompositionTest in fixture-monkey-tests/java-tests (11 tests):
    • object, list and nested container contents of Optional and Map.Entry
    • an AbstractMap.SimpleEntry and a set of entries into an Object property
    • a list and a JDK internal list in an Optional<Object>
  • Added 4 tests to SetObjectIsolationTest: Values.just from setLazy and from a registered setLazy keeps the instance, and an Optional and a registered setLazy object are new instances in each sample
  • Added 9 tests to SetJdkInternalContainerTest:
    • an empty List.of(), Arrays.asList, an unmodifiable list, HashMap.values() and a list with a null element into an Object property
    • an unmodifiable NavigableSet becomes a TreeSet without a registered implementation
    • List.of into an Object property and then set of an element, and setLazy of List.of
    • Map.entry(...) into an Object property
  • Added sampleListOfEntries to ContainerTypeTest: List<Map.Entry<String, Integer>> is generated with entries
  • Added contentOfSetOptionalKeepsItsOwnType to JvmNodeTreeTransformerTest
  • Added setMapEntryToObject to the Kotlin CustomizationTest
  • Ran object-farm-api, fixture-monkey, fixture-monkey-kotlin, java-tests, java-17-tests and kotlin-tests with checkstyle, Checker Framework and ktlint

Is the Document updated?

No documentation change is needed.

@seongahjo
seongahjo added this pull request to stack #1400 November 19, 2026 07:33
@seongahjo
seongahjo force-pushed the fix/restore-set-value-handling branch from ae7b894 to c690a92 Compare November 19, 2026 07:51
@seongahjo
seongahjo force-pushed the fix/restore-set-value-handling branch from c690a92 to 26f9599 Compare November 19, 2026 08:18
@seongahjo
seongahjo force-pushed the fix/restore-set-value-handling branch from 26f9599 to ac90523 Compare November 19, 2026 08:31
Base automatically changed from fix/1390-object-immutable-container to main November 19, 2026 08:45
@seongahjo
seongahjo force-pushed the fix/restore-set-value-handling branch from ac90523 to 0c5a85f Compare November 19, 2026 08:45
@seongahjo seongahjo added this to the 1.2.5 milestone Nov 20, 2026
@seongahjo
seongahjo force-pushed the fix/restore-set-value-handling branch 7 times, most recently from b4218ae to 23dd108 Compare November 20, 2026 05:46
- Throw when a set, setLazy or register value is not assignable to the property type
- Call a per-element setLazy supplier for each element
- Decompose, check and rebuild Optional, Map.Entry and registered setLazy values
- Infer the type arguments of Map.Entry and Optional values from their contents
- Build Map.Entry nodes for Map.Entry values set into Object properties and containers
@seongahjo
seongahjo force-pushed the fix/restore-set-value-handling branch from 23dd108 to 72dcafa Compare November 20, 2026 06:12
@seongahjo
seongahjo merged commit 5f52222 into main Nov 20, 2026
12 checks passed
@seongahjo
seongahjo deleted the fix/restore-set-value-handling branch November 20, 2026 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant