-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1818 from softwaremill/annotations-enumeratum
macro for collecting schema annotations
- Loading branch information
Showing
8 changed files
with
205 additions
and
24 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
core/src/main/scala-2/sttp/tapir/internal/SchemaAnnotations.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 sttp.tapir.internal | ||
|
||
import sttp.tapir.Schema.SName | ||
import sttp.tapir.{Schema, Validator} | ||
|
||
final case class SchemaAnnotations[T]( | ||
description: Option[String], | ||
encodedExample: Option[Any], | ||
default: Option[T], | ||
format: Option[String], | ||
deprecated: Option[Boolean], | ||
encodedName: Option[String], | ||
validate: Option[Validator[T]] | ||
) { | ||
private case class SchemaEnrich(current: Schema[T]) { | ||
def optionally(f: Schema[T] => Option[Schema[T]]): SchemaEnrich = f(current).map(SchemaEnrich.apply).getOrElse(this) | ||
} | ||
|
||
def enrich(s: Schema[T]): Schema[T] = { | ||
SchemaEnrich(s) | ||
.optionally(s => description.map(s.description(_))) | ||
.optionally(s => encodedExample.map(s.encodedExample(_))) | ||
.optionally(s => default.map(s.default(_))) | ||
.optionally(s => format.map(s.format(_))) | ||
.optionally(s => deprecated.map(s.deprecated(_))) | ||
.optionally(s => encodedName.map(en => s.name(SName(en)))) | ||
.optionally(s => validate.map(s.validate)) | ||
.current | ||
} | ||
} | ||
|
||
object SchemaAnnotations { | ||
implicit def schemaAnnotations[T]: SchemaAnnotations[T] = macro SchemaAnnotationsMacro.derived[T] | ||
} |
33 changes: 33 additions & 0 deletions
33
core/src/main/scala-2/sttp/tapir/internal/SchemaAnnotationsMacro.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,33 @@ | ||
package sttp.tapir.internal | ||
|
||
import scala.reflect.macros.blackbox | ||
|
||
object SchemaAnnotationsMacro { | ||
def derived[T: c.WeakTypeTag](c: blackbox.Context): c.Expr[SchemaAnnotations[T]] = { | ||
import c.universe._ | ||
|
||
val DescriptionAnn = typeOf[sttp.tapir.Schema.annotations.description] | ||
val EncodedExampleAnn = typeOf[sttp.tapir.Schema.annotations.encodedExample] | ||
val DefaultAnn = typeOf[sttp.tapir.Schema.annotations.default[_]] | ||
val FormatAnn = typeOf[sttp.tapir.Schema.annotations.format] | ||
val DeprecatedAnn = typeOf[sttp.tapir.Schema.annotations.deprecated] | ||
val EncodedNameAnn = typeOf[sttp.tapir.Schema.annotations.encodedName] | ||
val ValidateAnn = typeOf[sttp.tapir.Schema.annotations.validate[_]] | ||
|
||
val annotations = weakTypeOf[T].typeSymbol.annotations | ||
|
||
val firstArg: Annotation => Tree = a => a.tree.children.tail.head | ||
|
||
val description = annotations.collectFirst { case ann if ann.tree.tpe <:< DescriptionAnn => firstArg(ann) } | ||
val encodedExample = annotations.collectFirst { case ann if ann.tree.tpe <:< EncodedExampleAnn => firstArg(ann) } | ||
val default = annotations.collectFirst { case ann if ann.tree.tpe <:< DefaultAnn => firstArg(ann) } | ||
val format = annotations.collectFirst { case ann if ann.tree.tpe <:< FormatAnn => firstArg(ann) } | ||
val deprecated = annotations.collectFirst { case ann if ann.tree.tpe <:< DeprecatedAnn => q"""true""" } | ||
val encodedName = annotations.collectFirst { case ann if ann.tree.tpe <:< EncodedNameAnn => firstArg(ann) } | ||
val validator = annotations.collectFirst { case ann if ann.tree.tpe <:< ValidateAnn => firstArg(ann) } | ||
|
||
c.Expr[SchemaAnnotations[T]]( | ||
q"""_root_.sttp.tapir.internal.SchemaAnnotations.apply($description, $encodedExample, $default, $format, $deprecated, $encodedName, $validator)""" | ||
) | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
docs/openapi-docs/src/test/resources/validator/expected_valid_enumeratum_with_metadata.yml
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,45 @@ | ||
openapi: 3.0.3 | ||
info: | ||
title: Numbers | ||
version: '1.0' | ||
paths: | ||
/numbers: | ||
get: | ||
operationId: getNumbers | ||
requestBody: | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/NumberWithMsg' | ||
required: true | ||
responses: | ||
'200': | ||
description: '' | ||
'400': | ||
description: 'Invalid value for: body' | ||
content: | ||
text/plain: | ||
schema: | ||
type: string | ||
components: | ||
schemas: | ||
MyNumber: | ||
type: integer | ||
description: |- | ||
* 1 - One | ||
* 2 - Two | ||
* 3 - Three | ||
enum: | ||
- 1 | ||
- 2 | ||
- 3 | ||
NumberWithMsg: | ||
required: | ||
- number | ||
- msg | ||
type: object | ||
properties: | ||
number: | ||
$ref: '#/components/schemas/MyNumber' | ||
msg: | ||
type: string |
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
Oops, something went wrong.