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.

nullable & optional/required marks

See original GitHub issue

Kotlin solves NullPointerException with nullable/non-nullable classes.

But sometimes we need to show that a field is required or not. Yes sometimes we can express this by using null, but not always. An use case will be partial update, when we want to update just some columns in database. So, if a column is nullable in database, we can not send null to that field, because null is valid value for it.

A solution will be to use a class Maybe or Option, something like java’s Optional. The issue with Optional is that it accepts null even if we have Optional<String>, it should accept null only if we have Optional<String?>.

  1. Option<String> - optional & non-nullable
  2. Option<String?> - optional & nullable
  3. String - required & non-nullable
  4. String? - required & nullable

This mean, or we send a data for the field, or just do not send the field in request, null is not allowed. {"foo": "bar", "baz": true} is allowed {"baz": true} is allowed, because is optional {"foo": null, "baz": true} is NOT allowed, because is non-nullable

Basically null has the meaning of “this property existed before, and I want to nullify its value”. If there’s no value, or the value needs not change (for requests, for example), it just shouldn’t be sent.

An example of partial update you can find here https://medium.com/workflowgen/graphql-mutations-partial-updates-implementation-bff586bda989 This is also supported by OpenAPI https://swagger.io/docs/specification/data-models/data-types/ you can find that a filed can be marked as optional & non-nullable

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
raderiocommented, Mar 20, 2019

Ok, but in my case I do not have a default value, and I can not put a random string as a default, because in that case I will not be able to make difference, is it my default or this value was in the request.

So, my solution is class Req(val foo: Option<String>), or value is provided or is missing, null is not allowed.

0reactions
raderiocommented, Mar 21, 2019

Yes, there is not Option class in Kotlin stdlib, but maybe it is possible to add it into this library?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Question: nullable & optional/required marks · Issue #1775
Can we represent in specification that a field is non-nullable & optional? this mean, or we send a data for the field, or...
Read more >
Understanding the usage of optional and nullable in TypeScript
As the official document says, Optional is a constructor parameter decorator that marks a dependency as optional. This clearly states that ...
Read more >
Uses for Optional - java - Stack Overflow
The main design goal of Optional is to provide a means for a function returning a value to indicate the absence of a...
Read more >
Defining Optional Properties and Nullable Values in RAML
Defining optional properties and nullable values in RAML is vital for a smooth integration experience. Here's how to do it.
Read more >
Using nullability in GraphQL
The fields whose types have an exclamation mark, ! , next to them are ... in an optional argument, then making that argument...
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