Skip to content

Commit

Permalink
Merge pull request #3100 from kazievab/swagger-ui-extensions-option
Browse files Browse the repository at this point in the history
Add support showExtensions option for swagger-ui
  • Loading branch information
adamw authored Aug 17, 2023
2 parents 6c0d982 + 4efcd84 commit ec175ff
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/docs/openapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ val openAPIYaml = OpenAPIDocsInterpreter().toOpenAPI(sampleEndpoint, Info("title
However, to add extensions to other unusual places (like, `License` or `Server`, etc.) you should modify the `OpenAPI`
object manually or using a tool such as [quicklens](https://github.com/softwaremill/quicklens).

If you are using `tapir-swagger-ui` you need to set `withShowExtensions` option for `SwaggerUIOptions`.

## Hiding inputs/outputs

It's possible to hide an input/output from the OpenAPI description using following syntax:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,16 @@ object SwaggerUI {
val swaggerInitializerJsWithReplacedUrl =
swaggerInitializerJs.replace("https://petstore.swagger.io/v2/swagger.json", s"${concat(fullPathPrefix, options.yamlName)}")

val swaggerInitializerJsWithOptions =
swaggerInitializerJsWithReplacedUrl.replace(
"window.ui = SwaggerUIBundle({",
s"""window.ui = SwaggerUIBundle({
| showExtensions: ${options.showExtensions},""".stripMargin
)

val textJavascriptUtf8: EndpointIO.Body[String, String] = stringBodyUtf8AnyFormat(Codec.string.format(CodecFormat.TextJavascript()))
val swaggerInitializerJsEndpoint =
baseEndpoint.in("swagger-initializer.js").out(textJavascriptUtf8).serverLogicPure[F](_ => Right(swaggerInitializerJsWithReplacedUrl))
baseEndpoint.in("swagger-initializer.js").out(textJavascriptUtf8).serverLogicPure[F](_ => Right(swaggerInitializerJsWithOptions))

val resourcesEndpoint = staticResourcesGetServerEndpoint[F](prefixInput)(
SwaggerUI.getClass.getClassLoader,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,26 @@ package sttp.tapir.swagger
* referrer's uri). Also used for creating redirects. Defaults to `Nil`.
* @param useRelativePaths
* Should relative paths be used for yaml references and redirects. Defaults to `true`.
* @param showExtensions
* Should display the content of vendor extensions (x-) fields and values for Operations, Parameters, Responses, and Schema. Defaults to
* `false`.
*/
case class SwaggerUIOptions(pathPrefix: List[String], yamlName: String, contextPath: List[String], useRelativePaths: Boolean) {
case class SwaggerUIOptions(
pathPrefix: List[String],
yamlName: String,
contextPath: List[String],
useRelativePaths: Boolean,
showExtensions: Boolean
) {
def pathPrefix(pathPrefix: List[String]): SwaggerUIOptions = copy(pathPrefix = pathPrefix)
def yamlName(yamlName: String): SwaggerUIOptions = copy(yamlName = yamlName)
def contextPath(contextPath: List[String]): SwaggerUIOptions = copy(contextPath = contextPath)
def withRelativePaths: SwaggerUIOptions = copy(useRelativePaths = true)
def withAbsolutePaths: SwaggerUIOptions = copy(useRelativePaths = false)
def withShowExtensions: SwaggerUIOptions = copy(showExtensions = true)
def withHideExtensions: SwaggerUIOptions = copy(showExtensions = false)
}

object SwaggerUIOptions {
val default: SwaggerUIOptions = SwaggerUIOptions(List("docs"), "docs.yaml", Nil, useRelativePaths = true)
val default: SwaggerUIOptions = SwaggerUIOptions(List("docs"), "docs.yaml", Nil, useRelativePaths = true, showExtensions = false)
}

0 comments on commit ec175ff

Please sign in to comment.