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

Specmatic unusual behaviour #1417

Open
twinkle12b opened this issue Nov 11, 2024 · 3 comments
Open

Specmatic unusual behaviour #1417

twinkle12b opened this issue Nov 11, 2024 · 3 comments
Assignees
Labels
question Further information is requested

Comments

@twinkle12b
Copy link

twinkle12b commented Nov 11, 2024

Issue regarding generating wrong examples - For a property specified in openapi.yaml with maxlength 11 and type: string, pattern: "^([1-9]\\d{0,10})(\\.\\d{0,10})?$" it keeps generating a string sometimes with length greater than 11 and fails in generative tests. Can you please help?

Also We use discriminator types in openapi.yaml instead of enums, and usage of pattern to generate specific string names as the name of discriminator types for eg:

components:
  schemas:
    Animal:
      type: object
      required:
        - type
      properties:
        type:
          type: string
        name:
          type: string
      discriminator:
        propertyName: type
        mapping:
          dog: '#/components/schemas/Dog'
          cat: '#/components/schemas/Cat'

    Dog:
      allOf:
        - $ref: '#/components/schemas/Animal'
        - type: object
          properties:
            animal:
              type:string
              pattern: "^Dog$"
            breed:
              type: string
            barkVolume:
              type: integer

    Cat:
      allOf:
        - $ref: '#/components/schemas/Animal'
        - type: object
          properties:
            animal:
              type:string
              pattern: "^Cat$"
            breed:
              type: string
            isIndoor:
              type: boolean

----- So when use specmatic examples openapi.yaml to generate examples specmatic generate proper examples but for the examples that get generated with second discriminator types say- animal cat-- it keeps throwing error on run saying expected Dog but found cat which is against the contract. Please help me with these issues

UPDATE: Formatting changes by @harikrishnan83 for readability

@harikrishnan83 harikrishnan83 self-assigned this Nov 11, 2024
@harikrishnan83
Copy link
Member

@twinkle12b thanks a lot for sharing the details regarding the issue. We are taking a look.

@harikrishnan83 harikrishnan83 added help wanted Extra attention is needed bug Something isn't working question Further information is requested and removed help wanted Extra attention is needed bug Something isn't working labels Nov 11, 2024
@harikrishnan83
Copy link
Member

hello @twinkle12b, we did a initial analysis of the issues you have reported and here are our findings and comments on the same.

Issue no 1: maxLength not being honoured

I see that the pattern "^([1-9]\\d{0,10})(\\.\\d{0,10})?$" and your maxLength attributes are contradicting each other. As per the regex I guess a string of length 21 may be possible. Here is a quick test that shows the same.

image

That said there is indeed an issue in how Specmatic is also handling this contradiction. When generating test, it is purely going by the pattern thereby coming up with strings of length greater than 11.

We are already in the process of addressing this by flagging such contradictions while reading the API spec itself so that we can give you feedback on the same. I will keep you posted on the progress.

Meanwhile can you please update the spec so that pattern and maxLength are in agreement with each other.

Issue no 2: Question regarding discriminator with pattern instead of enum

In the schema you have shared, the discriminator syntax does not seem to be as per OpenAPI spec docs. Please let me know if I am missing something here. We took a stab at updating it ourselves, here is the version that in line with OpenAPI spec.

components:
  schemas:

    BaseAnimal:
      type: object
      required:
        - type
        - name
      properties:
        type:
          type: string
        name:
          type: string
     
    Dog:
      allOf:
        - $ref: '#/components/schemas/BaseAnimal'
        - type: object
          properties:
            animal:
              type: string
              pattern: "^Dog$"
            breed:
              type: string
            barkVolume:
              type: integer

    Cat:
      allOf:
        - $ref: '#/components/schemas/BaseAnimal'
        - type: object
          properties:
            animal:
              type: string
              pattern: "^Cat$"
            breed:
              type: string
            isIndoor:
              type: boolean
    
    Animal:
      oneOf:
        - $ref: '#/components/schemas/Dog'
        - $ref: '#/components/schemas/Cat'
      discriminator:
        propertyName: type
        mapping:
          dog: '#/components/schemas/Dog'
          cat: '#/components/schemas/Cat'

Can you please have a look at the above and see if this is what you wanted to achieve with your schema design?

Also took the liberty to update your issue description to improve formatting for readability.

Please let us know if you have any questions on this.

Thanks.

@twinkle12b
Copy link
Author

Hello @harikrishnan83 thank you so much for going through my issues and also kind of resolving the first one. Let me get back to you on the discriminator issue that I am facing with more detail example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants