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

do not allow arguments with default values to be required (DAT-17436) #5848

Merged
merged 2 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ CommandTests.define {
Short Description: Drop all database objects owned by the user
Long Description: NOT SET
Required Args:
force (Boolean) Argument to allow use of dropAll with values of 'true' or 'false'. The default is 'false'.
requireForce (Boolean) Argument to require user of dropAll to supply a 'force' argument, with values of 'true' or 'false'. The default is 'false'.
url (String) The JDBC database connection URL
OBFUSCATED
Optional Args:
Expand All @@ -27,9 +25,13 @@ Optional Args:
Default: null
driverPropertiesFile (String) The JDBC driver properties file
Default: null
force (Boolean) Argument to allow use of dropAll with values of 'true' or 'false'. The default is 'false'.
Default: false
password (String) Password to use to connect to the database
Default: null
OBFUSCATED
requireForce (Boolean) Argument to require user of dropAll to supply a 'force' argument, with values of 'true' or 'false'. The default is 'false'.
Default: false
schemas (String) Schemas to include in drop
Default: null
username (String) Username to use to connect to the database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ protected void register(String[] commandName, CommandArgumentDefinition<?> defin
if (commandArgumentDefinitions.get(commandNameKey).contains(definition)) {
throw new IllegalArgumentException("Duplicate argument '" + definition.getName() + "' found for command '" + commandNameKey + "'");
}
if (definition.isRequired() && definition.getDefaultValue() != null) {
throw new IllegalArgumentException("Argument '" + definition.getName() + "' for command '" + commandNameKey + "' has both a default value and the isRequired flag set to true. Arguments with default values cannot be marked as required.");
}
this.commandArgumentDefinitions.get(commandNameKey).add(definition);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ public class DropAllCommandStep extends AbstractCommandStep {
SCHEMAS_ARG.setSupersededBy(CATALOG_AND_SCHEMAS_ARG);
REQUIRE_FORCE_ARG = builder.argument("requireForce", Boolean.class)
.description("Argument to require user of dropAll to supply a 'force' argument, with values of 'true' or 'false'. The default is 'false'.")
.required()
.defaultValue(false)
.build();
FORCE_ARG = builder.argument("force", Boolean.class)
.description("Argument to allow use of dropAll with values of 'true' or 'false'. The default is 'false'.")
.required()
.defaultValue(false)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ class CommandArgumentDefinitionTest extends Specification {
arg1.defaultValue == null

when:
def arg2 = builder.argument("arg2", String).required().defaultValue("default value").description("This is arg2").build()
def arg2 = builder.argument("arg2", String).defaultValue("default value").description("This is arg2").build()
then:
arg2.name == "arg2"
arg2.required
!arg2.required
arg2.defaultValue == "default value"

when:
Expand All @@ -90,7 +90,13 @@ class CommandArgumentDefinitionTest extends Specification {
def e = thrown(IllegalArgumentException)
e.message == "Invalid argument format: kabob-case"

when:
builder.argument("arg3", String).defaultValue("def").required().build()
then:
def e2 = thrown(IllegalArgumentException)
e2.message == "Argument 'arg3' for command 'mock' has both a default value and the isRequired flag set to true. Arguments with default values cannot be marked as required."

then:
StringUtil.join(Scope.currentScope.getSingleton(CommandFactory).getCommandDefinition("mock").getArguments(), ", ") == "arg1=arg1, arg2=arg2 (required)"
StringUtil.join(Scope.currentScope.getSingleton(CommandFactory).getCommandDefinition("mock").getArguments(), ", ") == "arg1=arg1, arg2=arg2"
}
}