Last active
March 16, 2018 07:31
-
-
Save jenol/975ea072e60a893efedb9d51643ac812 to your computer and use it in GitHub Desktop.
x-nullable for swagger-akka-http
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
class SwaggerDocService(canonicalHost: String, canonicalPort: Int)( | |
implicit override val actorSystem: ActorSystem, | |
override val materializer: ActorMaterializer | |
) extends SwaggerHttpService | |
with HasActorSystem { | |
override val apiTypes = Seq( | |
// your routes | |
typeOf[XNullableExtender] // you register listeners the same way as routes. The reader will sort out what is what | |
) |
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
import io.swagger.jaxrs.config.ReaderListener | |
import io.swagger.models.Swagger | |
import scala.collection.JavaConverters._ | |
class XNullableExtender extends ReaderListener { | |
override def afterScan(reader: Reader, swagger: Swagger) = { | |
val definitions: mutable.Map[String, Model] = swagger.getDefinitions.asScala | |
definitions.foreach(kv => { | |
val (_, model) = kv | |
val properties: mutable.Map[String, Property] = model.getProperties.asScala | |
properties.foreach(kv => { | |
val (_, property) = kv | |
if (!property.getRequired) { | |
val extensions: mutable.Map[String, AnyRef] = property.getVendorExtensions.asScala | |
extensions("x-nullable") = Predef.boolean2Boolean(true) | |
} | |
}) | |
}) | |
} | |
override def beforeScan(reader: Reader, swagger: Swagger) = {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment