OpenAPI如何避免任何显示可选响应的空的null
我具有这样的OpenAPI响应模式,
ProfileMetrics:
description: List of metrics available in Field Profile
type: object
properties:
fieldProfileMetrics:
type: array
items:
type: object
summaryProfileMetrics:
type: array
items:
type: object
anyOf:
- required: [ fieldProfileMetrics ]
- required: [ summaryProfileMetrics ]
例如,我不希望在响应中进行现场生化的响应。
{
"fieldProfileMetrics": null,
"summaryProfileMetrics": [
{
"name": "count_events",
"description": "Number of rows/events in the dataset for particular timeframe",
"subMetrics": []
}]
}
因此,响应可以是{fieldProfileMetric:someValue}
,{summaryProfileMetric:somevalue}
{fieldprofilemetric:somevalue:somevalue,sommaryProfilemetric:somevalue}
服务类似于此类,
@Service
class ProfileMetricsService : ProfileMetricsApiService {
override fun v1MetricsGet(profile: ProfileType?): ProfileMetrics {
return when (profile) {
ProfileType.field -> convertToProfileMetrics(profile, getFieldProfileMetrics())
ProfileType.summary -> convertToProfileMetrics(
profile, getSummaryProfileMetrics()
)
else -> convertToProfileMetrics(
getFieldProfileMetrics(),
getSummaryProfileMetrics()
)
}
}
private fun getFieldProfileMetrics(): MutableList<Map<String, Serializable>> {
val fieldProfileList = mutableListOf<Map<String, Serializable>>()
FieldProfileMetrics.values().forEach { it ->
fieldProfileList.add(
mapOf(
Pair("name", it.value),
Pair(FieldProfileMetrics::description.name, it.description),
Pair(FieldProfileMetrics::subMetrics.name, it.subMetrics.map { it.value }.toTypedArray())
)
)
}
return fieldProfileList
}
private fun getSummaryProfileMetrics(): MutableList<Map<String, Serializable>> {
val summaryProfileList = mutableListOf<Map<String, Serializable>>()
SummaryProfileMetrics.values().forEach { profileMetric ->
summaryProfileList.add(
mapOf(
Pair("name", profileMetric.value),
Pair(SummaryProfileMetrics::description.name, profileMetric.description),
Pair(SummaryProfileMetrics::subMetrics.name, profileMetric.subMetrics.map { it.value }.toTypedArray())
)
)
}
return summaryProfileList
}
private fun convertToProfileMetrics(
profile: ProfileType,
metricList: MutableList<Map<String, Serializable>>
): ProfileMetrics {
return if (profile == ProfileType.field)
ProfileMetrics(fieldProfileMetrics = metricList)
else
ProfileMetrics(summaryProfileMetrics = metricList)
}
private fun convertToProfileMetrics(
fieldMetricList: MutableList<Map<String, Serializable>>,
summaryMetricList: MutableList<Map<String, Serializable>>
): ProfileMetrics {
return ProfileMetrics(
fieldProfileMetrics = fieldMetricList,
summaryProfileMetrics = summaryMetricList
)
}
companion object {
val logger = LoggerFactory.getLogger(ProfileMetricsService::class.java)
}
}
生成源后生成的接口
interface ProfileMetricsApiService {
/**
* GET /v1/metrics : Get list of field and summary profile metrics
* Retrieves list of metrics for field and summary profile
*
* @param profile Profile Type (optional)
* @return OK (status code 200)
* or Bad Request. (status code 400)
* or The specified resource was not found. (status code 404)
* or Unexpected error. (status code 200)
* @see ProfileMetricsApi#v1MetricsGet
*/
fun v1MetricsGet(profile: ProfileType?): ProfileMetrics
}
在编译生成之后给出了此探讨,
/**
* List of metrics available in Field Profile
* @param fieldProfileMetrics
* @param summaryProfileMetrics
*/
data class ProfileMetrics(
@field:Valid
@Schema(example = "null", description = "")
@field:JsonProperty("fieldProfileMetrics") val fieldProfileMetrics: kotlin.collections.List<kotlin.Any>? = null,
@field:Valid
@Schema(example = "null", description = "")
@field:JsonProperty("summaryProfileMetrics") val summaryProfileMetrics: kotlin.collections.List<kotlin.Any>? = null
) {
}
任何帮助都将不胜感激。
I have openapi response schema like this
ProfileMetrics:
description: List of metrics available in Field Profile
type: object
properties:
fieldProfileMetrics:
type: array
items:
type: object
summaryProfileMetrics:
type: array
items:
type: object
anyOf:
- required: [ fieldProfileMetrics ]
- required: [ summaryProfileMetrics ]
Which results in a Response for example where I don't want fieldProfileMetrics in the Response.
{
"fieldProfileMetrics": null,
"summaryProfileMetrics": [
{
"name": "count_events",
"description": "Number of rows/events in the dataset for particular timeframe",
"subMetrics": []
}]
}
So the response can be either {fieldProfileMetric : SomeValue}
, {summaryProfileMetric : SomeValue}
or {fieldProfileMetric : SomeValue, summaryProfileMetric : SomeValue}
The Service Class is like this
@Service
class ProfileMetricsService : ProfileMetricsApiService {
override fun v1MetricsGet(profile: ProfileType?): ProfileMetrics {
return when (profile) {
ProfileType.field -> convertToProfileMetrics(profile, getFieldProfileMetrics())
ProfileType.summary -> convertToProfileMetrics(
profile, getSummaryProfileMetrics()
)
else -> convertToProfileMetrics(
getFieldProfileMetrics(),
getSummaryProfileMetrics()
)
}
}
private fun getFieldProfileMetrics(): MutableList<Map<String, Serializable>> {
val fieldProfileList = mutableListOf<Map<String, Serializable>>()
FieldProfileMetrics.values().forEach { it ->
fieldProfileList.add(
mapOf(
Pair("name", it.value),
Pair(FieldProfileMetrics::description.name, it.description),
Pair(FieldProfileMetrics::subMetrics.name, it.subMetrics.map { it.value }.toTypedArray())
)
)
}
return fieldProfileList
}
private fun getSummaryProfileMetrics(): MutableList<Map<String, Serializable>> {
val summaryProfileList = mutableListOf<Map<String, Serializable>>()
SummaryProfileMetrics.values().forEach { profileMetric ->
summaryProfileList.add(
mapOf(
Pair("name", profileMetric.value),
Pair(SummaryProfileMetrics::description.name, profileMetric.description),
Pair(SummaryProfileMetrics::subMetrics.name, profileMetric.subMetrics.map { it.value }.toTypedArray())
)
)
}
return summaryProfileList
}
private fun convertToProfileMetrics(
profile: ProfileType,
metricList: MutableList<Map<String, Serializable>>
): ProfileMetrics {
return if (profile == ProfileType.field)
ProfileMetrics(fieldProfileMetrics = metricList)
else
ProfileMetrics(summaryProfileMetrics = metricList)
}
private fun convertToProfileMetrics(
fieldMetricList: MutableList<Map<String, Serializable>>,
summaryMetricList: MutableList<Map<String, Serializable>>
): ProfileMetrics {
return ProfileMetrics(
fieldProfileMetrics = fieldMetricList,
summaryProfileMetrics = summaryMetricList
)
}
companion object {
val logger = LoggerFactory.getLogger(ProfileMetricsService::class.java)
}
}
The interface generated after generate-sources gives this
interface ProfileMetricsApiService {
/**
* GET /v1/metrics : Get list of field and summary profile metrics
* Retrieves list of metrics for field and summary profile
*
* @param profile Profile Type (optional)
* @return OK (status code 200)
* or Bad Request. (status code 400)
* or The specified resource was not found. (status code 404)
* or Unexpected error. (status code 200)
* @see ProfileMetricsApi#v1MetricsGet
*/
fun v1MetricsGet(profile: ProfileType?): ProfileMetrics
}
ProfileMetrics after compilation generates this
/**
* List of metrics available in Field Profile
* @param fieldProfileMetrics
* @param summaryProfileMetrics
*/
data class ProfileMetrics(
@field:Valid
@Schema(example = "null", description = "")
@field:JsonProperty("fieldProfileMetrics") val fieldProfileMetrics: kotlin.collections.List<kotlin.Any>? = null,
@field:Valid
@Schema(example = "null", description = "")
@field:JsonProperty("summaryProfileMetrics") val summaryProfileMetrics: kotlin.collections.List<kotlin.Any>? = null
) {
}
This is how the response looks like
Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论