Skip to content

Allow recursive profile group references#24327

Closed
dreis2211 wants to merge 3 commits into
spring-projects:masterfrom
dreis2211:gh-24319
Closed

Allow recursive profile group references#24327
dreis2211 wants to merge 3 commits into
spring-projects:masterfrom
dreis2211:gh-24319

Conversation

@dreis2211

Copy link
Copy Markdown
Contributor

Hi,

this PR fixes #24319 by removing the current profile from group profiles in order to avoid an endless loop when expanding/flattening the profile list.

Cheers,
Christoph

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 3, 2020
List<String> reversed = new ArrayList<>(list);
Collections.reverse(reversed);
return Collections.unmodifiableList(reversed);
return reversed;

@dreis2211 dreis2211 Dec 3, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, we could do a stream/filter approach, but the unmodifiable list seemed not needed as the result is only used above and cannot be modified from the outside.

@mbhave

mbhave commented Dec 3, 2020

Copy link
Copy Markdown
Contributor

I'm not sure if just removing the current profile is enough. I think we'd end up with the same issue if we had a configuration like:

spring.profiles.group.a=e,f
spring.profiles.group.e=a,x,y

Maybe we should throw an exception in this case since it is an invalid configuration.

@dreis2211

Copy link
Copy Markdown
Contributor Author

@mbhave Good catch - I covered the additional case now.

Which sort of exception do you want to be thrown here? IllegalStateException or a new one? Just let me know if I should add the exception and if so which type. The "maybe" suggests that you're not sure yet.

@philwebb

philwebb commented Dec 4, 2020

Copy link
Copy Markdown
Member

I think an IllegalStateException would be fine. We can always change it later if we need to.

@dreis2211

dreis2211 commented Dec 4, 2020

Copy link
Copy Markdown
Contributor Author

@philwebb & @mbhave I added the IllegalStateException. Let me know if this is what you had in mind.

@dreis2211 dreis2211 changed the title Ignore recursive reference in profile groups Fail on recursive references in profile groups Dec 7, 2020
philwebb pushed a commit that referenced this pull request Dec 9, 2020
Update `Profiles` group expansion logic to fail if recursive
references are found.

See gh-24327
@philwebb philwebb closed this in a2a7bd5 Dec 9, 2020
@philwebb philwebb modified the milestones: 2.4.x, 2.4.1 Dec 9, 2020
@philwebb

philwebb commented Dec 9, 2020

Copy link
Copy Markdown
Member

Thanks once again @dreis2211! I've merged this into master with a minor update to make the message catch duplicates in the expanded set as well as the stack.

@philwebb philwebb reopened this Dec 9, 2020
@philwebb

philwebb commented Dec 9, 2020

Copy link
Copy Markdown
Member

Reopening because I just realized that a user might want to have the same expansion in different groups:

	@Test
	void test() {
		MockEnvironment environment = new MockEnvironment();
		environment.setProperty("spring.profiles.active", "a,b,c");
		environment.setProperty("spring.profiles.group.a", "e,f");
		environment.setProperty("spring.profiles.group.e", "f,g");
		Binder binder = Binder.get(environment);
		assertThatIllegalStateException().isThrownBy(() -> new Profiles(environment, binder, null))
				.withMessageContaining("Profiles could not be resolved. Remove profile 'a' from group: 'e'");
	}

@philwebb philwebb changed the title Fail on recursive references in profile groups Allow recusive profile group references Dec 9, 2020
@philwebb philwebb changed the title Allow recusive profile group references Allow recursive profile group references Dec 9, 2020
@philwebb philwebb requested a review from mbhave December 9, 2020 03:00
philwebb added a commit that referenced this pull request Dec 9, 2020
Update the original fix for issue #24327 so that recursive elements
are tolerated rather than fail.

See gh-24327
@philwebb

philwebb commented Dec 9, 2020

Copy link
Copy Markdown
Member

After some more thought, I think it's best if we tolerate duplicates rather than throwing an exception. I've changed things again in commit bef5fe2.

@mbhave @dreis2211 I'd appreciate a review if you have time. (I'll leave the issue open for now)

@dreis2211

Copy link
Copy Markdown
Contributor Author

Looks good to me, @philwebb

@philwebb philwebb closed this Dec 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug A general bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Spring Boot profile groups with recursive references end up in endless loop

5 participants