question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conditional documents in profile specific file are not loaded consistently

See original GitHub issue

I’ve following problem.

I’ve an instance with following active profiles: dev, test, foo

and apllication-test.yaml file

---
my.prop1: default
my.prop2: default
---
spring.profiles: dev
my.
    prop1: dev
    prop3: abc
---
spring.profiles: foo
my.prop2: foo

My intuition would tell me: my.prop1 = dev my.prop2 = foo

Thought the result is really: my.prop1 = default my.prop2 = foo

It seems to use the order of profiles, rather then the order of the document. This seems to be different then what the documentation is saying. (Correct me if I’m wrong)

Further more my.prop3 seems to be not set at all as if this section is ignored even if dev is set active.

Is this a bug of Spring or is there a trick to it?

lG Lukas

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
shakuzencommented, Mar 14, 2018

I also ran into this problem and it took quite some time to track it down and figure out why it was happening. My use case is that we have configuration properties specific to logical environments (say dev, stg, prod) and that we want to be active only when the application is running on our platform (Cloud Foundry as cloud profile; or our other platform that uses the server profile) as opposed to a developer’s machine. The assumption being a developer might activate a logical environment profile from her or his machine but would not activate cloud or server.

Therefore, to achieve this, it seemed reasonable to me to put say the DEV environment spring.boot.admin.client.uri in a file application-dev.yml like:

---
spring:
  profiles: cloud,server

  boot.admin.client.uri: http://my-admin-url

The above works fine, actually, if the active profiles are in the “right” order dev,cloud, but it does not work if the order is cloud,dev. This can also be demonstrated by the following change to an existing Spring Boot test (switch the order of the active profiles):

===================================================================
--- spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java	(revision 3ae4a541b630fb7bccf45ebd0922c19d099c2276)
+++ spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java	(date 1521006994000)
@@ -777,7 +777,7 @@
 		SpringApplication application = new SpringApplication(Config.class);
 		application.setWebApplicationType(WebApplicationType.NONE);
 		this.context = application.run(
-				"--spring.profiles.active=activeprofilewithdifferentsubdoc,activeprofilewithdifferentsubdoc2");
+				"--spring.profiles.active=activeprofilewithdifferentsubdoc2,activeprofilewithdifferentsubdoc");
 		String property = this.context.getEnvironment().getProperty("foobar");
 		assertThat(property).isEqualTo("baz");
 	}

https://github.com/spring-projects/spring-boot/blob/3ae4a541b630fb7bccf45ebd0922c19d099c2276/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java#L774-L783

I’ve also pushed a repro project with a failing test case: https://github.com/shakuzen/demo-compound-profile

To summarize the use case, being able to use sub-documents in YAML files that are themselves profiled allows for AND semantics. In the example shown above, the dev profile AND the cloud (OR server) profile should be active for the property source to be included. #12469 was raised recently also regarding AND profile matching semantics. What I find nice about what I expected to work (and does if the active profile order is “right”) is that it allows one AND and an arbitrary number of ORs. Perhaps the outcome of SPR-12458 will pave a way for something more robust.

For my specific use case mentioned above, we can work around this thanks to Spring Boot Admin client offering an enabled property that we can set to false by default and only make it true when the cloud OR server profile is active. Perhaps there are other use cases that aren’t as easily worked around. At the least, the behavior was hard to understand and surprising.

0reactions
mbhavecommented, Jul 31, 2020

This should be solved by config data rewrite in 2.4.x. For multi-document files, later documents can override the properties defined in earlier ones. With the following configuration in application-test.yml and active profiles of dev, foo, test,

---
my.prop1: default
my.prop2: default
---
spring.profiles: dev
my.
    prop1: dev
    prop3: abc
---
spring.profiles: foo
my.prop2: foo

the result is

my.prop1 = dev my.prop2 = foo my.prop3 = abc

Read more comments on GitHub >

github_iconTop Results From Across the Web

Config file processing in Spring Boot 2.4
Documents will be loaded in the order that they're defined. Profiles can no longer be activated from profile specific documents. Document Order.
Read more >
Spring Profile conditional prop files multiple environment
I have a question, with Spring Profiles. I understand the reason for not using maven profiles because each environment would require another ...
Read more >
Known issues - Azure Information Protection | Microsoft Learn
Protected documents that are uploaded to SharePoint or OneDrive lose their ContentID value, and access can't be tracked or revoked. If a user...
Read more >
Spring Profiles - Baeldung
Tutorial for how to work with properties files and property values in ... annotation — we are mapping the bean to that particular...
Read more >
Managing Import Profiles - Ex Libris Knowledge Center
For the Delimited Text File and Excel options, the content of these files can be converted from one metadata standard to another (such...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found