Skip to content

Commit b9da4b7

Browse files
authored
Merge pull request #6 from SamTheisens/fix/spurious-required-fields
Fix/spurious required fields
2 parents 55ae58f + a3e5ef6 commit b9da4b7

File tree

4 files changed

+68
-15
lines changed

4 files changed

+68
-15
lines changed

.scalafmt.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# https://scalameta.org/scalafmt/docs/configuration.html
2+
version = "3.5.9"
3+
runner.dialect = scala213source3
4+
maxColumn = 140
5+
project.git = true
6+
7+
spaces.afterKeywordBeforeParen = true
8+
align.preset = none
9+
align.openParenCallSite = false
10+
align.openParenDefnSite = false

src/main/scala/com/github/swagger/enumeratum/converter/SwaggerEnumeratumModelConverter.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,3 @@ class SwaggerEnumeratumModelConverter extends ModelResolver(Json.mapper()) {
9797
case Some(arr) => arr.toList
9898
}
9999
}
100-

src/test/scala/com/github/swagger/enumeratum/converter/ModelPropertyParserTest.scala

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@ package com.github.swagger.enumeratum.converter
22

33
import io.swagger.v3.core.converter._
44
import io.swagger.v3.oas.models.media._
5-
import models.{Ctx, ModelWCtxEnum, ModelWCtxEnumAndAnnotation, ModelWEnum, ModelWEnumAnnotated, ModelWEnumSet, ModelWOptionalEnum, OrderSize}
5+
import models.{
6+
Animal,
7+
Ctx,
8+
ModelWCtxEnum,
9+
ModelWCtxEnumAndAnnotation,
10+
ModelWEnum,
11+
ModelWEnumAnnotated,
12+
ModelWEnumSet,
13+
ModelWOptionalEnum,
14+
OrderSize
15+
}
616
import org.scalatest.OptionValues
717
import org.scalatest.flatspec.AnyFlatSpec
818
import org.scalatest.matchers.should.Matchers
@@ -14,32 +24,32 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
1424
val converter = ModelConverters.getInstance()
1525
val schemas = converter.readAll(classOf[ModelWEnum]).asScala.toMap
1626
val model = findModel(schemas, "ModelWEnum")
17-
model should be (defined)
27+
model should be(defined)
1828
model.get.getProperties should not be (null)
1929
val field = model.value.getProperties.get("field")
20-
field shouldBe a [StringSchema]
30+
field shouldBe a[StringSchema]
2131
field.asInstanceOf[StringSchema].getEnum.asScala shouldEqual OrderSize.values.map(_.entryName)
2232
nullSafeList(model.value.getRequired) shouldBe Seq("field")
2333
}
2434
it should "process Model with Optional Enumeratum Enum" in {
2535
val converter = ModelConverters.getInstance()
2636
val schemas = converter.readAll(classOf[ModelWOptionalEnum]).asScala.toMap
2737
val model = findModel(schemas, "ModelWOptionalEnum")
28-
model should be (defined)
38+
model should be(defined)
2939
model.get.getProperties should not be (null)
3040
val field = model.value.getProperties.get("field")
31-
field shouldBe a [StringSchema]
41+
field shouldBe a[StringSchema]
3242
field.asInstanceOf[StringSchema].getEnum.asScala shouldEqual OrderSize.values.map(_.entryName)
3343
nullSafeList(model.value.getRequired) shouldBe empty
3444
}
3545
it should "process Model with Enumeratum Set" in {
3646
val converter = ModelConverters.getInstance()
3747
val schemas = converter.readAll(classOf[ModelWEnumSet]).asScala.toMap
3848
val model = findModel(schemas, "ModelWEnumSet")
39-
model should be (defined)
49+
model should be(defined)
4050
model.get.getProperties should not be (null)
4151
val field = model.value.getProperties.get("sizes")
42-
field shouldBe a [ArraySchema]
52+
field shouldBe a[ArraySchema]
4353
val arraySchema = field.asInstanceOf[ArraySchema]
4454
arraySchema.getItems.getEnum.asScala shouldEqual OrderSize.values.map(_.entryName)
4555
nullSafeList(model.value.getRequired) shouldEqual List("sizes")
@@ -48,10 +58,10 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
4858
val converter = ModelConverters.getInstance()
4959
val schemas = converter.readAll(classOf[ModelWEnumAnnotated]).asScala.toMap
5060
val model = findModel(schemas, "ModelWEnumAnnotated")
51-
model should be (defined)
61+
model should be(defined)
5262
model.get.getProperties should not be (null)
5363
val field = model.value.getProperties.get("field")
54-
field shouldBe a [StringSchema]
64+
field shouldBe a[StringSchema]
5565
val schema = field.asInstanceOf[StringSchema]
5666
schema.getDescription shouldEqual "enum value"
5767
schema.getEnum.asScala shouldEqual OrderSize.values.map(_.entryName)
@@ -62,17 +72,40 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
6272
val schemas = converter.readAll(classOf[ModelWCtxEnum]).asScala.toMap
6373
schemas.keys should have size 1
6474
val model = findModel(schemas, "ModelWCtxEnum")
65-
model should be (defined)
75+
model should be(defined)
6676
model.get.getProperties should not be (null)
77+
6778
val field = model.value.getProperties.get("field")
68-
field shouldBe a [StringSchema]
79+
field shouldBe a[StringSchema]
80+
field.asInstanceOf[StringSchema].getRequired shouldBe null
81+
6982
val schema = field.asInstanceOf[StringSchema]
7083
schema.getDescription shouldEqual (null)
71-
schema.getDefault should be (null)
84+
schema.getDefault should be(null)
7285
schema.getEnum.asScala shouldEqual Ctx.Color.values.map(_.entryName)
7386
nullSafeList(model.value.getRequired) shouldBe Seq("field")
7487
}
7588

