Closed danielgtaylor closed 1 month ago
The changes introduce a new test case in huma_test.go
for an API operation that allows public access without security requirements. It registers a new GET operation at the path /public
with an empty security array. In openapi.go
, a new function isNilValue
is added to check for nil values, enhancing JSON marshaling logic. The marshalJSON
function is updated to use this new method, and the MarshalJSON
method for the Operation
struct is modified to omit the security
field when it is nil instead of when it is empty.
Files | Change Summary |
---|---|
huma_test.go | Added a test case in TestFeatures for a new public API operation with no security requirements. |
openapi.go | Introduced isNilValue function to check for nil values; updated marshalJSON and MarshalJSON methods for improved field omission logic. |
Objective | Addressed | Explanation |
---|---|---|
Ensure security: [] is reflected in OAS for operations (#[593]) |
β |
huma_test.go
related to middleware and parameter handling may connect with the enhancements made in the same file, particularly regarding the robustness of request body processing and validation, which aligns with the overall improvements in the API framework.π° In the meadow where the code does play,
A new path opens, bright as day.
With security light, the public can roam,
Hopping through APIs, they feel at home.
A nil check added, to keep things neat,
In the world of JSON, itβs quite a feat! πΌ
openapi.go (4)
`54-71`: **Comprehensive nil check implementation** The new `isNilValue` function is a well-implemented utility that enhances nil checks beyond simple equality. It correctly handles various types such as channels, functions, interfaces, maps, pointers, and slices using reflection. This addition will improve the robustness of nil checks throughout the codebase. --- `978-978`: **Fix for marshaling empty security object** This change addresses the issue described in #593 by using `omitNil` instead of `omitEmpty` for the `security` field. This ensures that an empty array `[]` is properly marshaled, which is semantically significant in OpenAPI for indicating that a route should be publicly accessible. The change aligns perfectly with the PR objectives and resolves the limitation in the previous implementation. --- `79-79`: **Appropriate usage of new `isNilValue` function** The new `isNilValue` function is correctly utilized in the `marshalJSON` function, enhancing the nil check for fields marked with `omitNil`. This usage aligns well with the function's purpose and improves the overall robustness of the JSON marshaling process. Additionally, the change to the `Operation` struct's `MarshalJSON` method appears to be isolated and doesn't seem to have any unintended consequences on other parts of the code. --- Line range hint `54-978`: **Summary of changes and overall impact** The changes in this file effectively address the issue of marshaling empty security objects in OpenAPI specifications. The introduction of the `isNilValue` function provides a robust way to check for nil values across various types, which is then utilized in the JSON marshaling process. The modification to the `Operation` struct's `MarshalJSON` method ensures that empty security arrays are correctly represented, allowing for the specification of publicly accessible routes. These changes are focused, clean, and solve the problem without introducing unnecessary complexity. Moreover, the `isNilValue` function has potential for broader use throughout the codebase, adding value beyond this specific fix. Overall, these modifications improve the accuracy and flexibility of the OpenAPI specification generation, particularly in handling security-related configurations.huma_test.go (1)
`1795-1812`: **LGTM! New test case for security override feature.** The new test case "security-override-public" has been added to the `TestFeatures` function. It tests the ability to override security settings for a specific endpoint, making it publicly accessible. The implementation looks correct and follows the existing pattern of other test cases in this function. Key points: 1. The test registers a new GET operation at the path "/public". 2. It sets an empty security array, effectively making the endpoint public. 3. The test verifies that the OpenAPI document correctly serializes the empty security array as `"security":[]`. This addition enhances the test coverage for the security override feature and ensures that the OpenAPI documentation accurately reflects the security settings.
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 92.83%. Comparing base (
b509335
) to head (2587949
). Report is 2 commits behind head on main.
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
This implements the suggestion from #593 to enable better
nil
checks using reflection and to make sure the security object is marshaled anytime it is not nil, since an empty array[]
has valid semantic meaning in OpenAPI to e.g. remove a top-level security requirement to make a single route public.Adds a test to ensure the empty security object is marshaled. Fixes #593.
Summary by CodeRabbit
New Features
Bug Fixes