Skip to content

Commit

Permalink
fix: better management of numbers in OpenApi3Generator (ePages-de#202)
Browse files Browse the repository at this point in the history
Co-authored-by: Jordan GAZEAU <[email protected]>
  • Loading branch information
jgazeau and Jordan GAZEAU authored Apr 23, 2022
1 parent 02c7a41 commit 31bb1e1
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -456,21 +456,15 @@ object OpenApi3Generator {
.forEach { this.addEnumItem(it) }
}
SimpleType.NUMBER.name.toLowerCase() -> NumberSchema().apply {
this._default(parameterDescriptor.defaultValue?.let { it as BigDecimal })
this._default(parameterDescriptor.defaultValue?.asBigDecimal())
parameterDescriptor.attributes.enumValues
.map {
when (it) {
is Int -> it.toBigDecimal()
is Double -> it.toBigDecimal()
else -> it as BigDecimal
}
}
.map { it.asBigDecimal() }
.forEach { this.addEnumItem(it) }
}
SimpleType.INTEGER.name.toLowerCase() -> IntegerSchema().apply {
this._default(parameterDescriptor.defaultValue?.let { it as Int })
this._default(parameterDescriptor.defaultValue?.asInt())
parameterDescriptor.attributes.enumValues
.map { it as Int }
.map { it.asInt() }
.forEach { this.addEnumItem(it) }
}
else -> throw IllegalArgumentException("Unknown type '${parameterDescriptor.type}'")
Expand All @@ -485,6 +479,24 @@ object OpenApi3Generator {
return if (this.isEmpty()) null else this
}

private fun Any.asInt(): Int {
return when (this) {
is Int -> this
is Long -> toInt()
else -> this as Int
}
}

private fun Any.asBigDecimal(): BigDecimal {
return when (this) {
is Int -> toBigDecimal()
is Long -> toBigDecimal()
is Double -> toBigDecimal()
is Float -> toBigDecimal()
else -> this as BigDecimal
}
}

private data class RequestModelWithOperationId(
val operationId: String,
val request: RequestModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,44 @@ class OpenApi3GeneratorTest {
(it["schema"] as LinkedHashMap<*, *>)["type"] == "number" &&
(it["schema"] as LinkedHashMap<*, *>)["default"] == 1
}
then(params).anyMatch {
it["name"] == "intNumberParameter" &&
it["description"] == "a int number parameter" &&
(it["schema"] as LinkedHashMap<*, *>)["type"] == "number" &&
(it["schema"] as LinkedHashMap<*, *>)["default"] == 1
}
then(params).anyMatch {
it["name"] == "longNumberParameter" &&
it["description"] == "a long number parameter" &&
(it["schema"] as LinkedHashMap<*, *>)["type"] == "number" &&
(it["schema"] as LinkedHashMap<*, *>)["default"] == 1
}
then(params).anyMatch {
it["name"] == "doubleNumberParameter" &&
it["description"] == "a double number parameter" &&
(it["schema"] as LinkedHashMap<*, *>)["type"] == "number" &&
(it["schema"] as LinkedHashMap<*, *>)["default"] == 1.0
}
then(params).anyMatch {
it["name"] == "floatNumberParameter" &&
it["description"] == "a float number parameter" &&
(it["schema"] as LinkedHashMap<*, *>)["type"] == "number" &&
(it["schema"] as LinkedHashMap<*, *>)["default"] == 1.0
}
then(params).anyMatch {
it["name"] == "integerParameter" &&
it["description"] == "a integer parameter" &&
(it["schema"] as LinkedHashMap<*, *>)["type"] == "integer" &&
(it["schema"] as LinkedHashMap<*, *>)["format"] == "int32" &&
(it["schema"] as LinkedHashMap<*, *>)["default"] == 2
}
then(params).anyMatch {
it["name"] == "longIntegerParameter" &&
it["description"] == "a long integer parameter" &&
(it["schema"] as LinkedHashMap<*, *>)["type"] == "integer" &&
(it["schema"] as LinkedHashMap<*, *>)["format"] == "int32" &&
(it["schema"] as LinkedHashMap<*, *>)["default"] == 2
}
then(params).anyMatch {
it["name"] == "X-SOME-BOOLEAN" &&
it["description"] == "a header boolean parameter" &&
Expand All @@ -312,14 +343,45 @@ class OpenApi3GeneratorTest {
(it["schema"] as LinkedHashMap<*, *>)["type"] == "number" &&
(it["schema"] as LinkedHashMap<*, *>)["default"] == 1
}
then(params).anyMatch {
it["name"] == "X-SOME-INT-NUMBER" &&
it["description"] == "a header int number parameter" &&
(it["schema"] as LinkedHashMap<*, *>)["type"] == "number" &&
(it["schema"] as LinkedHashMap<*, *>)["default"] == 1
}
then(params).anyMatch {
it["name"] == "X-SOME-LONG-NUMBER" &&
it["description"] == "a header long number parameter" &&
(it["schema"] as LinkedHashMap<*, *>)["type"] == "number" &&
(it["schema"] as LinkedHashMap<*, *>)["default"] == 1
}
then(params).anyMatch {
it["name"] == "X-SOME-DOUBLE-NUMBER" &&
it["description"] == "a header double number parameter" &&
(it["schema"] as LinkedHashMap<*, *>)["type"] == "number" &&
(it["schema"] as LinkedHashMap<*, *>)["default"] == 1.0
}
then(params).anyMatch {
it["name"] == "X-SOME-FLOAT-NUMBER" &&
it["description"] == "a header float number parameter" &&
(it["schema"] as LinkedHashMap<*, *>)["type"] == "number" &&
(it["schema"] as LinkedHashMap<*, *>)["default"] == 1.0
}
then(params).anyMatch {
it["name"] == "X-SOME-INTEGER" &&
it["description"] == "a header integer parameter" &&
(it["schema"] as LinkedHashMap<*, *>)["type"] == "integer" &&
(it["schema"] as LinkedHashMap<*, *>)["format"] == "int32" &&
(it["schema"] as LinkedHashMap<*, *>)["default"] == 2
}
then(params).hasSize(9)
then(params).anyMatch {
it["name"] == "X-SOME-LONG-INTEGER" &&
it["description"] == "a header long integer parameter" &&
(it["schema"] as LinkedHashMap<*, *>)["type"] == "integer" &&
(it["schema"] as LinkedHashMap<*, *>)["format"] == "int32" &&
(it["schema"] as LinkedHashMap<*, *>)["default"] == 2
}
then(params).hasSize(19)

thenOpenApiSpecIsValid()
}
Expand Down Expand Up @@ -1205,12 +1267,47 @@ class OpenApi3GeneratorTest {
optional = true,
defaultValue = 1.toBigDecimal()
),
HeaderDescriptor(
name = "X-SOME-INT-NUMBER",
description = "a header int number parameter",
type = "NUMBER",
optional = true,
defaultValue = 1
),
HeaderDescriptor(
name = "X-SOME-LONG-NUMBER",
description = "a header long number parameter",
type = "NUMBER",
optional = true,
defaultValue = 1L
),
HeaderDescriptor(
name = "X-SOME-DOUBLE-NUMBER",
description = "a header double number parameter",
type = "NUMBER",
optional = true,
defaultValue = 1.0
),
HeaderDescriptor(
name = "X-SOME-FLOAT-NUMBER",
description = "a header float number parameter",
type = "NUMBER",
optional = true,
defaultValue = 1.toFloat()
),
HeaderDescriptor(
name = "X-SOME-INTEGER",
description = "a header integer parameter",
type = "INTEGER",
optional = true,
defaultValue = 2
),
HeaderDescriptor(
name = "X-SOME-LONG-INTEGER",
description = "a header long integer parameter",
type = "INTEGER",
optional = true,
defaultValue = 2L
)
),
requestParameters = listOf(
Expand Down Expand Up @@ -1238,13 +1335,53 @@ class OpenApi3GeneratorTest {
ignored = false,
defaultValue = 1.toBigDecimal()
),
ParameterDescriptor(
name = "intNumberParameter",
description = "a int number parameter",
type = "NUMBER",
optional = true,
ignored = false,
defaultValue = 1
),
ParameterDescriptor(
name = "longNumberParameter",
description = "a long number parameter",
type = "NUMBER",
optional = true,
ignored = false,
defaultValue = 1L
),
ParameterDescriptor(
name = "doubleNumberParameter",
description = "a double number parameter",
type = "NUMBER",
optional = true,
ignored = false,
defaultValue = 1.0
),
ParameterDescriptor(
name = "floatNumberParameter",
description = "a float number parameter",
type = "NUMBER",
optional = true,
ignored = false,
defaultValue = 1.toFloat()
),
ParameterDescriptor(
name = "integerParameter",
description = "a integer parameter",
type = "INTEGER",
optional = true,
ignored = false,
defaultValue = 2
),
ParameterDescriptor(
name = "longIntegerParameter",
description = "a long integer parameter",
type = "INTEGER",
optional = true,
ignored = false,
defaultValue = 2L
)
)
)
Expand Down

0 comments on commit 31bb1e1

Please sign in to comment.