Skip to content

Commit

Permalink
Merge branch 'feature/upgrade-victools-jsonschemagenerator' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
SMILEY4 committed Nov 11, 2023
2 parents 66ed370 + fc0cc75 commit e5171bf
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 37 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ dependencies {
val swaggerParserVersion = "2.1.13"
implementation("io.swagger.parser.v3:swagger-parser:$swaggerParserVersion")

val jsonSchemaGeneratorVersion = "4.28.0"
val jsonSchemaGeneratorVersion = "4.32.0"
implementation("com.github.victools:jsonschema-generator:$jsonSchemaGeneratorVersion")
implementation("com.github.victools:jsonschema-module-jackson:$jsonSchemaGeneratorVersion")
implementation("com.github.victools:jsonschema-module-swagger-2:$jsonSchemaGeneratorVersion")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ data class EncodingData(
*/
fun schemaGeneratorConfigBuilder(): SchemaGeneratorConfigBuilder =
SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON)
.with(JacksonModule())
.with(Swagger2Module())
.with(Option.EXTRA_OPEN_API_FORMAT_VALUES)
.with(Option.ALLOF_CLEANUP_AT_THE_END)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import com.github.victools.jsonschema.generator.OptionPreset
import com.github.victools.jsonschema.generator.SchemaGenerator
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder
import com.github.victools.jsonschema.generator.SchemaVersion
import com.github.victools.jsonschema.module.jackson.JacksonModule
import com.github.victools.jsonschema.module.swagger2.Swagger2Module
import io.github.smiley4.ktorswaggerui.dsl.Example
import io.github.smiley4.ktorswaggerui.dsl.SchemaEncoder
import io.github.smiley4.ktorswaggerui.dsl.getSchemaType
import io.github.smiley4.ktorswaggerui.builder.schema.SchemaBuilder
import io.github.smiley4.ktorswaggerui.builder.schema.SchemaDefinitions
import io.github.smiley4.ktorswaggerui.builder.schema.SchemaTypeAttributeOverride
import io.github.smiley4.ktorswaggerui.builder.schema.TypeOverwrites
import io.github.smiley4.ktorswaggerui.dsl.Example
import io.github.smiley4.ktorswaggerui.dsl.SchemaEncoder
import io.github.smiley4.ktorswaggerui.dsl.getSchemaType
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.collections.shouldContainExactly
import io.kotest.matchers.maps.shouldHaveSize
Expand All @@ -30,35 +29,6 @@ import kotlin.reflect.jvm.javaType

class SchemaBuilderTest : StringSpec({

"victools field names".config(enabled = false) {
/**
* Test-case for https://github.com/SMILEY4/ktor-swagger-ui/issues/60.
* -> victools incorrectly ignores fields with is[A-Z]
*/
createSchemaVictools<WithFieldNames>(false).also { defs ->
defs.root.also { schema ->
schema.`$ref` shouldBe null
schema.type shouldBe "object"
schema.properties.keys shouldContainExactly setOf("flag", "getAnotherText", "text", "isSomething", "isSomeText")
schema.properties["flag"]!!.also { prop ->
prop.type shouldBe "boolean"
}
schema.properties["isSomething"]!!.also { prop ->
prop.type shouldBe "boolean"
}
schema.properties["text"]!!.also { prop ->
prop.type shouldBe "string"
}
schema.properties["getAnotherText"]!!.also { prop ->
prop.type shouldBe "string"
}
schema.properties["isSomeText"]!!.also { prop ->
prop.type shouldBe "string"
}
}
}
}

"primitive (victools, all definitions)" {
createSchemaVictools<Int>(true).also { defs ->
defs.definitions.keys shouldContainExactly setOf("int")
Expand Down Expand Up @@ -149,6 +119,31 @@ class SchemaBuilderTest : StringSpec({
}
}

"object with special field names (victools)" {
createSchemaVictools<WithFieldNames>(false).also { defs ->
defs.root.also { schema ->
schema.`$ref` shouldBe null
schema.type shouldBe "object"
schema.properties.keys shouldContainExactly setOf("flag", "getAnotherText", "text", "isSomething", "isSomeText")
schema.properties["flag"]!!.also { prop ->
prop.type shouldBe "boolean"
}
schema.properties["isSomething"]!!.also { prop ->
prop.type shouldBe "boolean"
}
schema.properties["text"]!!.also { prop ->
prop.type shouldBe "string"
}
schema.properties["getAnotherText"]!!.also { prop ->
prop.type shouldBe "string"
}
schema.properties["isSomeText"]!!.also { prop ->
prop.type shouldBe "string"
}
}
}
}

"simple object (kotlinx, definitions)" {
createSchemaKotlinX<Pet>(true).also { defs ->
defs.definitions.keys shouldContainExactly setOf("1d8t6cs0dbcap", "x1xjd3yo2dbzzz", "xq0zwcprkn9j3")
Expand Down Expand Up @@ -209,6 +204,31 @@ class SchemaBuilderTest : StringSpec({
}
}

"object with special field names (kotlinx)" {
createSchemaKotlinX<WithFieldNames>(false).also { defs ->
defs.root.also { schema ->
schema.`$ref` shouldBe null
schema.type shouldBe "object"
schema.properties.keys shouldContainExactly setOf("flag", "getAnotherText", "text", "isSomething", "isSomeText")
schema.properties["flag"]!!.also { prop ->
prop.type shouldBe "boolean"
}
schema.properties["isSomething"]!!.also { prop ->
prop.type shouldBe "boolean"
}
schema.properties["text"]!!.also { prop ->
prop.type shouldBe "string"
}
schema.properties["getAnotherText"]!!.also { prop ->
prop.type shouldBe "string"
}
schema.properties["isSomeText"]!!.also { prop ->
prop.type shouldBe "string"
}
}
}
}

//==== SIMPLE LIST =====================================================

"simple list (victools, all definitions)" {
Expand Down Expand Up @@ -432,7 +452,8 @@ class SchemaBuilderTest : StringSpec({

)

data class WithFieldNames(
@Serializable
class WithFieldNames(
val flag: Boolean,
val isSomething: Boolean,
val text: String,
Expand All @@ -457,7 +478,6 @@ class SchemaBuilderTest : StringSpec({
return { type ->
SchemaGenerator(
SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON)
.with(JacksonModule())
.with(Swagger2Module())
.with(Option.EXTRA_OPEN_API_FORMAT_VALUES)
.with(Option.ALLOF_CLEANUP_AT_THE_END)
Expand Down

0 comments on commit e5171bf

Please sign in to comment.