Skip to content

Commit

Permalink
Merge branch 'bugfix/correctly-generate-json-schema-for-maps' into re…
Browse files Browse the repository at this point in the history
…lease
  • Loading branch information
SMILEY4 committed Jan 20, 2023
2 parents 3d3a6b8 + 000a322 commit 77b6b3f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "io.github.smiley4"
version = "1.0.1"
version = "1.0.2"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,6 @@ class SwaggerUIPluginConfig {
.with(Option.INLINE_ALL_SCHEMAS)
.with(Option.EXTRA_OPEN_API_FORMAT_VALUES)
.with(Option.ALLOF_CLEANUP_AT_THE_END)
.with(Option.MAP_VALUES_AS_ADDITIONAL_PROPERTIES)

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ class JsonToOpenApiSchemaConverter {
return Schema<Any>().apply {
node["\$schema"]?.let { this.`$schema` = it.asText() }
node["type"]?.let { this.type = it.asText() }
node["format"]?.let { this.format = it.asText() }
node["items"]?.let { this.items = toSchema(it) }
node["properties"]?.let { this.properties = it.collectFields().associate { prop -> prop.key to toSchema(prop.value) } }
node["additionalProperties"]?.let { this.additionalProperties = toSchema(it) }
node["allOf"]?.let { this.allOf = it.collectElements().map { prop -> toSchema(prop) } }
node["anyOf"]?.let { this.anyOf = it.collectElements().map { prop -> toSchema(prop) } }
node["required"]?.let { this.required = it.collectElements().map { prop -> prop.asText() } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ infix fun Schema<*>.shouldBeSchema(expected: Schema<*>?) {
this.properties[key]!! shouldBeSchema expected.properties[key]
}
}
this.additionalProperties shouldBe expected.additionalProperties
assertNullSafe(this.additionalProperties, expected.additionalProperties) {
(this.additionalProperties as Schema<Any>) shouldBeSchema (expected.additionalProperties as Schema<Any>)
}
this.description shouldBe expected.description
this.`$ref` shouldBe expected.`$ref`
this.nullable shouldBe expected.nullable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.smiley4.ktorswaggerui.tests

import io.github.smiley4.ktorswaggerui.specbuilder.ApiSpecBuilder
import io.github.smiley4.ktorswaggerui.specbuilder.OApiComponentsBuilder
import io.github.smiley4.ktorswaggerui.specbuilder.OApiContentBuilder
import io.github.smiley4.ktorswaggerui.specbuilder.OApiExampleBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.smiley4.ktorswaggerui.tests
import com.fasterxml.jackson.annotation.JsonSubTypes
import com.fasterxml.jackson.annotation.JsonTypeInfo
import com.fasterxml.jackson.core.type.TypeReference
import com.github.victools.jsonschema.generator.SchemaGenerator
import io.github.smiley4.ktorswaggerui.SwaggerUIPluginConfig
import io.github.smiley4.ktorswaggerui.specbuilder.ComponentsContext
import io.kotest.core.spec.style.StringSpec
Expand All @@ -17,6 +18,27 @@ class JsonSchemaGenerationTests : StringSpec({
}
}

"generate schema for maps" {
getOApiSchemaBuilder().build(DataClassWithMaps::class.java, ComponentsContext.NOOP, SwaggerUIPluginConfig()) shouldBeSchema {
type = "object"
properties = mapOf(
"mapStringValues" to Schema<Any>().apply {
type = "object"
additionalProperties = Schema<Any>().apply {
type = "string"
}
},
"mapLongValues" to Schema<Any>().apply {
type = "object"
additionalProperties = Schema<Any>().apply {
type = "integer"
format = "int64"
}
},
)
}
}

"generate schema for a list of simple classes" {
getOApiSchemaBuilder().build(Array<SimpleDataClass>::class.java, ComponentsContext.NOOP, SwaggerUIPluginConfig()) shouldBeSchema {
type = "array"
Expand All @@ -28,6 +50,7 @@ class JsonSchemaGenerationTests : StringSpec({
},
"value" to Schema<Any>().apply {
type = "number"
format = "float"
}
)
}
Expand All @@ -43,6 +66,7 @@ class JsonSchemaGenerationTests : StringSpec({
},
"value" to Schema<Any>().apply {
type = "number"
format = "float"
}
)
}
Expand All @@ -54,11 +78,13 @@ class JsonSchemaGenerationTests : StringSpec({
properties = mapOf(
"primitiveValue" to Schema<Any>().apply {
type = "integer"
format = "int32"
},
"primitiveList" to Schema<Any>().apply {
type = "array"
items = Schema<Any>().apply {
type = "integer"
format = "int32"
}
},
"nestedClass" to Schema<Any>().apply {
Expand All @@ -69,6 +95,7 @@ class JsonSchemaGenerationTests : StringSpec({
},
"value" to Schema<Any>().apply {
type = "number"
format = "float"
}
)
},
Expand All @@ -82,6 +109,7 @@ class JsonSchemaGenerationTests : StringSpec({
},
"value" to Schema<Any>().apply {
type = "number"
format = "float"
}
)
}
Expand All @@ -99,6 +127,7 @@ class JsonSchemaGenerationTests : StringSpec({
},
"subFieldA" to Schema<Any>().apply {
type = "integer"
format = "int32"
},
"_type" to Schema<Any>().apply {
setConst("io.github.smiley4.ktorswaggerui.tests.JsonSchemaGenerationTests\$Companion\$SubClassA")
Expand All @@ -119,6 +148,7 @@ class JsonSchemaGenerationTests : StringSpec({
},
"subFieldA" to Schema<Any>().apply {
type = "integer"
format = "int32"
},
"_type" to Schema<Any>().apply {
setConst("io.github.smiley4.ktorswaggerui.tests.JsonSchemaGenerationTests\$Companion\$SubClassA")
Expand Down Expand Up @@ -187,6 +217,7 @@ class JsonSchemaGenerationTests : StringSpec({
},
"value" to Schema<Any>().apply {
type = "number"
format = "float"
}
)
},
Expand All @@ -200,6 +231,7 @@ class JsonSchemaGenerationTests : StringSpec({
},
"value" to Schema<Any>().apply {
type = "number"
format = "float"
}
)
}
Expand All @@ -222,6 +254,11 @@ class JsonSchemaGenerationTests : StringSpec({
val value: Float
)

data class DataClassWithMaps(
val mapStringValues: Map<String, String>,
val mapLongValues: Map<String, Long>
)

data class AnotherDataClass(
val primitiveValue: Int,
val primitiveList: List<Int>,
Expand Down

0 comments on commit 77b6b3f

Please sign in to comment.