0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

OpenAPIでList型を使う場合の@Schemaの記載例

0
Posted at

忘備録的に・・・

OpenAPIを使ってRESTでやり取りする場合、PUT、POSTはRequestBodyにJSONで送ってくるのでListで何重にもネストさせることができます。
Listを使った場合、RequestBody の data classの@Schemaアノテーションはどう書くべきなのか、整理してみました。

プリミティブ型(Int、Long、String、Boolean等)

@field:Valid
@field:JsonProperty("hogeList")
@field:ArraySchema(arraySchema = Schema(description = "ほげリスト", example = "[ \"0000000001\", \"0000000002\" ]"),
    schema = Schema(type = "string"), minItems = 1)
var hogeList: List<
        @NotEmpty(message = "省略できません")
        @Pattern(regexp = "^[0-9]{1,10}$", message = "1~10桁の数字で入力してください")
        String> = emptyList(),

kotlinのdata classの場合はフィールドはコンストラクタの引数でもあります。
フィールドに付けるアノテーションなのか、コンストラクタの引数なのか、区別するため、@の前にfield:を付ける必要があります。

  • @Schemaではなくて、@ArraySchemaを使います
  • アノテーションの引数、arraySchema にはそのListそのものの注釈を付けます
  • アノテーションの引数、schema にはそのListの中身の要素の注釈を付けます
  • Listの中身の要素のValidation chedkを付ける場合は上記のように、List< > の間に入れます。ここは@Valid要りません

data class、Enumの場合

@field:Valid
@field:Size(min = 1, message = "リストは1件以上指定してください")
@field:JsonProperty("hogeList")
@field:ArraySchema(arraySchema = Schema(description = "発注残入庫保存リスト"),
    schema = Schema(implementation = SavePendInstock::class), minItems = 1)
var hogeList: List<HogeDataClass> = emptyList()

・・・

data class HogeDataClass (
	・・・・
)

Listの中身の要素がdata classの場合はimplementation を使います。これはListじゃないベタの場合も同じです

swaggerで見てみるとListの中身の要素まで定義が見えます

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?