pb33f / wiretap

The world's coolest API Validation and compliance tool. Validate APIs against OpenAPI specifications and much more
https://pb33f.io/wiretap/
Other
125 stars 18 forks source link

handle schemas with different format types when mocking/returning responses #93

Closed TristanSpeakEasy closed 7 months ago

TristanSpeakEasy commented 10 months ago

I suspect this is likely to be a feature request more than a bug. But consider this spec:

openapi: 3.1.0
info:
  title: Test
  version: 0.1.0
paths:
  /anything/requestBodies/post/application/json/simple:
    post:
      operationId: requestBodyPostApplicationJsonSimple
      tags:
        - requestBodies
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/simpleObject"
        required: true
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                title: res
                type: object
                properties:
                  json:
                    $ref: "#/components/schemas/simpleObject"
                required:
                  - json
components:
  schemas:
    simpleObject:
      description: "A simple object that uses all our supported primitive types and enums and has optional properties."
      externalDocs:
        description: "A link to the external docs."
        url: "https://docs.speakeasyapi.dev"
      type: object
      properties:
        str:
          type: string
          description: "A string property."
          example: "test"
        bool:
          type: boolean
          description: "A boolean property."
          example: true
        int:
          type: integer
          description: "An integer property."
          example: 1
        int32:
          type: integer
          format: int32
          description: "An int32 property."
          example: 1
        num:
          type: number
          description: "A number property."
          example: 1.1
        float32:
          type: number
          format: float
          description: "A float32 property."
          example: 1.1
        enum:
          $ref: "#/components/schemas/enum"
        date:
          type: string
          format: date
          description: "A date property."
          example: "2020-01-01"
        dateTime:
          type: string
          format: date-time
          description: "A date-time property."
          example: "2020-01-01T00:00:00.000001Z"
        any:
          description: "An any property."
          example: "any"
        strOpt:
          type: string
          description: "An optional string property."
          example: "testOptional"
        boolOpt:
          type: boolean
          description: "An optional boolean property."
          example: true
        intOptNull:
          type: integer
          description: "An optional integer property will be null for tests."
        numOptNull:
          type: number
          description: "An optional number property will be null for tests."
        intEnum:
          type: integer
          description: "An integer enum property."
          enum:
            - 1
            - 2
            - 3
          example: 2
          x-speakeasy-enums:
            - First
            - Second
            - Third
        int32Enum:
          type: integer
          format: int32
          description: "An int32 enum property."
          enum:
            - 55
            - 69
            - 181
          example: 55
        bigint:
          type: integer
          format: bigint
          example: 8821239038968084
        bigintStr:
          type: string
          format: bigint
          example: "9223372036854775808"
        decimal:
          type: number
          format: decimal
          example: 3.141592653589793
        decimalStr:
          type: string
          format: decimal
          example: "3.14159265358979344719667586"
      required:
        - str
        - bool
        - int
        - int32
        - num
        - float32
        - enum
        - date
        - dateTime
        - any
        - intEnum
        - int32Enum
    enum:
      type: string
      description: "A string based enum"
      enum:
        - "one"
        - "two"
        - "three"
        - "four_and_more"
      example: "one"
  securitySchemes:
    apiKeyAuth:
      type: apiKey
      in: header
      name: Authorization
      description: Authenticate using an API Key generated via our platform.

The response has properties like:

bigint:
  type: integer
  format: bigint
  example: 8821239038968084
bigintStr:
  type: string
  format: bigint
  example: "9223372036854775808"
decimal:
  type: number
  format: decimal
  example: 3.141592653589793
decimalStr:
  type: string
  format: decimal
  example: "3.14159265358979344719667586"

ie integers and numbers that are using advances formats. decimal is currently a recognized format in the official OpenAPI format registry and we are trying to get bigint added as well.

currently wiretap just returns nothing for these properties:

{
  "json": {
    "any": "any",
    "bool": true,
    "date": "2020-01-01",
    "dateTime": "2020-01-01T00:00:00.000001Z",
    "enum": "one",
    "float32": 1.1,
    "int": 1,
    "int32": 1,
    "int32Enum": 55,
    "intEnum": 2,
    "num": 1.1,
    "str": "test"
  }
}

where we need it to return the defined examples at least

daveshanley commented 7 months ago

libopenapi v0.16.5 now supports the follow format values:

Which means they can be used to render out strings and numbers.

If you try your spec again with v0.1.10 of wiretap, and add in the following required values to your schema

required:
 ...
  - bigint
  - bigintStr
  - decimal
  - decimalStr

wiretap will now return the following mock

{
    "json": {
        "any": "any",
        "bigint": 8821239038968084,
        "bigintStr": "9223372036854775808",
        "bool": true,
        "date": "2020-01-01",
        "dateTime": "2020-01-01T00:00:00.000001Z",
        "decimal": 3.141592653589793,
        "decimalStr": "3.14159265358979344719667586",
        "enum": "one",
        "float32": 1.1,
        "int": 1,
        "int32": 1,
        "int32Enum": 55,
        "intEnum": 2,
        "num": 1.1,
        "str": "test"
    }
}