Skip to content

Make nullable parameters always optional #1528

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

Merged

Conversation

xenomachina
Copy link
Contributor

📝 Description

Treat nullable parameters that have no default value as if their default was set to null.

Nullable parameters that have a default value explicitly set will continue to use that as their default.

🔗 Related Issues

#1527

Treat nullable parameters that have no default value as if their default
was set to null.

Nullable parameters that have a default value explicitly set will
continue to use that as their default.
parameter.type.isOptionalInputType() ||

// for nullable parameters that have no explicit default, we pass in null if not in input
(parameter.type.isMarkedNullable && !parameter.isOptional)
Copy link
Contributor

Choose a reason for hiding this comment

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

probably worth to wrap this expression in an extension function in kParameterExtensions,
KParameter.isNotOptionalNullable()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -109,7 +109,11 @@ private fun <T : Any> mapToKotlinObject(input: Map<String, *>, targetClass: KCla
// filter parameters that are actually in the input in order to rely on parameters default values
// in target constructor
val constructorParametersInInput = constructorParameters.filter { parameter ->
Copy link
Contributor

Choose a reason for hiding this comment

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

we will probably need to rename this
we can just call it constructorParameters, and do

val constructorParameters = targetConstructor.parameters.filter { ... }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@samuelAndalon
Copy link
Contributor

Thanks for the PR, i left couple of minor comments

- add KParameter.isNotOptionalNullable()
- minor cleanups in mapToKotlinObject
@samuelAndalon samuelAndalon merged commit 095b056 into ExpediaGroup:master Aug 24, 2022
@xenomachina xenomachina deleted the nullables-default-to-null branch August 24, 2022 22:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants