首页 > 解决方案 > 在 CUE 中定义数组并导出到 OpenAPI

问题描述

对于以下简单的 CUE 代码,它定义了一个match包含两个团队名称(作为两个字符串的数组)和比赛得分(作为两个整数的数组)的对象:

#match: {
    id: int
    teams: 2 * [string]
    score: 2 * [int]
}

当我运行cue export --out openapi test.cue(使用 CUE v0.4.0)时,我收到错误消息:

components.schemas.match.properties.score.default.0: incomplete value int
components.schemas.match.properties.score.default.1: incomplete value int
components.schemas.match.properties.teams.default.0: incomplete value string
components.schemas.match.properties.teams.default.1: incomplete value string

如果我更改stringstring | *null,它可以工作 - 但是我真的不明白为什么数组的元素需要具有默认值才能导出为 OpenAPI(特别是因为对对象属性没有这样的要求)?我在这里错过了什么吗?

标签: openapicuelang

解决方案


通常,这看起来与 OpenAPI 不一致且值不完整。解决方法是指定默认元素。

import "list"

#s: string | *null
#match: {
    id: int
    teams: [#s, #s]
    score: list.Repeat([int | *null], 2)
}

请注意,我编写的方式teamscore此处导致的架构略有不同但等效。

另一方面,数学运算符已被弃用。您应该使用list.Repeat代替该部分。如果需要,您还可以列出元素。

有关更多详细信息,请参阅https://github.com/cue-lang/cue/discussions/1115


推荐阅读