Skip to content

Commit

Permalink
sttp-apispec 0.9.0 (#3677)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghik committed Apr 18, 2024
1 parent 6fe95e4 commit 59e48b9
Show file tree
Hide file tree
Showing 103 changed files with 210 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ package object apispec {

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(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 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 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
6 changes: 3 additions & 3 deletions docs/asyncapi-docs/src/test/resources/expected_binding.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ channels:
ws:
method: POST
query:
type: object
required:
- multiplier
type: object
properties:
multiplier:
type: string
headers:
type: object
required:
- Authorization
type: object
properties:
Authorization:
type: string
components:
schemas:
Fruit:
title: Fruit
type: object
required:
- f
type: object
properties:
f:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ channels:
ws:
method: GET
headers:
type: object
required:
- Test
type: object
properties:
Test:
type: string
description: Test token
type: string
components:
messages:
string:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ channels:
type: object
properties:
limit:
type: integer
description: GET `limit` field description
type: integer
format: int32
offset:
type: integer
description: GET `offset` field description
type: integer
format: int32
components:
messages:
Expand Down
4 changes: 2 additions & 2 deletions docs/asyncapi-docs/src/test/resources/expected_extensions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ components:
schemas:
FruitAmount:
title: FruitAmount
type: object
required:
- fruit
- amount
type: object
properties:
fruit:
type: string
Expand All @@ -45,9 +45,9 @@ components:
format: int32
Fruit:
title: Fruit
type: object
required:
- f
type: object
properties:
f:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ channels:
ws:
method: GET
headers:
type: object
required:
- Test
type: object
properties:
Test:
type: string
description: Test token
deprecated: true
type: string
components:
messages:
string:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ channels:
ws:
method: GET
query:
type: object
required:
- limit
- offset
type: object
properties:
limit:
type: integer
format: int32
offset:
deprecated: true
type: integer
format: int32
deprecated: true
components:
messages:
string:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ components:
schemas:
sttp.tapir.tests.data.Fruit:
title: sttp.tapir.tests.data.Fruit
type: object
required:
- f
type: object
properties:
f:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ components:
schemas:
Fruit:
title: Fruit
type: object
required:
- f
type: object
properties:
f:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ components:
schemas:
Fruit:
title: Fruit
type: object
required:
- f
type: object
properties:
f:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ channels:
ws:
method: GET
query:
type: object
required:
- limit
type: object
properties:
limit:
type: integer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ components:
schemas:
Fruit:
title: Fruit
type: object
required:
- f
type: object
properties:
f:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ components:
schemas:
Fruit:
title: Fruit
type: object
required:
- f
type: object
properties:
f:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ components:
schemas:
Fruit:
title: Fruit
type: object
required:
- f
type: object
properties:
f:
type: string
FruitAmount:
title: FruitAmount
type: object
required:
- fruit
- amount
type: object
properties:
fruit:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ components:
- $ref: '#/components/schemas/Person'
Organization:
title: Organization
type: object
required:
- name
type: object
properties:
name:
type: string
Person:
title: Person
type: object
required:
- name
- age
type: object
properties:
name:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ components:
sml: '#/components/schemas/Organization'
Organization:
title: Organization
type: object
required:
- name
type: object
properties:
name:
type: string
Person:
title: Person
type: object
required:
- name
- age
type: object
properties:
name:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ components:
sml: '#/components/schemas/Organization'
NestedEntity:
title: NestedEntity
type: object
required:
- entity
type: object
properties:
entity:
$ref: '#/components/schemas/Entity'
Organization:
title: Organization
type: object
required:
- name
type: object
properties:
name:
type: string
Person:
title: Person
type: object
required:
- name
- age
type: object
properties:
name:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ components:
Square: '#/components/schemas/Square'
Square:
title: Square
type: object
required:
- color
- shapeType
type: object
properties:
color:
type: string
Expand Down

0 comments on commit 59e48b9

Please sign in to comment.