Skip to content

Commit 070e32c

Browse files
authored
fix: respect user opt-out when adding overrides (#24123)
Adjust the checks for user opt-out in package.json overrides, so that an existing override for new entries added by Vaadin is considered as an opt-out, so that the existing user value is kept. This behavior satisfies the previously existing `NodeUpdatePackagesNpmVersionLockingTest.shouldNotUpdatesOverrides_whenHasUserModification` test.
1 parent beaed7c commit 070e32c

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

flow-build-tools/src/main/java/com/vaadin/flow/server/frontend/TaskUpdatePackages.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -191,25 +191,31 @@ boolean lockVersionForNpm(ObjectNode packageJson) throws IOException {
191191
// Override value didn't change, skipping.
192192
continue;
193193
}
194+
final JsonNode lastUserValue;
195+
final List<String> keyPath = List
196+
.of(entryToUpdate.getKey().split(">"));
197+
if (enablePnpm) {
198+
lastUserValue = overridesSection.get(entryToUpdate.getKey());
199+
} else {
200+
lastUserValue = JacksonUtils.getNestedKey(overridesSection,
201+
keyPath);
202+
}
203+
boolean optOut = !Objects.equals(StringNode.valueOf(lastValue),
204+
lastUserValue);
205+
if (optOut) {
206+
// Actual override value is different from last Vaadin override:
207+
// assume user opt-out and skip.
208+
continue;
209+
}
194210
versionLockingUpdated = true;
195211
if (enablePnpm) {
196212
// Use flat format for pnpm
197213
overridesSection.put(entryToUpdate.getKey(),
198214
entryToUpdate.getValue());
199-
continue;
200-
}
201-
// Handle possibly nested overrides object
202-
final List<String> keyPath = List
203-
.of(entryToUpdate.getKey().split(">"));
204-
final JsonNode lastValueNode = StringNode.valueOf(lastValue);
205-
if (lastValueNode != null && !lastValueNode.equals(
206-
JacksonUtils.getNestedKey(overridesSection, keyPath))) {
207-
// Actual override value is different from last Vaadin override:
208-
// assume opt-out and skip.
209-
continue;
215+
} else {
216+
putNestedOverride(overridesSection, keyPath,
217+
entryToUpdate.getValue());
210218
}
211-
putNestedOverride(overridesSection, keyPath,
212-
entryToUpdate.getValue());
213219
}
214220
for (final Map.Entry<String, String> entryToRemove : flatLastVaadinOverrides
215221
.entrySet()) {

flow-build-tools/src/test/java/com/vaadin/flow/server/frontend/NodeUpdatePackagesNpmVersionLockingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void shouldHandleNestedObjectOverrides_withoutError() throws IOException {
192192
packageUpdater.lockVersionForNpm(packageJson);
193193

194194
// Verify flat override stays flat
195-
JsonNode overrides = packageJson.get(PNPM).get(OVERRIDES);
195+
JsonNode overrides = packageJson.get(OVERRIDES);
196196
assertTrue(overrides.has("flat-dep"),
197197
"Flat override should remain unchanged");
198198
assertEquals("3.0.0", overrides.get("flat-dep").stringValue());

flow-build-tools/src/test/java/com/vaadin/flow/server/frontend/TaskUpdatePackagesNpmTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.slf4j.LoggerFactory;
4040
import tools.jackson.databind.JsonNode;
4141
import tools.jackson.databind.node.ObjectNode;
42+
import tools.jackson.databind.node.StringNode;
4243

4344
import com.vaadin.flow.internal.JacksonUtils;
4445
import com.vaadin.flow.server.Constants;
@@ -234,6 +235,11 @@ void overridesContainPinnedVersion_platformVersionUpdatedToNewerWhileDependencyA
234235
ObjectNode packageJson = getOrCreatePackageJson();
235236
packageJson.set(OVERRIDES, JacksonUtils.createObjectNode());
236237
((ObjectNode) packageJson.get(OVERRIDES)).put("@vaadin/aura", "1.0");
238+
// Assuming Vaadin added the override, set Vaadin overrides accordingly
239+
JacksonUtils.setNestedKey(packageJson,
240+
List.of(VAADIN_DEP_KEY, OVERRIDES, "@vaadin/aura"),
241+
StringNode.valueOf("1.0"),
242+
(nonObjectNode) -> JacksonUtils.createObjectNode());
237243

238244
FileUtils.writeStringToFile(new File(npmFolder, PACKAGE_JSON),
239245
packageJson.toPrettyString(), StandardCharsets.UTF_8);

0 commit comments

Comments
 (0)