softwaremill / sttp-apispec

OpenAPI, AsyncAPI and JSON Schema Scala models.
Apache License 2.0
23 stars 11 forks source link

Missing `ListMap` properties break decoding #136

Closed Primetalk closed 7 months ago

Primetalk commented 7 months ago

When decoding MediaType without examples, circe issues an error:

DecodingFailure at ./url.post.parameters.examples: Missing required field

A better alternative would be to use ListMap.empty. The implementation in https://github.com/softwaremill/sttp-apispec/blob/master/openapi-circe/src/main/scala/sttp/apispec/openapi/internal/InternalSttpOpenAPICirceDecoders.scala#L61:

  implicit val mediaTypeDecoder: Decoder[MediaType] = withExtensions(deriveDecoder[MediaType])

is a bit too simple. It seems that we'd better use something like https://github.com/softwaremill/sttp-apispec/blob/master/openapi-circe/src/main/scala/sttp/apispec/openapi/internal/InternalSttpOpenAPICirceDecoders.scala#L65:

  implicit val responseDecoder: Decoder[Response] = {
    implicit def listMapDecoder[A: Decoder]: Decoder[ListMap[String, ReferenceOr[A]]] =
      Decoder.decodeOption(Decoder.decodeMapLike[String, ReferenceOr[A], ListMap]).map(_.getOrElse(ListMap.empty))

    implicit def listMapMediaTypeDecoder: Decoder[ListMap[String, MediaType]] =
      Decoder.decodeOption(Decoder.decodeMapLike[String, MediaType, ListMap]).map(_.getOrElse(ListMap.empty))

    withExtensions(deriveDecoder[Response])
  }
adamw commented 7 months ago

@Primetalk Yes, decoding might have some holes. Maybe you'd like to attempt creating a PR to fix this? :)

Primetalk commented 7 months ago

@adamw Could you take a look https://github.com/softwaremill/sttp-apispec/pull/138 ?