znsio / specmatic

Turn your contracts into executable specifications. Contract Driven Development - Collaboratively Design & Independently Deploy MicroServices & MicroFrontends.
https://specmatic.io
MIT License
258 stars 51 forks source link

Issue when parsing Iso String Date. #1182

Closed barath121 closed 1 month ago

barath121 commented 1 month ago

Description When parsing ISO date specmatic is throwing error.

Steps to reproduce

  1. Add a string field to any object.
  2. Add format as date-time to the key.
  3. The response/request value should match this format - 2023-10-31T08:58:25.945Z which is valid as per swagger format - Regex to validate, Swagger Docs
  4. Check the Error - Error: Error while parsing the date time as per RFC 3339: 1

Expected behaviour Should parse this Date-Time when .sssZ is present.

System Information:

harikrishnan83 commented 1 month ago

hello @barath121 , thanks for reporting the issue.

I tried reproducing the problem with below API spec. I am not able to reproduce the issue.

openapi: 3.0.0
info:
  title: Hello API
  version: 1.0.0
paths:
  /hello:
    post:
      summary: Returns a greeting with the current date-time
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                requestDateTime:
                  type: string
                  format: date-time
              required:
                - requestDateTime
            examples:
              SUCCESS:
                value:
                  requestDateTime: '2023-10-31T08:58:25.945Z'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  responseDateTime:
                    type: string
                    format: date-time
              examples:
                SUCCESS:
                  value:
                    responseDateTime: '2023-10-31T08:58:25.945Z'

As you can see I am using the date value that you have shared. And Specmatic is able to both run a stub server as well as contract tests based on this OpenAPI spec.

% specmatic test test.yaml --port=9000
API Specification Summary: test.yaml
  OpenAPI Version: 3.0.0
  API Paths: 1, API Operations: 1

Endpoints API not found, cannot calculate actual coverage

--------------------
  Request to http://localhost:9000 at 2024-7-15 2:36:56.710
    POST /hello
    Accept-Charset: UTF-8
    Accept: */*
    Content-Type: application/json

    {
        "dateTime": "2023-10-31T08:58:25.945Z"
    }

  Response at 2024-7-15 2:36:56.711
    200 OK
    Vary: Origin
    X-Specmatic-Result: success
    Content-Length: 54
    Content-Type: application/json
    Connection: keep-alive

    {
        "responseDateTime": "2023-10-31T08:58:25.945Z"
    }

 Scenario: POST /hello -> 200 | EX:SUCCESS has SUCCEEDED

Can you please let me know if I am missing anything here?

barath121 commented 1 month ago

Got it seems like issue with my setup when an invalid test case is passed it throws parsing error instead of Validation Error.