Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the issue with logging when there are multiple error causes #1088

Merged
merged 1 commit into from
May 3, 2024

Conversation

yogeshnikam671
Copy link
Member

@yogeshnikam671 yogeshnikam671 commented May 3, 2024

What:
When we have multiple error causes, Specmatic used to log only the single cause out of it as an error message.
This PR fixes that issue and logs all the error messages encountered.

e.g. Suppose we have a schema with examples as follows -

openapi: 3.0.1
info:
  title: OpenAPI definition
  version: v0
servers:
  - url: http://localhost:8080
    description: Generated server url
paths:
  /customers:
    post:
      tags:
        - customer-controller
      operationId: saveCustomer
      requestBody:
        required: true
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/CustomerAddress'
                - $ref: '#/components/schemas/Customer'
            examples:
              CUSTOMER_ADDRESS:
                summary: Example of a customer address object
                value:
                  id: 1
                  customerId: 1005
                  houseNo: '1234'
                  street: 'B.St'
                  city: 'Melbourne'
                  state: 'Victoria'
                  postCode: '3000'
                  requestType: 'address'
              CUSTOMER:
                summary: Example of a customer object
                value:
                  id: 104
                  customerId: 1004
                  initial: J
                  #firstName: John
                  secondName: Doe
                  customerType: GOLD
      responses:
        "200":
          description: OK
          content:
            'application/json':
              schema:
                oneOf:
                  - $ref: '#/components/schemas/CustomerAddress'
                  - $ref: '#/components/schemas/Customer'
              examples:
                CUSTOMER_ADDRESS:
                  summary: Example of a customer address object
                  value:
                    id: 1
                    customerId: 1005
                    houseNo: '1234'
                    street: 'B.St'
                    city: 'Melbourne'
                    state: 'Victoria'
                    postCode: '3000'
                    requestType: 'address'
                CUSTOMER:
                  summary: Example of a customer object
                  value:
                    id: 104
                    customerId: 1004
                    initial: J
                    firstName: John
                    secondName: Doe
                    customerType: GOLD
components:
  schemas:
    Customer:
      required:
        - customerType
        - firstName
        - initial
        - secondName
      type: object
      properties:
        id:
          type: integer
          format: int32
        customerId:
          type: integer
          format: int32
        initial:
          type: string
        firstName:
          type: string
        secondName:
          type: string
        customerType:
          type: string
          enum:
            - BRONZE
            - SILVER
            - GOLD
            - PLATINUM
        verified:
          type: boolean
    CustomerAddress:
      type: object
      properties:
        id:
          type: integer
          format: int32
        customerId:
          type: integer
          format: int32
        houseNo:
          type: string
        street:
          type: string
        city:
          type: string
        state:
          type: string
        postCode:
          type: string
        requestType:
          type: string

In the above specification, I have commented out the firstName key from the CUSTOMER example.
If we try to run Specmatic tests using such schema, it should definitely break since the CUSTOMER example is not satisfying any of the schemas defined.

Ideally, Specmatic should compare this example with both the oneOf schemas and log the respective error messages.

Specmatic does compare the example with both the schemas but while logging it logs only a single error message as follows -
Screenshot 2024-05-03 at 11 11 07 AM

This PR fixes that and logs all the respective error messages -
Screenshot 2024-05-03 at 11 11 35 AM

Why:
Specmatic was not logging all the error message due to a very minor bug where we were not returning the error message when there are multiple error causes.

How:
The PR fixes the bug by simply returning that error message.

Checklist:

Copy link
Member

@joelrosario joelrosario left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@yogeshnikam671 yogeshnikam671 merged commit e268a6c into main May 3, 2024
3 checks passed
@joelrosario joelrosario deleted the oneOf_error_log_devex_issue branch May 3, 2024 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants