我使用的是OpenAPI 3.0.1和swagger-codegen-maven-plugin 3.0.27(很高兴调整这些版本)。我定义了以下内容用于上传文件(以及其他数据)…
/myobject/:
post:
tags:
- my-objects
operationId: add
requestBody:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/MyDTO'
required: true
...
schemas:
MyDTO:
type: object
properties:
...
dataFile:
type: string
format: binary
我想将上传的文件类型限制为mime类型为"application/vnd.ms-excel"one_answers"文本/csv"。我如何用OpenAPI规范/Swagger CodeGen做到这一点?
您可以在规范中定义encoding
部分,定义期望的mime类型,如
...
encoding: # The same level as schema
dataFile: # Property name
contentType: application/vnd.ms-excel, text/csv
...
参考https://swagger.io/docs/specification/describing-request-body/multipart-requests/
我希望这会使无效数据类型的请求失败:
/myobject/:
post:
tags:
- my-objects
operationId: add
requestBody:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/MyDTO'
encoding:
dataFile:
contentType: application/vnd.ms-excel, text/csv
required: true