tapir icon indicating copy to clipboard operation
tapir copied to clipboard

Feature request: ability to override servers for concrete endpoint

Open payurgin opened this issue 5 years ago • 1 comments

With swagger we can override default server for concrete endpoint: https://swagger.io/docs/specification/api-host-and-base-path/

WDYT about adding special syntax to ServerEndpoint ? Out use-case: We have one application server based on tapir which also serves documentation. We also have proxy for application with authorization logic and two domains: secured-api.domain.com and api.domain.com. So most of our method hosted on api.domain.com but few hosted on secured-api.domain.com


case class Endpoint[I, E, O, +S](input: EndpointInput[I], errorOutput: EndpointOutput[E], output: EndpointOutput[O], info: EndpointInfo) {
   ...
   ...
   def server(server: EndpointServer): Endpoint[I, E, O, S]
   def servers(server: EndpointServer, servers: EndpointServer*): Endpoint[I, E, O, S]
}

case class EndpointServer(url: String,
                          description: Option[String] = None,
                          variables: Option[ListMap[String, EndpointServer.Variable]] = None) {

  def description(d: String): EndpointServer = copy(description = Some(d))
  def variables(vars: (String, EndpointServer.Variable)*): EndpointServer = copy(variables = Some(ListMap(vars: _*)))
}

object EndpointServer {
  case class Variable(enum: Option[List[String]], default: String, description: Option[String]) {
    require(`enum`.fold(true)(_.contains(default)), "ServerVariable#default must be one of the values in enum if enum is defined")
  }
}

and we can use it as:

endpoint.get.in("foo" / "bar").server(ServerEndpoint("secured-api.domain.com"))

payurgin avatar Jul 08 '20 20:07 payurgin

This refers to softwaremill/sttp-apispec#3

ghostbuster91 avatar Jul 09 '20 11:07 ghostbuster91