Skip to content

Allow map binding to work with unescaped characters #13404

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

Open
yygnay opened this issue Jun 7, 2018 · 16 comments
Open

Allow map binding to work with unescaped characters #13404

yygnay opened this issue Jun 7, 2018 · 16 comments
Labels
theme: config-data Issues related to the configuration theme type: enhancement A general enhancement
Milestone

Comments

@yygnay
Copy link

yygnay commented Jun 7, 2018

Spring boot version: 2.0.2 (1.5.13 is normal)

Problem Description:

If the map key is with “/”, it cannot be parsed normally. Others are normal.

Example:

application.yml 

shiro:
  testMap:
    /test1: test1
    /test2: test2

Configuration class

@Data
@ConfigurationProperties(prefix="shiro")
public class ShiroProperties {
	private Map<String,String> testMap;
}

Test class

@Transactional
@RunWith(SpringRunner.class)
@SpringBootTest(classes=DataManage.class)
public class ShiroTest extends BaseTransactionalTest{

	@Autowired
	private ShiroProperties shiroConfig;
	
	@Test
	public void read() {
		System.out.println(shiroConfig.toString());
	}
	
}

console info:

ShiroProperties(testMap={test1=test1, test2=test2})
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 7, 2018
@philwebb
Copy link
Member

philwebb commented Jun 7, 2018

The new binder is much stricter about property names which means you need to surround them in square brackets. Try the following:

shiro:
  testMap:
    "[/test1]": test1
    "[/test2]": test2

@philwebb philwebb added the for: team-attention An issue we'd like other members of the team to review label Jun 7, 2018
@philwebb
Copy link
Member

philwebb commented Jun 7, 2018

I'm wonder if there's any way we can make this more user friendly. I've flagged the issue for discussion at our next call.

@yygnay
Copy link
Author

yygnay commented Jun 7, 2018

Thank you! According to your suggestion it can be used normally, I hope there are relevant examples in the official document...

@yygnay yygnay closed this as completed Jun 7, 2018
@wilkinsona wilkinsona reopened this Jun 7, 2018
@mbhave
Copy link
Contributor

mbhave commented Jun 7, 2018

We might be able to get the original key in the EntryBinder.

@philwebb philwebb added type: enhancement A general enhancement and removed for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged labels Jun 13, 2018
@philwebb philwebb added this to the Backlog milestone Jun 13, 2018
@edysli
Copy link

edysli commented Jun 19, 2018

This also tripped me up while migrating to 2.0 because I have URLs as keys, something like:

myapp
  serviceUrls:
    'https://example.org/test': 'https://test.example.org/Endpoint'

and it used to work fine with 1.5. After finding this issue, I changed this map to:

'[https://example.org/test]': 'https://test.example.org/Endpoint'

and now it parses correctly. Please document this explicitly in the migration guide or reference doc.

@snicoll
Copy link
Member

snicoll commented Jun 19, 2018

@edysli there is already an issue for that #13506, with a reference right above your comment.

@edysli
Copy link

edysli commented Jun 19, 2018

@snicoll Yeah I saw it, I just didn't know which one was best to host my "me too" example so I picked the lowest id... 😉 I can move my comment over to #13506, if that's less messy.

@Chatom
Copy link

Chatom commented Dec 17, 2018

Hi, it indeed works well with the use of brackets, but it is quite unnatural to do so.
I'd understand the use of quotes (even if they're not necessary in yaml syntax) but brackets seems a bit too much.
Another problem is that the mapping to the cleaned string without any warning can easily lead to errors if the config is not checked properly...

@wilkinsona
Copy link
Member

@Chatom We're aware that there's some room for improvement here. That's why this issue remains open.

@philwebb philwebb added the theme: config-data Issues related to the configuration theme label Jan 14, 2019
@philwebb philwebb modified the milestones: 2.x, 2.2.x Jan 14, 2019
@maraswrona
Copy link

Any idea when this could be improved? Is there some better way around this (other than using brackets) ?

@wilkinsona
Copy link
Member

@maraswrona The issue is in the 2.x milestone which means it'll be considered for inclusion in 2.3.

@kdowbecki
Copy link

kdowbecki commented Nov 28, 2019

I'd really prefer the binder to fail fast with an exception when it discovers unescaped property names. Currently 2.2.1 silently modifies the property name by removing special characters which is a nightmare to debug.

@philwebb philwebb changed the title Problem with yml configuration file parsing map Allow map binding to work with unescaped characters Oct 23, 2020
@pablocavalieri
Copy link

The new binder is much stricter about property names which means you need to surround them in square brackets. Try the following:

shiro:
  testMap:
    "[/test1]": test1
    "[/test2]": test2

Is there a way to scape the square brackets? I need my Map key to be some value[value between square brackets]

@philwebb
Copy link
Member

@pablocavalieri I believe you can do this:

testMap:
  "[some value[value between square brackets]]": "test"

@MinhDrake

This comment was marked as outdated.

@philwebb

This comment was marked as outdated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: config-data Issues related to the configuration theme type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.