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: required of parent schema not considered in allOf #1570 #1659

Merged
merged 5 commits into from
Oct 14, 2024

Conversation

MGabr
Copy link
Contributor

@MGabr MGabr commented Oct 9, 2024

Status

READY

Description

Fixes #1570

Given a schema like

components:
  schemas:
    NewTicketNotification:
      required:
        - ctime
        - id
        - ticketId
        - type
        - userId
      type: object
      allOf:
        - $ref: '#/components/schemas/Notification'
        - type: object
          properties:
            ticketId:
              type: integer
              format: int32
            userId:
              type: integer
              format: int32

the allOf was considered as schema on its own when resolving the value of the fields, but in fact we want to consider the required fields of the parent schema also for the allOf sub schema.

This fixes this by including the required field of the parent schema in the sub schema.

Related PRs

There is this existing outdated PR: #861

Todos

  • Tests
  • Documentation
  • Changelog Entry (unreleased)

Steps to Test or Reproduce

> git clone https://github.com/MGabr/orval.git
> yarn build
> cd tests
> yarn generate:react-query

The TypeScript model generated for the new polymorphic-required.yaml test spec should have the required fields set correctly.

So for

components:
  schemas:
    DescendantOne:
      required:
        - type
        - count
      type: object
      allOf:
        - $ref: '#/components/schemas/ParentType'
        - type: object
          properties:
            value:
              type: boolean
            count:
              type: integer
              format: int32
            otherCount:
              type: integer
              format: int32

it generates

export type DescendantOneAllOf = {
  count: number;
  otherCount?: number;
  value?: boolean;
};

@MGabr MGabr changed the title fix: required not considered in allOf #1570 fix: required of parent schema not considered in allOf #1570 Oct 9, 2024
@melloware melloware added this to the 7.2.0 milestone Oct 9, 2024
@melloware melloware added the bug Something isn't working label Oct 9, 2024
melloware
melloware previously approved these changes Oct 9, 2024
melloware
melloware previously approved these changes Oct 10, 2024
@soartec-lab soartec-lab self-assigned this Oct 10, 2024
@MGabr
Copy link
Contributor Author

MGabr commented Oct 10, 2024

I think this still needs to be adjusted to handle edge cases where both the parent and the sub schema have a required field.

Like:

components:
  schemas:
    DescendantOne:
      required:
        - type
      type: object
      allOf:
        - $ref: '#/components/schemas/ParentType'
        - type: object
           required:
            - count
          properties:
            value:
              type: boolean
            count:
              type: integer
              format: int32
            otherCount:
              type: integer
              format: int32

Currently only the parent required would be considered but I think both required should be considered in this case. Still to check.

@MGabr MGabr marked this pull request as draft October 10, 2024 23:05
@MGabr MGabr marked this pull request as ready for review October 12, 2024 11:03
melloware
melloware previously approved these changes Oct 12, 2024
Copy link
Member

@soartec-lab soartec-lab left a comment

Choose a reason for hiding this comment

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

@MGabr
Thanks for nice update. i added a comment.

@@ -0,0 +1,59 @@
openapi: 3.0.1
Copy link
Member

Choose a reason for hiding this comment

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

could you merge the yaml of these test files?
I don't wanna separeate related tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I merged those two into the existing polymorphic.yaml file now

Copy link
Member

@soartec-lab soartec-lab left a comment

Choose a reason for hiding this comment

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

Thanks!

@soartec-lab soartec-lab merged commit 86b63eb into orval-labs:master Oct 14, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Schema allOf generation does not follow Swagger spec
3 participants