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

sttp-apispec 0.9.0 #3677

Merged
merged 7 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package sttp.tapir.docs

import sttp.apispec.{ExampleMultipleValue, ExampleSingleValue, ExampleValue, SecurityScheme}
import sttp.apispec.{ExampleValue, SecurityScheme}
import sttp.tapir.{AnyEndpoint, Codec, EndpointInput, Schema, SchemaType}

import java.nio.ByteBuffer
Expand All @@ -26,8 +26,10 @@ package object apispec {
case _ => v.toString
}

private[docs] def exampleValue(v: String): ExampleValue = ExampleSingleValue(v)
private[docs] def exampleValue[T](codec: Codec[_, T, _], e: T): Option[ExampleValue] = exampleValue(codec.schema, codec.encode(e))
private[docs] def exampleValue(v: String): ExampleValue = ExampleValue.string(v)
private[docs] def exampleValue[T](codec: Codec[_, T, _], e: T): Option[ExampleValue] =
exampleValue(codec.schema, codec.encode(e))

private[docs] def exampleValue(schema: Schema[_], raw: Any): Option[ExampleValue] = {
// #3581: if there's a delimiter and the encoded value is a string, the codec will have produced a final
// representation (with the delimiter applied), but in the docs we want to show the split values
Expand All @@ -42,10 +44,10 @@ package object apispec {
}

(rawDelimited, schema.schemaType) match {
case (it: Iterable[_], SchemaType.SArray(_)) => Some(ExampleMultipleValue(it.map(rawToString).toList))
case (it: Iterable[_], _) => it.headOption.map(v => ExampleSingleValue(rawToString(v)))
case (it: Option[_], _) => it.map(v => ExampleSingleValue(rawToString(v)))
case (v, _) => Some(ExampleSingleValue(rawToString(v)))
case (it: Iterable[_], SchemaType.SArray(_)) => Some(ExampleValue.array(it.map(v => ExampleValue.string(rawToString(v)))))
case (it: Iterable[_], _) => it.headOption.map(v => ExampleValue.string(rawToString(v)))
case (it: Option[_], _) => it.map(v => ExampleValue.string(rawToString(v)))
case (v, _) => Some(ExampleValue.string(rawToString(v)))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private[docs] class TSchemaToASchema(
// the initial list of schemas.
val propagated = propagateMetadataForOption(schema, opt).element
val ref = toSchemaReference.map(propagated, name)
if (!markOptionsAsNullable) ref else ref.copy(nullable = Some(true))
if (!markOptionsAsNullable) ref else ref.nullable
case TSchemaType.SOption(el) => apply(el, allowReference = true, isOptionElement = true)
case TSchemaType.SBinary() => ASchema(SchemaType.String).copy(format = SchemaFormat.Binary)
case TSchemaType.SDate() => ASchema(SchemaType.String).copy(format = SchemaFormat.Date)
Expand All @@ -58,7 +58,10 @@ private[docs] class TSchemaToASchema(
.map(apply(_, allowReference = true))
.sortBy {
case schema if schema.$ref.isDefined => schema.$ref.get
case schema => schema.`type`.collect { case t: BasicSchemaType => t.value }.getOrElse("") + schema.toString
case schema => schema.`type`.collect {
case List(t) => t.value
case List(t, SchemaType.Null) => t.value
}.getOrElse("") + schema.toString
},
d.map(tDiscriminatorToADiscriminator)
)
Expand All @@ -82,7 +85,7 @@ private[docs] class TSchemaToASchema(
}

var s = result
s = if (nullable) s.copy(nullable = Some(true)) else s
s = if (nullable) s.nullable else s
s = addMetadata(s, schema)
s = addAttributes(s, schema)
s = addConstraints(s, primitiveValidators, schemaIsWholeNumber)
Expand Down Expand Up @@ -111,7 +114,7 @@ private[docs] class TSchemaToASchema(
oschema.copy(
description = tschema.description.orElse(oschema.description),
default = tDefaultToADefault(tschema).orElse(oschema.default),
example = tExampleToAExample(tschema).orElse(oschema.example),
examples = tExampleToAExample(tschema).map(List(_)).orElse(oschema.examples),
format = tschema.format.orElse(oschema.format),
deprecated = (if (tschema.deprecated) Some(true) else None).orElse(oschema.deprecated),
extensions = DocsExtensions.fromIterable(tschema.docsExtensions)
Expand All @@ -126,16 +129,14 @@ private[docs] class TSchemaToASchema(

private def addConstraints(aschema: ASchema, v: Validator.Primitive[_], wholeNumbers: Boolean): ASchema = {
v match {
case m @ Validator.Min(v, exclusive) =>
aschema.copy(
minimum = Some(toBigDecimal(v, m.valueIsNumeric, wholeNumbers)),
exclusiveMinimum = Option(exclusive).filter(identity)
)
case m @ Validator.Max(v, exclusive) =>
aschema.copy(
maximum = Some(toBigDecimal(v, m.valueIsNumeric, wholeNumbers)),
exclusiveMaximum = Option(exclusive).filter(identity)
)
case m @ Validator.Min(v, false) =>
aschema.copy(minimum = Some(toBigDecimal(v, m.valueIsNumeric, wholeNumbers)))
case m @ Validator.Min(v, true) =>
aschema.copy(exclusiveMinimum = Some(toBigDecimal(v, m.valueIsNumeric, wholeNumbers)))
case m @ Validator.Max(v, false) =>
aschema.copy(maximum = Some(toBigDecimal(v, m.valueIsNumeric, wholeNumbers)))
case m @ Validator.Max(v, true) =>
aschema.copy(exclusiveMaximum = Some(toBigDecimal(v, m.valueIsNumeric, wholeNumbers)))
case Validator.Pattern(value) => aschema.copy(pattern = Some(Pattern(value)))
case Validator.MinLength(value, _) => aschema.copy(minLength = Some(value))
case Validator.MaxLength(value, _) => aschema.copy(maxLength = Some(value))
Expand All @@ -148,7 +149,17 @@ private[docs] class TSchemaToASchema(
}

private def addEnumeration[T](aschema: ASchema, v: List[T], encode: EncodeToRaw[T]): ASchema = {
val values = v.flatMap(x => encode(x).map(ExampleSingleValue.apply))
val values = v.flatMap(x => encode(x).map {
case null => ExampleValue.Null
case v: String => ExampleValue.string(v)
case v: Int => ExampleValue.number(v)
case v: Long => ExampleValue.number(v)
case v: Float => ExampleValue.number(v)
case v: Double => ExampleValue.number(v)
case v: BigDecimal => ExampleValue.number(v)
case v: BigInt => ExampleValue.number(v)
case v => ExampleValue.string(v.toString)
})
aschema.copy(`enum` = if (values.nonEmpty) Some(values) else None)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private[schema] class ToSchemaReference(
// the differently customised properties are reset to their default values in ToKeyedSchemas (#1203)
if (originalSchema.description != schema.description) result = result.copy(description = schema.description)
if (originalSchema.default != schema.default) result = result.copy(default = tDefaultToADefault(schema))
if (originalSchema.encodedExample != schema.encodedExample) result = result.copy(example = tExampleToAExample(schema))
if (originalSchema.encodedExample != schema.encodedExample) result = result.copy(examples = tExampleToAExample(schema).map(List(_)))
if (originalSchema.deprecated != schema.deprecated && schema.deprecated) result = result.copy(deprecated = Some(schema.deprecated))
if (originalSchema.attributes.get(Title.Attribute) != schema.attributes.get(Title.Attribute))
result = result.copy(title = schema.attributes.get(Title.Attribute).map(_.value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private[asyncapi] class EndpointToAsyncAPIWebSocketChannel(
else
Some {
ASchema(
`type` = Some(ASchemaType.Object),
`type` = Some(List(ASchemaType.Object)),
required = fields.collect { case ((name, codec), _) if !codec.schema.isOptional => name }.toList,
properties = fields.map { case ((name, _), schema) => name -> schema }.toListMap
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private[asyncapi] object ExampleConverter {
case _: WebSocketFrame.Control => None
}

exampleValue.map(ExampleSingleValue)
exampleValue.map(ExampleValue.string)
}
}
}
2 changes: 1 addition & 1 deletion project/Versions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object Versions {
val sttp = "3.9.5"
val sttpModel = "1.7.9"
val sttpShared = "1.3.17"
val sttpApispec = "0.8.0"
val sttpApispec = "0.8.0+0-5924f9dc+20240411-1227-SNAPSHOT"
val akkaHttp = "10.2.10"
val akkaStreams = "2.6.20"
val pekkoHttp = "1.0.1"
Expand Down
Loading