Skip to content

serDes does not work in additionalProperties objects #821

Closed
@leonardo-speranzon

Description

@leonardo-speranzon

Describe the bug
The serialization of date-time not work if the field is in a additionalProperties object

To Reproduce

The openapi definition

  /test:
    get: 
      responses:
        '200':
          description: foo
          content:
            application/json:
              schema:
                type: object
                properties:
                  good_date:
                    type: string
                    format: date-time
                  other_info:
                    type: object
                    additionalProperties:
                      type: object
                      properties:
                        bad_date:
                          type: string
                          format: date-time

Express server implementation

app.use(
    OpenApiValidator.middleware({
        apiSpec: './openapi.yaml',
        validateRequests: true, // (default)
        validateResponses: true, // false by default
        serDes: [
            OpenApiValidator.serdes.date,
            OpenApiValidator.serdes.dateTime,
        ]  // the default ones
    }),
)

app.get('/test', (req:Request,res:Response,next:NextFunction)=>{
    return res.json({
        good_date: new Date(),
        other_info: {
            something:{
                bad_date: new Date()
            }
        }
    })
})

Actual behavior
The serializer does not convert the bad_date from date to string and so the validator produce an error

  "message": "/response/other_info/something/bad_date must be string",
    "errors": [
        {
            "path": "/response/other_info/something/bad_date",
            "message": "must be string",
            "errorCode": "type.openapi.validation"
        }
    ]

Expected behavior
The response should be validated succesfully

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions