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

Add support for untagged union types #2143

Closed
Xtansia opened this issue Feb 15, 2024 · 5 comments
Closed

Add support for untagged union types #2143

Xtansia opened this issue Feb 15, 2024 · 5 comments

Comments

@Xtansia
Copy link
Contributor

Xtansia commented Feb 15, 2024

While working on the Smithy specifications for OpenSearch we've come across a need to represent a field that may have more than one type. In this instance the field is int | boolean, which could be represented as a plain Document type in Smithy, but this would be too broad of a typing.

Our primary target from Smithy is OpenAPI where this could map onto a oneOf:

{
  "oneOf": [
    {"type": "integer"},
    {"type": "boolean"}
  ]
}

I understand that probably the largest challenge for adding this to the Smithy language is determining how to and implementing support for this in the various language generators. As we are only targeting OpenAPI an intermediary step of a trait that only affects OpenAPI translation may be a viable solution for now.

@JordonPhillips
Copy link
Contributor

I don't think this is something we can ever reasonably support. You've already called out the difficulty in representing this in a variety of programming languages, and that is a critical issue that can't be ignored.

The way you would achieve the same effect in Smithy is to use a Document, as you mentioned, or to use a custom trait/protocol that knows to flatten unions in serialization.

@JordonPhillips JordonPhillips added the closing-soon This issue will automatically close in 7 days unless further comments are made. label Feb 20, 2024
@mtdowling
Copy link
Member

You might also be interested in the Alloy library from disneystreaming supports untagged unions and integrates it with OpenAPI conversions: https://github.com/disneystreaming/alloy/blob/main/modules/core/resources/META-INF/smithy/unions.smithy#L54

@Xtansia
Copy link
Contributor Author

Xtansia commented Feb 20, 2024

I don't think this is something we can ever reasonably support. You've already called out the difficulty in representing this in a variety of programming languages, and that is a critical issue that can't be ignored.

The way you would achieve the same effect in Smithy is to use a Document, as you mentioned, or to use a custom trait/protocol that knows to flatten unions in serialization.

Is there a world where this is feasible if it's limited to only simply differentiable types? e.g. int | string, int | [int], [int] | map<string, int>, but structA | structB is disallowed.
Obviously still inconvenient to implement in some languages, but I feel would remove a lot of ambiguity in deserialization.

@github-actions github-actions bot removed the closing-soon This issue will automatically close in 7 days unless further comments are made. label Feb 21, 2024
@JordonPhillips
Copy link
Contributor

You can't necessarily know what's simply differentiable or not, it's highly dependant on the protocol. You could imagine an XML protocol that does things like <float value="1.3"/> / <int value="1> and so on to remove that ambiguity, or similar for json. Ultimately whatever happens at the serialization layer is invisible to sdk and service tooling users, so while deserialization does matter it doesn't matter as much as long as it's deterministic. That's why Alloy's strategy of having an @untagged trait works - tooling users get the well-supported union types and the serialization layer is free to do what is must. They can attach validation to that trait to make sure their deserialization is deterministic, and if they want to convert to openapi they could write a plugin that handles the translation.

@kstich
Copy link
Contributor

kstich commented Apr 22, 2024

Closing as this is something we do not plan to support in Smithy.

@kstich kstich closed this as completed Apr 22, 2024
@kstich kstich closed this as not planned Won't fix, can't repro, duplicate, stale Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants