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 const attribute to Schema #3764

Closed
kamilkloch opened this issue May 13, 2024 · 10 comments
Closed

Add const attribute to Schema #3764

kamilkloch opened this issue May 13, 2024 · 10 comments

Comments

@kamilkloch
Copy link
Contributor

Both OpenAPI 3.1 and AsyncApi 3.0.0 support const:

More importantly, for AsynApi 3.0.0, const looks like the only working mechanism to encode union types with a discriminator - see https://www.asyncapi.com/docs/reference/specification/v3.0.0#models-with-polymorphism-support. (Current AsyncApi schema for union types rendered by tapir is invalid: #3754)

@adamw
Copy link
Member

adamw commented May 14, 2024

There is const in the jsonschema that we use: https://github.com/softwaremill/sttp-apispec/blob/master/apispec-model/src/main/scala/sttp/apispec/Schema.scala#L55

I don't think we should be assing const to sttp.tapir.Schema. That's the high-level representaiton. Possibly, the translation between a tapir schema and an async api schema needs to be adjusted?

@adamw
Copy link
Member

adamw commented May 14, 2024

That's probably the place where the discriminators are generated:

private def tDiscriminatorToADiscriminator(discriminator: TSchemaType.SDiscriminator): Discriminator = {

@adamw
Copy link
Member

adamw commented May 14, 2024

I wonder if the way discriminators are defined for OpenAPI and AsyncAPI diverges? But hopefully we can generate something that works for both cases

@kamilkloch
Copy link
Contributor Author

There is const in the jsonschema that we use: https://github.com/softwaremill/sttp-apispec/blob/master/apispec-model/src/main/scala/sttp/apispec/Schema.scala#L55

True, I think it is not being set anywhere in the schema generation code, though.

I don't think we should be assing const to sttp.tapir.Schema. That's the high-level representaiton. Possibly, the translation between a tapir schema and an async api schema needs to be adjusted?

If I am not mistaken, currently OpenAPI and AsyncApi schemas are rendered in the same way, this leads to an illegal async api spec when using .withDiscriminator: #3754. So yes, it looks like a translation between a tapir schema and an async api schema needs to be adjusted.

@kamilkloch
Copy link
Contributor Author

I wonder if the way discriminators are defined for OpenAPI and AsyncAPI diverges? But hopefully we can generate something that works for both cases

AsyncApi does not support mapping for the discriminator, also discriminator must be a String. It looks like separate rendering paths might be needed - OpenApi conforms to JSON Schema Specification Draft 2020-12, AsyncApi does not.

@adamw
Copy link
Member

adamw commented May 14, 2024

Ah I see. Then I guess we'll need a flag for the TSchemaToASchema class to make this configurable

@kamilkloch
Copy link
Contributor Author

Ah I see. Then I guess we'll need a flag for the TSchemaToASchema class to make this configurable

What would be your suggestion for the flag? Discriminator-specific (we might stumble on other asyncapi/openapi incompatibilities in the future), or perhaps enum OpenApi/AsyncApi/JsonSchema? It seems that ASchema is only used for rendering to one of those specs.

@adamw
Copy link
Member

adamw commented May 14, 2024

Let's start with a simple Boolean flag if we want to generate mappings or consts for discriminators. If there are more such cases, we'll refactor. ASchema is an alias for the sttp-apispec Schema, I think it's used by both?

@kamilkloch
Copy link
Contributor Author

OK, let's roll with a Boolean flag. Yes, ASchema is used by both.

@kamilkloch
Copy link
Contributor Author

Closining for now, see #3765.

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

2 participants