Skip to content
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

Support command line arguments with an empty value #24464

Closed
robwruck opened this issue Jan 31, 2020 · 4 comments
Closed

Support command line arguments with an empty value #24464

robwruck opened this issue Jan 31, 2020 · 4 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: feedback-provided Feedback has been provided type: enhancement A general enhancement
Milestone

Comments

@robwruck
Copy link

robwruck commented Jan 31, 2020

With Spring Boot, you can override application properties using command line arguments like so:

java -jar my-boot.jar --my.property.name=value

This, however, fails when you try to pass an empty value:

java -jar my-boot.jar --my.property.name=
java.lang.IllegalArgumentException: Invalid argument syntax: --my.property.name=
  at org.springframework.core.env.SimpleCommandLineArgsParser.parse(SimpleCommandLineArgsParser.java:75)
  at org.springframework.core.env.SimpleCommandLinePropertySource.<init>(SimpleCommandLinePropertySource.java:90)

Since an empty string is a legal value for an application property, there seems to be no good reason to reject command line arguments with an empty value.

A real-world example is Kafka's ssl.endpoint.identification.algorithm which has to be set to an empty string to disable host name verification.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 31, 2020
@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement labels Jan 31, 2020
@sbrannen
Copy link
Member

What happens if you leave out the = sign as follows?

java -jar my-boot.jar --my.property.name

Does my.property.name then have the value of an empty string for you?

@sbrannen sbrannen added the status: waiting-for-feedback We need additional information before we can continue label Jan 31, 2020
@sbrannen
Copy link
Member

sbrannen commented Jan 31, 2020

FWIW, comparing the out-of-the-box implementations of CommandLinePropertySource, the following tests reveal inconsistent support for empty optional argument values.

The test for JOptCommandLinePropertySource passes.

	@Test
	void withOptionalArg_andArgIsEmpty() {
		OptionParser parser = new OptionParser();
		parser.accepts("foo").withOptionalArg();
		OptionSet options = parser.parse("--foo=");

		PropertySource<?> ps = new JOptCommandLinePropertySource(options);
		assertThat(ps.containsProperty("foo")).isTrue();
		assertThat(ps.getProperty("foo")).isEqualTo("");
	}

Whereas, the test for SimpleCommandLinePropertySource fails with an exception analogous to the one mentioned in this issue's description.

	@Test
	void withOptionalArg_andArgIsEmpty() {
		EnumerablePropertySource<?> ps = new SimpleCommandLinePropertySource("--foo=");

		assertThat(ps.containsProperty("foo")).isTrue();
		assertThat(ps.getProperty("foo")).isEqualTo("");
	}

Thus, we could consider removing the non-empty check in org.springframework.core.env.SimpleCommandLineArgsParser.parse(String...) to make the support consistent.

@sbrannen sbrannen added status: waiting-for-triage An issue we've not yet triaged or decided on and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 31, 2020
@robwruck
Copy link
Author

robwruck commented Jan 31, 2020

Yes, java -jar my-boot.jar --my.property.name correctly sets the property to an empty value.

But the way to pass an argument value should not depend upon whether it is empty or not.

Actually, I created this issue because Spring Cloud Stream hits it when trying to pass empty properties while creating a binder application:
spring-cloud/spring-cloud-stream#1887

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jan 31, 2020
@sbrannen
Copy link
Member

Yes, java -jar my-boot.jar --my.property.name correctly sets the property to an empty value.

Glad to hear that works at least as a workaround. Thanks for the feedback.

But the way to pass an argument value should not depend upon whether it is empty or not.

I agree, and we will address that for SimpleCommandLinePropertySource in Spring Framework 5.2.x.

For previous versions of Spring Framework, please use the aforementioned workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: feedback-provided Feedback has been provided type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants