Closed jmpicnic closed 2 years ago
trait
s. That's a design decision and I think the right one. Here, the traits form a nice tree structure, but in general this can be an arbitrary graph. You can have other traits which are added to some, otherwise non-related implementations. How could Magnolia (or tapir) know which traits are the "main" ones? (btw. literals are not in the schema as your case classes don't extend Literal
:) )To "fix" 2&3, I think the right answer is to write down the problematic parts of the schema by hand. Automatic derivation has its limits :)
In your case, I think you would need to define:
implicit opSchema: Schema[Op] = implicitly[Schema[String]]
implicit opValidator: Validator[Op] = Validator.enum[Op]
// similar for compose
implicit val termSchema: Schema[Term] = Schema(SCoproduct(...))
I believe I have a similar issue, although I'm not quite sure if this is completely the same thing. My data structure:
case class AnimalHolder(value: Animal)
sealed trait Animal
case class Boar(weight: Int) extends Animal
case class Cat(name: String, weight: Int) extends Animal
I'm using circe with semi-automatic codec derivation My endpoint definition:
def post: Endpoint[AnimalHolder, Unit, Unit, Nothing] = endpoint
.post
.in(jsonBody[AnimalHolder])
.out(statusCode(Created))
Generating an OpenAPI YAML leads to the following:
Animal:
oneOf:
- $ref: '#/components/schemas/Boar'
- $ref: '#/components/schemas/Cat'
Boar:
allOf:
- $ref: '#/components/schemas/Animal'
- required:
...
Cat:
allOf:
- $ref: '#/components/schemas/Animal'
- required:
...
Both, SwaggerUI and ReDoc, have trouble visualizing this schema and I wonder if it shouldn't look more like
Cat:
required:
...
I'd appreciate any hints on how I can either change my model/circe derivation or the OpenAPI schema derivation to remove this recursion.
@gbastkowski your definition isn't recursive, but the way of generating the schema that you mention was changed in https://github.com/softwaremill/tapir/issues/308. What kind of problems does this cause with SwaggerUI?
@gbastkowski ah I ran this locally and I can see this problem; let's move to #308 and discuss further there
This is the SwaggerUI rendering:
Boar {
description: the value
weight* integer
oneOf -> {
}
Cat {
name* string Cats are pets and therefore should have a name
weight* integer
}
}
Cat {
description: the value
name* string Cats are pets and therefore should have a name
weight* integer
oneOf -> Boar {
weight* integer
}
{
}
}
Closing as this is partly fixed, partly expected
First, thanks for putting together a strong foundation for defining API's from the Scala Type system. It has been a great help already.
As I have used the package more, I was trying to build a schema to describe flexible filters in the body and I am running into some oddities in the Yaml OAS generation. I am currently using 0.11.9, but from the release notes, I don't think it will be different in 0.12.3 (planning to upgrade soon)
I have boiled down the issues I am seeing to this sample test:
The generated Yaml is:
The things that are stumping me are:
Clause
schema only listsExpression
as a one of its options, I would expect to seeExpression
,Composite
,Not
Term
should beoneOf
with optionsReference
andLiteral
instead of repeating theReference
Options and skippingLiteral
altogether.case object
types to be translated as astring
schemas with anenum
constraint instead of unqualifiedobjects
I have tried to provide some hints using the custom types descriptions in the docs but with not much success. The results vary a bit, but I am not able to get them to line up, in particular #1, & #2
Could you give me some guidance on how to give enough information to be able to get the schema (and hopefully then validators, encoders and decoders) to generate the code I am expecting? Or may be I am missing something in the way I am approaching the problem.
Thanks a lot for your help.
Miguel
PS. Please feel free to use the example in any way it may be useful for unit test, docs, or anything else.