From 2cc01721bda112eec2dc4bcafa43678b98c46d40 Mon Sep 17 00:00:00 2001 From: Roman Janusz Date: Mon, 22 Apr 2024 17:46:17 +0200 Subject: [PATCH] added test for enum & const checking --- .../validation/SchemaCompatibilityIssue.scala | 6 ++-- .../validation/SchemaComparatorTest.scala | 33 ++++++++++++++++++- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/apispec-model/src/main/scala/sttp/apispec/validation/SchemaCompatibilityIssue.scala b/apispec-model/src/main/scala/sttp/apispec/validation/SchemaCompatibilityIssue.scala index efe0cac..41180af 100644 --- a/apispec-model/src/main/scala/sttp/apispec/validation/SchemaCompatibilityIssue.scala +++ b/apispec-model/src/main/scala/sttp/apispec/validation/SchemaCompatibilityIssue.scala @@ -1,6 +1,6 @@ package sttp.apispec.validation -import sttp.apispec.{Pattern, Schema, SchemaType} +import sttp.apispec.{ExampleSingleValue, ExampleValue, Pattern, Schema, SchemaType} sealed abstract class SchemaCompatibilityIssue extends Product { def description: String @@ -47,8 +47,8 @@ case class TypeMismatch( case class EnumMismatch( // None indicates that the writer schema has no enum values - incompatibleWriterValues: Option[List[Any]], - readerValues: List[Any] + incompatibleWriterValues: Option[List[ExampleValue]], + readerValues: List[ExampleValue] ) extends SchemaCompatibilityIssue { def description: String = { val writerValuesRepr = diff --git a/apispec-model/src/test/scala/sttp/apispec/validation/SchemaComparatorTest.scala b/apispec-model/src/test/scala/sttp/apispec/validation/SchemaComparatorTest.scala index 4434b0f..38abb6a 100644 --- a/apispec-model/src/test/scala/sttp/apispec/validation/SchemaComparatorTest.scala +++ b/apispec-model/src/test/scala/sttp/apispec/validation/SchemaComparatorTest.scala @@ -1,7 +1,7 @@ package sttp.apispec.validation import org.scalatest.funsuite.AnyFunSuite -import sttp.apispec.{Pattern, Schema, SchemaType} +import sttp.apispec.{ExampleSingleValue, Pattern, Schema, SchemaType} import scala.collection.immutable.ListMap @@ -130,5 +130,36 @@ class SchemaComparatorTest extends AnyFunSuite { )) } + test("enum and const") { + def enums(values: Any*): List[ExampleSingleValue] = + values.toList.map(ExampleSingleValue) + + def enumSchema(values: String*): Schema = values.toList match { + case single :: Nil => stringSchema.copy(enum = Some(List(single).map(ExampleSingleValue))) + case multiple => stringSchema.copy(enum = Some(multiple.map(ExampleSingleValue))) + } + + assert(compare(enumSchema("a"), stringSchema) == Nil) + assert(compare(enumSchema("a"), enumSchema("a")) == Nil) + assert(compare(enumSchema("a"), enumSchema("a", "b")) == Nil) + assert(compare(enumSchema("a", "b"), enumSchema("a", "b", "c")) == Nil) + + assert(compare(stringSchema, enumSchema("a", "b")) == List( + EnumMismatch(None, enums("a", "b")) + )) + assert(compare(enumSchema("a"), enumSchema("b")) == List( + EnumMismatch(Some(enums("a")), enums("b")) + )) + assert(compare(enumSchema("a"), enumSchema("b", "c")) == List( + EnumMismatch(Some(enums("a")), enums("b", "c")) + )) + assert(compare(enumSchema("a", "b"), enumSchema("c")) == List( + EnumMismatch(Some(enums("a", "b")), enums("c")) + )) + assert(compare(enumSchema("a", "b"), enumSchema("b", "c")) == List( + EnumMismatch(Some(enums("a")), enums("b", "c")) + )) + } + //TODO significantly more tests }