-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fail fast on duplicate configuration resources (with configurable merge/first-match) #12383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3ffcf4b
Add configuration load strategy API
graemerocher 08bf465
Expose configuration load strategy on builders
graemerocher b25b248
Fix constant property source loader initialization
graemerocher 84ed2bb
Handle duplicate configuration resources
graemerocher 09a8e77
Add tests for configuration loading strategy
graemerocher 0434499
Document duplicate configuration resource handling
graemerocher c15759e
Address PR review feedback for duplicate configuration resource handl…
Copilot 35b5f96
fix tests
graemerocher 79e3daf
add test coverage
graemerocher 435f2d6
CR
graemerocher 8304bc4
Fix nullability in configuration load strategy
graemerocher bf5e272
Add tests for duplicate configuration resource strategies
graemerocher 4996ba9
Remove stray semicolons in ConfigurationLoadStrategySpec
graemerocher 8b64d53
Move resource load strategy to core
graemerocher 292af66
Merge branch '5.0.x' into opencode/witty-canyon
graemerocher 856aec4
move more duplicate detection logic to core
graemerocher 6a0c9bf
Merge branch 'opencode/witty-canyon' of github.com:micronaut-projects…
graemerocher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
inject/src/main/java/io/micronaut/context/env/ConfigurationLoadStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| * Copyright 2017-2026 original authors | ||
| * | ||
| * 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 | ||
| * | ||
| * https://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 io.micronaut.context.env; | ||
|
|
||
| import io.micronaut.context.exceptions.ConfigurationException; | ||
| import org.jspecify.annotations.NullMarked; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Configuration resource loading strategy. | ||
| * | ||
| * @param type The strategy type. Defaults to {@link ConfigurationLoadStrategyType#FAIL_ON_DUPLICATE}. | ||
| * @param warnOnDuplicates Whether to warn when duplicates are found. Applies only to {@link ConfigurationLoadStrategyType#FIRST_MATCH}. | ||
| * @param mergeOrder Artifact name regex patterns used to order resources before merging. Applies only to {@link ConfigurationLoadStrategyType#MERGE_ALL}. | ||
| * @since 5.0.0 | ||
| */ | ||
| @NullMarked | ||
| public record ConfigurationLoadStrategy( | ||
|
graemerocher marked this conversation as resolved.
Outdated
|
||
| ConfigurationLoadStrategyType type, | ||
| boolean warnOnDuplicates, | ||
| List<String> mergeOrder | ||
| ) { | ||
| /** | ||
| * Default strategy. | ||
| */ | ||
| public static ConfigurationLoadStrategy defaultStrategy() { | ||
| return builder().build(); | ||
| } | ||
|
|
||
| /** | ||
| * @return A new {@link Builder}. | ||
| */ | ||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public ConfigurationLoadStrategy { | ||
| if (type == null) { | ||
| type = ConfigurationLoadStrategyType.FAIL_ON_DUPLICATE; | ||
| } | ||
| if (mergeOrder == null) { | ||
| mergeOrder = List.of(); | ||
| } else { | ||
| // Always create a defensive copy to prevent external mutation | ||
| mergeOrder = new ArrayList<>(mergeOrder); | ||
| } | ||
|
|
||
| if (!mergeOrder.isEmpty() && type != ConfigurationLoadStrategyType.MERGE_ALL) { | ||
| throw new ConfigurationException("mergeOrder is only supported when configuration loading strategy type is MERGE_ALL"); | ||
| } | ||
|
|
||
| mergeOrder = Collections.unmodifiableList(mergeOrder); | ||
| } | ||
|
graemerocher marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Mutable builder for {@link ConfigurationLoadStrategy}. | ||
| */ | ||
| @NullMarked | ||
| public static final class Builder { | ||
| private ConfigurationLoadStrategyType type = ConfigurationLoadStrategyType.FAIL_ON_DUPLICATE; | ||
| private boolean warnOnDuplicates = true; | ||
| private List<String> mergeOrder = List.of(); | ||
|
|
||
| public Builder type(ConfigurationLoadStrategyType type) { | ||
| this.type = Objects.requireNonNullElse(type, ConfigurationLoadStrategyType.FAIL_ON_DUPLICATE); | ||
| return this; | ||
| } | ||
|
|
||
| public Builder warnOnDuplicates(boolean warnOnDuplicates) { | ||
| this.warnOnDuplicates = warnOnDuplicates; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder mergeOrder(List<String> mergeOrder) { | ||
| this.mergeOrder = Objects.requireNonNullElse(mergeOrder, List.of()); | ||
| return this; | ||
| } | ||
|
|
||
| public Builder mergeOrder(String... mergeOrder) { | ||
| if (mergeOrder == null || mergeOrder.length == 0) { | ||
| this.mergeOrder = List.of(); | ||
| } else { | ||
| this.mergeOrder = List.of(mergeOrder); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| public ConfigurationLoadStrategy build() { | ||
| return new ConfigurationLoadStrategy(type, warnOnDuplicates, mergeOrder); | ||
| } | ||
| } | ||
| } | ||
42 changes: 42 additions & 0 deletions
42
inject/src/main/java/io/micronaut/context/env/ConfigurationLoadStrategyType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Copyright 2017-2026 original authors | ||
| * | ||
| * 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 | ||
| * | ||
| * https://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 io.micronaut.context.env; | ||
|
|
||
| import org.jspecify.annotations.NullMarked; | ||
|
|
||
| /** | ||
| * Defines how Micronaut should behave when the same configuration resource (for example | ||
| * {@code application.yml} or {@code application.properties}) is found more than once on the classpath. | ||
| * | ||
| * @since 5.0.0 | ||
| */ | ||
| @NullMarked | ||
| public enum ConfigurationLoadStrategyType { | ||
| /** | ||
| * The first matching resource is used. Duplicates may be logged as a warning. | ||
| */ | ||
| FIRST_MATCH, | ||
|
|
||
| /** | ||
| * All matching resources are read and merged in the configured order. | ||
| */ | ||
| MERGE_ALL, | ||
|
|
||
| /** | ||
| * Fail fast if duplicate configuration resources are detected. | ||
| */ | ||
| FAIL_ON_DUPLICATE | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.