89+
it should "process sealed abstract class" in {
90+
val converter = ModelConverters.getInstance()
91+
val schemas = converter.readAll(classOf[Animal]).asScala.toMap
92+
val catModel = findModel(schemas, "Cat")
93+
catModel should be(defined)
94+
val catProps = catModel.value.getProperties
95+
catProps should have size 3
96+
catProps.get("name") shouldBe a[StringSchema]
97+
catProps.get("age") shouldBe a[IntegerSchema]
98+
catProps.get("animalType") shouldBe a[StringSchema]
99+
catModel.value.getRequired.asScala shouldEqual Seq("age", "animalType", "name")
100+
val dogModel = findModel(schemas, "Dog")
101+
dogModel should be(defined)
102+
val dogProps = dogModel.value.getProperties
103+
dogProps should have size 2
104+
dogProps.get("name") shouldBe a[StringSchema]
105+
dogProps.get("animalType") shouldBe a[StringSchema]
106+
dogModel.value.getRequired.asScala shouldEqual Seq("animalType", "name")
107+
}
108+
76109
it should "not add additional model when enum field is annotated" in {
77110
val converter = ModelConverters.getInstance()
78111
val schemas = converter.readAll(classOf[ModelWCtxEnumAndAnnotation]).asScala.toMap
@@ -86,8 +119,8 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
86119
val schema = field.asInstanceOf[StringSchema]
87120
schema.getDescription shouldEqual "An annotated field"
88121
schema.getName shouldEqual "field"
89-
schema.getDefault should be (null)
90-
schema.getDeprecated should be (null)
122+
schema.getDefault should be(null)
123+
schema.getDeprecated should be(null)
91124
schema.getEnum.asScala shouldEqual Ctx.Color.values.map(_.entryName)
92125
nullSafeList(model.value.getRequired) shouldBe Seq("field")
93126
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package models
2+
3+
sealed abstract class Animal(val animalType: String) {
4+
val name: String
5+
}
6+
7+
class Dog(val name: String) extends Animal("Dog")
8+
9+
class Cat(val name: String, val age: Int) extends Animal("Cat")
10+
11+
case class PetOwner(owner: String, pet: Animal)

0 commit comments

Comments
 (0)