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(openapi): merge handlers with same route and different method #1497

Merged
merged 3 commits into from
Aug 6, 2023
Merged

fix(openapi): merge handlers with same route and different method #1497

merged 3 commits into from
Aug 6, 2023

Conversation

Leo-Bourbon
Copy link
Contributor

πŸ”— Linked issue

#1350

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

In the openapi.ts file, the paths property is generated by using Object.fromEntries on the result of a map operation on handlersMeta. However, this causes unwanted behaviour : when there are sub-paths routes that only differ by their method, only the last path is present, as it has overridden the other paths information in the final paths property.

To fix this, I am now creating the paths object and merging the sub-paths using Array.reduce on handlersMeta instead. The operations inside this reduce are still the same as the ones inside the previous map implementation.
What has changed is that now instead of returning an array of the route and its path, I now check if the accumulated value, (the total paths object), has the current route as a property. If it does not, I add the route property and the corresponding route information. If it does, I merge the current route information with the information already present for this route.

Resolves #1350

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly. - N/A

The current logic uses `Object.fromEntries` in order to merge the paths.
However, this causes a bug where if two paths are identical, but their
methods differ, the last path will override the others. See issue #1350.

This fixes that by merging the paths using `reduce` instead.
@Hebilicious Hebilicious added the bug Something isn't working label Jul 28, 2023
@Hebilicious Hebilicious requested a review from pi0 July 28, 2023 13:39
@pi0 pi0 changed the title fix(openapi): Change how subpaths are merged for the Openapi paths fix(openapi): merge handlers with same route and different method Aug 6, 2023
@codecov
Copy link

codecov bot commented Aug 6, 2023

Codecov Report

Merging #1497 (3de1978) into main (fddf57e) will increase coverage by 0.07%.
Report is 25 commits behind head on main.
The diff coverage is n/a.

@@            Coverage Diff             @@
##             main    #1497      +/-   ##
==========================================
+ Coverage   76.25%   76.33%   +0.07%     
==========================================
  Files          73       73              
  Lines        7453     7550      +97     
  Branches      733      744      +11     
==========================================
+ Hits         5683     5763      +80     
- Misses       1769     1785      +16     
- Partials        1        2       +1     

see 10 files with indirect coverage changes

Copy link
Member

@pi0 pi0 left a comment

Choose a reason for hiding this comment

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

Thanks!

@pi0 pi0 merged commit 55ac06f into unjs:main Aug 6, 2023
8 of 9 checks passed
@Leo-Bourbon Leo-Bourbon deleted the fix/openapi-paths-merge branch August 9, 2023 13:31
@pi0 pi0 mentioned this pull request Aug 21, 2023
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.

Problem merging subpath routes when openapi.json is generated
3 participants