You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过OpenAPI 3.0 Java注解配置Map键值的大小约束?

问题:为OAS 3.0文档中的Map<String, String>添加键值大小约束

我需要让生成的OAS 3.0文档包含Map<String, String>类型字段的键和值的大小约束。以下是相关Kotlin代码示例:

@RouterOperation(
    operation = Operation(operationId = ":POST/foo/bar",
        requestBody = RequestBody(
            content = [Content(
                mediaType = MediaType.APPLICATION_JSON_VALUE,
                schema = Schema(implementation = MyRequestData::class)
            )]
        )
    )
)
suspend fun myHandler(@Valid request: Request, @Valid data: MyRequestData) {}

//
// and
//

@Serializable
data class MyRequestData(
    @field:Valid
    @field:Schema(description = "A map")
    val someKeysToValues: Map<
            @Size(max = 100) String,
            @Size(max = 50) String>? = null,

    @field:Valid
    @field:ArraySchema(schema = Schema(description = "A list item"))
    val someOtherKeys: List<@Size(max = 100) String>? = null,
)

在将Map的键值约束信息添加到生成的Swagger JSON时遇到了阻碍。根据文档,应该使用additionalProperties,但@Schema注解仅支持将其设为true或false,具体问题如下:

  • 泛型List可以用@ArraySchema注解,但Map没有对应的类似注解;
  • 约束注解无法像@ArraySchema那样被自动识别并填充到Schema中;
  • 尝试设置additionalPropertiesSchema并添加@Schema或@SchemaProperty注解,没有效果;
  • 未找到@AdditonalSchemaProperty或类似功能的注解。

当前生成的UI输出如下:

MyRequestData{
  someKeysToValues {
    description:   A map
    < * >:    string
              A map

  }
  someOtherKeys  [
    string
    maxLength: 100
    minLength: 0
    A list item
  ]
}

我无法为Map的内部Schema添加max-length约束信息,寻求相关解决建议或示例。

注:使用的依赖为org.springdoc:springdoc-openapi-starter-webflux-ui,注解功能来自io.swagger.core.v3:swagger-annotations-jarakata(2.2.15)

内容的提问来源于stack exchange,提问作者mrusinak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 12:00:10