Skip to content

The design with return Optional is ugly #689

@artemptushkin

Description

@artemptushkin

Hey, I know it's a Java library but I'd like open a thread that this one is quite ugly for Kotlin (and tbh, would be for me if I write Java too) because of so many return Optional in the API responses, e.g.

public abstract class EditImageResponse extends JsonSerializable {
  /** Used to retain the full HTTP response. */
  @JsonProperty("sdkHttpResponse")
  public abstract Optional<HttpResponse> sdkHttpResponse();

  /** Generated images. */
  @JsonProperty("generatedImages")
  public abstract Optional<List<GeneratedImage>> generatedImages();
...

In Kotlin (and Java it would be something similar) I end up writing:

return r.generatedImages().get().first().image().get().let { ... }

I find it quite ugly in 2025 and not trustworthy. Every time writing code like this it makes me think: "what if it's null". But sometimes it's never a null.

It's clearly for the SDK API that you put all of the fields as nullable but I have high doubts that in the API contract itself all are the fields can be nullable, i.e. optional. It's never like this, most probably we can expect some values as nulls or at least empty lists but not null.

Another point that is not a typical API design, other SDKs ~API wrappers do not have optionals usually. There are alternatives (annotations) to hightlight that a field can be optional but it must be confirmed by the actual API design.

The concern here is that this is a new library, another new library and from the begging we have inconinient.


Metadata

Metadata

Assignees

Labels

priority: p3Desirable enhancement or fix. May not be included in next release.status:awaiting user responsetype: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions