我在 API 响应中遇到日期时间格式验证问题,我正在寻求解决该问题的指导。
问题:
我有一个 API,它以“YYYY-MM-DDTHH:MM:SS”格式返回日期时间值,该格式遵循“日期时间”格式标准。但是,当我向 API 发送请求时,我始终收到以下错误响应:
{
"type": "about:blank",
"title": "Bad Request",
"detail": "'2021-01-30T08:30:00' is not a 'date-time'\n\nFailed validating 'format' in schema:\n {'format': 'date-time', 'type': 'string'}\n\nOn instance:\n '2021-01-30T08:30:00'",
"status": 400
}
和我的 yml 文件:
openapi: 3.0.0
info:
version: "1.0.0"
title: Health and Fitness Tracking Service
description: API for logging health and fitness activities
paths:
/getNutrition:
get:
summary: Get nutrition.
operationId: appstorage.get_nutrition
description: Enables users to receive their daily food intake between time periods.
parameters:
- in: query
name: start_timestamp
required: true
schema:
type: string
format: date-time
- in: query
name: end_timestamp
required: true
schema:
type: string
format: date-time
responses:
'200':
description: Successfully returned nutrition data.
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/NutritionDetails'
'400':
description: Failed to return nutrition data.
content:
application/json:
schema:
type: object
properties:
message:
type: string
/getActivity:
get:
summary: Get activities.
operationId: appstorage.get_activity
description: Enables users to receive their daily activity intake between time periods.
parameters:
- in: query
name: start_timestamp
required: true
schema:
type: string
format: date-time
- in: query
name: end_timestamp
required: true
schema:
type: string
format: date-time
responses:
'200':
description: Successfully returned activity data.
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/ActivityDetails'
'400':
description: Failed to return activity data.
content:
application/json:
schema:
type: object
properties:
message:
type: string
检查日期时间字符串以确保它们符合“日期-时间”格式标准。他们是这样。 检查验证器设置以确保其正确识别“日期时间”格式。 确保日期时间字符串周围没有无关的字符或空格。
date-time
格式由RFC 3339定义,并且需要在末尾添加时区偏移量 - ±HH:MM
或Z
/ z
(零偏移)。
有效值示例:
2021-01-30T08:30:00Z
2021-01-30T08:30:00+05:30
OpenAPI 没有为没有时区偏移的 YYYY-MM-DDTHH:MM:SS
值定义
format。这些值被视为字符串。