`minLength: 1` 是否暗示 OpenAPI 规范中需要?

Ale*_*nov 13 swagger openapi

考虑这个 OAS3 规范 ( testMinMax3.yaml):

openapi: 3.0.1
info:
  title: OpenAPI Test
  description: Test
  license:
    name: Apache-2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
  version: 1.0.0
servers:
- url: http://localhost:9999/v2
paths:
  /ping:
    post:
      summary: test
      description: test it
      operationId: pingOp
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SomeObj'
        required: true
      responses:
        200:
          description: OK
          content: {}
components:
  schemas:
    SomeObj:
      type: string
      minLength: 1
      maxLength: 3
Run Code Online (Sandbox Code Playgroud)

非空(又名minLength: 1)是否意味着required即使未根据 openapi 规范 (1) 设置字段?或者仅当用户提供该字段的值时才适用(2)?

Hel*_*len 12

不。

minLengthrequired是单独的约束。minLength意味着如果提供了字符串值,则其长度必须等于minLength或大于。

  • 为了补充这个答案,请考虑以下场景:模式如何定义不能提供属性*或*仅当其长度至少为“n”字符时才有效? (3认同)