-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[gen] fix for codegen of sumtypes with reusable fields (#2850)
* [gen] sum types (enums) are now generated with cases in companion that actually extend the sealed trait. Sum types abstract members generation by reusable components in the schema, as an opt-in, configurable feature. Only generating sealed trait body if all subtypes include same reusable component, with it's fields as the abstract trait's members. Redundant duplicate classes for each case is now omitted. Validate fields in case classes and traits does not contain duplicates that cannot be reconciled. Existing tests has been amended to reflect the fix. New tests were added. Some utilities were added. * Update zio-http-gen/src/main/scala/zio/http/gen/openapi/Config.scala * Update zio-http-gen/src/main/scala/zio/http/gen/openapi/Config.scala * fmt --------- Co-authored-by: Nabil Abdel-Hafeez <7283535+987Nabil@users.noreply.github.com>
- Loading branch information
Showing
19 changed files
with
945 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
zio-http-gen/src/main/scala/zio/http/gen/openapi/Config.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package zio.http.gen.openapi | ||
|
||
final case class Config(commonFieldsOnSuperType: Boolean) | ||
object Config { | ||
|
||
val default: Config = Config( | ||
commonFieldsOnSuperType = false, | ||
) | ||
|
||
lazy val config: zio.Config[Config] = | ||
zio.Config | ||
.boolean("common-fields-on-super-type") | ||
.withDefault(Config.default.commonFieldsOnSuperType) | ||
.map(Config.apply) | ||
} |
240 changes: 200 additions & 40 deletions
240
zio-http-gen/src/main/scala/zio/http/gen/openapi/EndpointGen.scala
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package test.component | ||
|
||
import zio.schema._ | ||
import zio.schema.annotation._ | ||
|
||
@noDiscriminator | ||
sealed trait Animal | ||
object Animal { | ||
|
||
implicit val codec: Schema[Animal] = DeriveSchema.gen[Animal] | ||
case class Alligator( | ||
age: Int, | ||
weight: Float, | ||
num_teeth: Int, | ||
) extends Animal | ||
object Alligator { | ||
|
||
implicit val codec: Schema[Alligator] = DeriveSchema.gen[Alligator] | ||
|
||
} | ||
case class Zebra( | ||
age: Int, | ||
weight: Float, | ||
num_stripes: Int, | ||
) extends Animal | ||
object Zebra { | ||
|
||
implicit val codec: Schema[Zebra] = DeriveSchema.gen[Zebra] | ||
|
||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
zio-http-gen/src/test/resources/ComponentAnimalWithAbstractMembers.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package test.component | ||
|
||
import zio.schema._ | ||
import zio.schema.annotation._ | ||
|
||
@noDiscriminator | ||
sealed trait Animal { | ||
def age: Int | ||
def weight: Float | ||
} | ||
object Animal { | ||
|
||
implicit val codec: Schema[Animal] = DeriveSchema.gen[Animal] | ||
case class Alligator( | ||
age: Int, | ||
weight: Float, | ||
num_teeth: Int, | ||
) extends Animal | ||
object Alligator { | ||
|
||
implicit val codec: Schema[Alligator] = DeriveSchema.gen[Alligator] | ||
|
||
} | ||
case class Zebra( | ||
age: Int, | ||
weight: Float, | ||
num_stripes: Int, | ||
) extends Animal | ||
object Zebra { | ||
|
||
implicit val codec: Schema[Zebra] = DeriveSchema.gen[Zebra] | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package test.component | ||
|
||
import zio.schema._ | ||
|
||
case class HttpError( | ||
messages: Option[String], | ||
) | ||
object HttpError { | ||
|
||
implicit val codec: Schema[HttpError] = DeriveSchema.gen[HttpError] | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package test.api.v1.zoo | ||
|
||
import test.component._ | ||
import zio.Chunk | ||
|
||
object Animal { | ||
import zio.http._ | ||
import zio.http.endpoint._ | ||
import zio.http.codec._ | ||
val get_animal = Endpoint(Method.GET / "api" / "v1" / "zoo" / string("animal")) | ||
.in[Unit] | ||
.out[Chunk[Animal]](status = Status.Ok) | ||
.outError[HttpError](status = Status.InternalServerError) | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
zio-http-gen/src/test/resources/EndpointForZooNoError.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package test.api.v1.zoo | ||
|
||
import test.component._ | ||
import zio.Chunk | ||
|
||
object Animal { | ||
import zio.http._ | ||
import zio.http.endpoint._ | ||
import zio.http.codec._ | ||
val get_animal = Endpoint(Method.GET / "api" / "v1" / "zoo" / string("animal")) | ||
.in[Unit] | ||
.out[Chunk[Animal]](status = Status.Ok) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...src/test/resources/inline_schema_sumtype_with_multiple_contradicting_reusable_fields.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
info: | ||
title: Animals Service | ||
version: 0.0.1 | ||
tags: | ||
- name: Animals_API | ||
paths: | ||
/api/v1/zoo/{animal}: | ||
get: | ||
operationId: get_animal | ||
parameters: | ||
- in: path | ||
name: animal | ||
schema: | ||
type: string | ||
required: true | ||
tags: | ||
- Animals_API | ||
description: Get animals by species name | ||
responses: | ||
"200": | ||
content: | ||
application/json: | ||
schema: | ||
type: array | ||
items: | ||
$ref: '#/components/schemas/Animal' | ||
description: OK | ||
openapi: 3.0.3 | ||
components: | ||
schemas: | ||
Animal: | ||
oneOf: | ||
- $ref: '#/components/schemas/Alligator' | ||
- $ref: '#/components/schemas/Zebra' | ||
HasAgeAndWeight: | ||
type: object | ||
required: | ||
- age | ||
properties: | ||
age: | ||
type: integer | ||
format: int32 | ||
minimum: 0 | ||
weight: | ||
type: number | ||
format: float | ||
minimum: 0 | ||
HasWeight: | ||
type: object | ||
required: | ||
- weight | ||
properties: | ||
weight: | ||
type: number | ||
format: double | ||
minimum: 0 | ||
Alligator: | ||
allOf: | ||
- $ref: '#/components/schemas/HasAgeAndWeight' | ||
- $ref: '#/components/schemas/HasWeight' | ||
- type: object | ||
required: | ||
- num_teeth | ||
properties: | ||
num_teeth: | ||
type: integer | ||
format: int32 | ||
minimum: 0 | ||
Zebra: | ||
allOf: | ||
- $ref: '#/components/schemas/HasAgeAndWeight' | ||
- $ref: '#/components/schemas/HasWeight' | ||
- type: object | ||
required: | ||
- num_stripes | ||
properties: | ||
num_stripes: | ||
type: integer | ||
format: int32 | ||
minimum: 0 |
Oops, something went wrong.