From 5c91288341f3dedbd86f19fad5057c9284b275ca Mon Sep 17 00:00:00 2001 From: Martin Pallmann Date: Wed, 9 Feb 2022 11:09:54 +0100 Subject: [PATCH] #1808: add external Refs (#1831) * #1808: add external Refs * #1808: improvements * #1808: use single quotes --- .../schema/NameToSchemaReference.scala | 8 ++++- .../src/test/resources/expected_external.yml | 17 +++++++++++ .../tapir/docs/openapi/VerifyYamlTest.scala | 29 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 docs/openapi-docs/src/test/resources/expected_external.yml diff --git a/docs/apispec-docs/src/main/scala/sttp/tapir/docs/apispec/schema/NameToSchemaReference.scala b/docs/apispec-docs/src/main/scala/sttp/tapir/docs/apispec/schema/NameToSchemaReference.scala index 676520729a..bbaa418ee5 100644 --- a/docs/apispec-docs/src/main/scala/sttp/tapir/docs/apispec/schema/NameToSchemaReference.scala +++ b/docs/apispec-docs/src/main/scala/sttp/tapir/docs/apispec/schema/NameToSchemaReference.scala @@ -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) + } } } diff --git a/docs/openapi-docs/src/test/resources/expected_external.yml b/docs/openapi-docs/src/test/resources/expected_external.yml new file mode 100644 index 0000000000..2f56cbe7c3 --- /dev/null +++ b/docs/openapi-docs/src/test/resources/expected_external.yml @@ -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' diff --git a/docs/openapi-docs/src/test/scala/sttp/tapir/docs/openapi/VerifyYamlTest.scala b/docs/openapi-docs/src/test/scala/sttp/tapir/docs/openapi/VerifyYamlTest.scala index b80728a73f..dd96dc8b5c 100644 --- a/docs/openapi-docs/src/test/scala/sttp/tapir/docs/openapi/VerifyYamlTest.scala +++ b/docs/openapi-docs/src/test/scala/sttp/tapir/docs/openapi/VerifyYamlTest.scala @@ -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 @@ -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 @@ -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") { @@ -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") + ) + ) + } +}