Skip to content

Commit

Permalink
#1808: add external Refs (#1831)
Browse files Browse the repository at this point in the history
* #1808: add external Refs

* #1808: improvements

* #1808: use single quotes
  • Loading branch information
martinpallmann authored Feb 9, 2022
1 parent 8047ad4 commit 5c91288
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import sttp.tapir.{Schema => TSchema}

private[schema] class NameToSchemaReference(nameToKey: Map[TSchema.SName, ObjectKey]) {
def map(name: TSchema.SName): Reference = {
Reference.to("#/components/schemas/", nameToKey(name))
nameToKey.get(name) match {
case Some(key) =>
Reference.to("#/components/schemas/", key)
case None =>
// no reference to internal model found. assuming external reference
Reference(name.fullName)
}
}
}
17 changes: 17 additions & 0 deletions docs/openapi-docs/src/test/resources/expected_external.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
openapi: '3.0.3'
info:
title: 'Fruits'
version: '1.0'
paths:
/:
get:
operationId: 'getRoot'
responses:
'200':
description: ''
default:
description: ''
content:
application/json:
schema:
$ref: 'https://example.com/models/model.yaml#/Problem'
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sttp.tapir.docs.openapi

import io.circe.Json
import io.circe.generic.auto._
import io.circe.yaml.Printer.StringStyle
import io.circe.yaml.Printer.StringStyle.{DoubleQuoted, Literal}
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
Expand All @@ -10,6 +11,7 @@ import sttp.model.{Method, StatusCode}
import sttp.tapir.Schema.SName
import sttp.tapir.Schema.annotations.description
import sttp.tapir.docs.apispec.DocsExtension
import sttp.tapir.docs.openapi.VerifyYamlTest.Problem
import sttp.tapir.docs.openapi.dtos.VerifyYamlTestData._
import sttp.tapir.docs.openapi.dtos.VerifyYamlTestData2._
import sttp.tapir.docs.openapi.dtos.Book
Expand Down Expand Up @@ -44,6 +46,20 @@ class VerifyYamlTest extends AnyFunSuite with Matchers {
actualYamlNoIndent shouldBe expectedYaml
}

test("should match the expected yaml when there are external references") {
val external_reference: PublicEndpoint[Unit, Problem, Unit, Any] =
endpoint.errorOut(jsonBody[Problem])

val expectedYaml = load("expected_external.yml")

val actualYaml =
OpenAPIDocsInterpreter().toOpenAPI(List(external_reference), Info("Fruits", "1.0")).toYaml(StringStyle.SingleQuoted)

val actualYamlNoIndent = noIndentation(actualYaml)

actualYamlNoIndent shouldBe expectedYaml
}

val endpoint_wit_recursive_structure: Endpoint[Unit, Unit, Unit, F1, Any] = endpoint.out(jsonBody[F1])

test("should match the expected yaml when schema is recursive") {
Expand Down Expand Up @@ -602,3 +618,16 @@ class VerifyYamlTest extends AnyFunSuite with Matchers {
noIndentation(actualYaml) shouldBe expectedYaml
}
}

object VerifyYamlTest {
case class Problem()

object Problem {
implicit val schema: Schema[Problem] =
Schema[Problem](
SchemaType.SRef(
Schema.SName("https://example.com/models/model.yaml#/Problem")
)
)
}
}

0 comments on commit 5c91288

Please sign in to comment.