ePages-de / restdocs-api-spec

Adds API specification support to Spring REST Docs
MIT License
392 stars 102 forks source link

Map responseBody examples to responseBody model. #91

Open abcfy2 opened 5 years ago

abcfy2 commented 5 years ago

Maybe related to #58

In my case, The generated openapi3.yaml looks like this:

openapi: 3.0.1
info:
  title: Grails-rest-seed API
  description: Grails-rest-seed API文档
  version: "1.0"
servers:
- url: http://localhost:8080
tags: []
paths:
  /api/getUploadAuthority:
    get:
      tags:
      - operation
      summary: OSS接口
      description: 阿里云OSS获取上传权限
      operationId: AliyunOSS
      parameters:
      - name: Authorization
        in: header
        description: JWT
        required: true
        schema:
          type: string
      responses:
        200:
          description: "200"
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/api-getUploadAuthority2026114897'
              examples:
                AliyunOSS:
                  value: "{\r\n  \"accessKeyId\" : \"mock\",\r\n  \"policy\" : \"\
                    eyJleHBpcmF0aW9uIjoiMjAxOS0wMy0yNlQxNjo1NToyOS41MDNaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCIxMzUwMDAwMDAwMSJdXX0=\"\
                    ,\r\n  \"signature\" : \"1slIn7u/Ruoptc+Hn6xBDOjNLWo=\",\r\n \
                    \ \"dir\" : \"13500000001\",\r\n  \"host\" : \"https://mock.oss-cn-hangzhou.aliyuncs.com\"\
                    ,\r\n  \"expire\" : 1553619329503,\r\n  \"cdnUrl\" : \"mock\"\r\
                    \n}"
components:
  schemas:
    api-getUploadAuthority2026114897:
      type: object
      required:
        - accessKeyId
        - signature
        - expire
        - host
        - dir
        - policy
      properties:
        accessKeyId:
          type: string
          description: OSS的access key id
        signature:
          type: string
          description: OSS认证成功后的签名
        cdnUrl:
          type: string
          description: 用于外部访问的CDN URL(可空)
        expire:
          type: number
          description: 授权过期时间
        host:
          type: string
          description: OSS访问主机
        dir:
          type: string
          description: 有权限上传的目录
        policy:
          type: string
          description: OSS的权限矩阵

But I want to see like this:

openapi: 3.0.1
info:
  title: Grails-rest-seed API
  description: Grails-rest-seed API文档
  version: "1.0"
servers:
- url: http://localhost:8080
tags: []
paths:
  /api/getUploadAuthority:
    get:
      tags:
      - operation
      summary: OSS接口
      description: 阿里云OSS获取上传权限
      operationId: AliyunOSS
      parameters:
      - name: Authorization
        in: header
        description: JWT
        required: true
        schema:
          type: string
      responses:
        200:
          description: "200"
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/api-getUploadAuthority2026114897'
components:
  schemas:
    api-getUploadAuthority2026114897:
      type: object
      required:
        - accessKeyId
        - signature
        - expire
        - host
        - dir
        - policy
      properties:
        accessKeyId:
          type: string
          description: OSS的access key id
          example: mock
        signature:
          type: string
          description: OSS认证成功后的签名
          example: 1slIn7u/Ruoptc+Hn6xBDOjNLWo=
        cdnUrl:
          type: string
          description: 用于外部访问的CDN URL(可空)
          example: mock
        expire:
          type: number
          description: 授权过期时间
          example: 1553619329503
        host:
          type: string
          description: OSS访问主机
          example: https://mock.oss-cn-hangzhou.aliyuncs.com
        dir:
          type: string
          description: 有权限上传的目录
          example: 1553619329503
        policy:
          type: string
          description: OSS的权限矩阵
          example: "eyJleHBpcmF0aW9uIjoiMjAxOS0wMy0yNlQxNjo1NToyOS41MDNaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCIxMzUwMDAwMDAwMSJdXX0="

Map the response examples to schemas.

mduesterhoeft commented 5 years ago

Of course we could also map the example request/response also to example values for each field in the schema. I find this a little bit redundant. It is also not adding too much value.

So I would rather shy away from adding this myself.

abcfy2 commented 5 years ago

Thanks @mduesterhoeft . Or maybe we can render example outside schema is better. See: https://swagger.io/docs/specification/adding-examples/

And this also works:

openapi: 3.0.1
info:
  title: Grails-rest-seed API
  description: Grails-rest-seed API文档
  version: "1.0"
servers:
- url: http://localhost:8080
tags: []
paths:
  /api/getUploadAuthority:
    get:
      tags:
      - operation
      summary: OSS接口
      description: 阿里云OSS获取上传权限
      operationId: AliyunOSS
      parameters:
      - name: Authorization
        in: header
        description: JWT
        required: true
        schema:
          type: string
      responses:
        200:
          description: "200"
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/api-getUploadAuthority2026114897'
              example:
                accessKeyId: 'mock'
                signature: '1slIn7u/Ruoptc+Hn6xBDOjNLWo='
                cdnUrl: 'mock'
                expire: 1553619329503
                host: 'https://mock.oss-cn-hangzhou.aliyuncs.com'
                dir: '1553619329503'
                policy: 'eyJleHBpcmF0aW9uIjoiMjAxOS0wMy0yNlQxNjo1NToyOS41MDNaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCIxMzUwMDAwMDAwMSJdXX0='

components:
  schemas:
    api-getUploadAuthority2026114897:
      type: object
      required:
        - accessKeyId
        - signature
        - expire
        - host
        - dir
        - policy
      properties:
        accessKeyId:
          type: string
          description: OSS的access key id
        signature:
          type: string
          description: OSS认证成功后的签名
        cdnUrl:
          type: string
          description: 用于外部访问的CDN URL(可空)
        expire:
          type: number
          description: 授权过期时间
        host:
          type: string
          description: OSS访问主机
          example: https://mock.oss-cn-hangzhou.aliyuncs.com
        dir:
          type: string
          description: 有权限上传的目录
        policy:
          type: string
          description: OSS的权限矩阵