-
Notifications
You must be signed in to change notification settings - Fork 119
Fix: Sanitize v2 schema #1353
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: Sanitize v2 schema #1353
Conversation
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughNormalize OpenAPI v2 schemas for compatibility (collapse string+int AnyOf, remove OneOf, simplify AdditionalProperties), apply sanitization during v2 post-processing, and add OpenAPI e2e tests plus CI/Makefile integration. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Gen as buildPostProcessV2
participant Clone as cloneKindSchemas
participant San as sanitizeForV2
participant Refs as rewrite $ref
participant JSON as json.Unmarshal
participant Defs as Definitions Loop
Gen->>Clone: clone per-kind schemas (obj,status,list)
Clone-->>Gen: per-kind schemas
Gen->>San: sanitize obj/status/list for v2
Gen->>Refs: rewrite $ref targets in spec JSON
Gen->>JSON: unmarshal processed spec JSON
JSON-->>Gen: spec with Definitions
Gen->>Defs: iterate definitions
Defs->>San: sanitize each definition (recursive)
San-->>Defs: normalized definition
note over San: Collapse AnyOf(string,int) → type "string" + x-kubernetes-int-or-string\nRemove OneOf\nSimplify AdditionalProperties (Allows=true or recurse)\nRecurse into Properties, Items, AllOf
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Assessment against linked issues
Out-of-scope changes
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. ✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @kvaps, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request addresses an issue with the OpenAPI v2 schema generation by introducing a robust sanitization process. It ensures that the generated Swagger v2 definitions are compatible and correctly represent schema constructs, particularly by handling AnyOf
and OneOf
patterns and refining the definition of AdditionalProperties
to align with Swagger v2 specifications. This fix improves the accuracy and usability of the API documentation by removing unsupported elements and normalizing common patterns for v2.
Highlights
- Schema Sanitization Logic: A new
sanitizeForV2
function has been introduced to process OpenAPI schemas, specifically targeting constructs that are not fully supported or require transformation for Swagger v2 compatibility. This function recursively traverses the schema to apply necessary changes. - Handling of
AnyOf
forint-or-string
: ThesanitizeForV2
function now specifically handlesAnyOf
schemas that combinestring
andinteger
types. These are converted to a singletype: "string"
with thex-kubernetes-int-or-string: true
extension, a common pattern in Kubernetes API definitions for flexible type handling in v2. - Removal of
OneOf
Constructs: Unsupported OpenAPI v3 constructs likeOneOf
are now explicitly removed from the schemas during the v2 sanitization process to ensure proper Swagger v2 generation. - Refinement of
AdditionalProperties
: TheAdditionalProperties
definition has been refined to remove redundantSchema
objects whenAllows
is true, streamlining the generated Swagger v2 schema. - Comprehensive Application of Sanitization: The newly implemented sanitization logic is applied to all generated kind schemas (e.g., object, status, list) and all global definitions within the OpenAPI v2 document during the post-processing phase, ensuring comprehensive compatibility.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces sanitization for OpenAPI v2 schemas to improve compatibility by handling constructs not supported by Swagger v2, such as anyOf
and oneOf
. The core change is the new sanitizeForV2
function and its integration into the v2 post-processing flow. The changes are generally good, but I've identified a potential issue in the sanitization logic for additionalProperties
that could lead to an incorrect schema, as well as some opportunities for code refinement and simplification.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (4)
pkg/cmd/server/openapi.go (4)
63-71
: Set.spec
to an explicit object when creating a schemaless containerWhen
raw
is empty and.spec
doesn't exist yet, we only setAdditionalProperties=true
on an otherwise zero-value schema. Some consumers requiretype: object
foradditionalProperties
to apply. Being explicit here avoids edge-case validation issues.Apply this diff:
if strings.TrimSpace(raw) == "" { if target.Properties == nil { target.Properties = map[string]spec.Schema{} } prop := target.Properties["spec"] + if !prop.Type.Contains("object") { + prop.Type = spec.StringOrArray{"object"} + } prop.AdditionalProperties = &spec.SchemaOrBool{Allows: true} target.Properties["spec"] = prop return nil }
274-283
: Make int-or-string detection robust to nullable unionsThe current check fails if the union includes
"null"
(a common pattern for nullable fields). Ignoring"null"
and requiring exactly the two target types avoids false negatives.Apply this diff:
func hasIntAndStringAnyOf(anyOf []spec.Schema) bool { seen := map[string]bool{} for i := range anyOf { for _, t := range anyOf[i].Type { - seen[t] = true + if t != "null" && t != "" { + seen[t] = true + } } } - return seen["string"] && seen["integer"] && len(seen) <= 2 + return seen["string"] && seen["integer"] && len(seen) == 2 }
291-304
: Consider preserving some signal when dropping AnyOf/OneOfV2 can’t express
anyOf
/oneOf
. Clearing them is necessary, but it erases helpful shape information for downstream consumers. Optional: stash the original branches in a vendor extension for traceability.Illustrative change (optional):
if len(s.AnyOf) > 0 { if hasIntAndStringAnyOf(s.AnyOf) { s.Type = spec.StringOrArray{"string"} if s.Extensions == nil { s.Extensions = map[string]interface{}{} } s.Extensions["x-kubernetes-int-or-string"] = true } + // Optional: retain visibility of the original union for tooling/doc purposes. + if s.Extensions == nil { + s.Extensions = map[string]interface{}{} + } + if _, ok := s.Extensions["x-cozystack-anyOf"]; !ok { + s.Extensions["x-cozystack-anyOf"] = s.AnyOf + } s.AnyOf = nil } if len(s.OneOf) > 0 { - s.OneOf = nil + if s.Extensions == nil { + s.Extensions = map[string]interface{}{} + } + if _, ok := s.Extensions["x-cozystack-oneOf"]; !ok { + s.Extensions["x-cozystack-oneOf"] = s.OneOf + } + s.OneOf = nil }
359-362
: Avoid double sanitization to reduce work and surprisesWe sanitize cloned kind schemas before inserting into definitions, and then sanitize all definitions again post-unmarshal. One pass is sufficient; keeping only the final pass guarantees all defs are covered and avoids redundant work.
Apply this diff to drop the early pass:
- sanitizeForV2(obj) - sanitizeForV2(status) - sanitizeForV2(l)Also applies to: 393-397
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
pkg/cmd/server/openapi.go
(5 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Build
🔇 Additional comments (1)
pkg/cmd/server/openapi.go (1)
77-79
: DefaultingAdditionalProperties
to true for custom.spec
is correctOpenAPI defaults
additionalProperties
to true; making it explicit improves interoperability. LGTM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (2)
hack/e2e-test-openapi.bats (1)
6-8
: Reduce log noise from kubectl -v7 in CI output.The verbose logs go to stderr and can clutter reports. Keep -v7 for local debugging, but in CI consider redirecting stderr or lowering verbosity.
Apply this minimal tweak if desired:
- kubectl get -v7 --raw '/openapi/v2?timeout=32s' > /dev/null + kubectl get --raw '/openapi/v2?timeout=32s' > /dev/null 2>&1And similarly for the v3 call.
Also applies to: 10-12
packages/core/testing/Makefile (1)
50-52
: Add .PHONY for the new test target (and test) to satisfy checkmake and prevent filename collisions.This prevents Make from treating these as file targets and addresses the static analysis warning.
Apply this diff:
+ .PHONY: test test-openapi
If you prefer grouping, you can also include other purely procedural targets in the same line.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (2)
hack/e2e-test-openapi.bats
(1 hunks)packages/core/testing/Makefile
(2 hunks)
🧰 Additional context used
🪛 checkmake (0.2.2)
packages/core/testing/Makefile
[warning] 33-33: Target "test" should be declared PHONY.
(phonydeclared)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Build
🔇 Additional comments (3)
hack/e2e-test-openapi.bats (2)
1-5
: Good addition: targeted E2E coverage for OpenAPI v2/v3 and protobuf.This gives us basic contract checks against the cluster’s OpenAPI endpoints and directly exercises the v2 sanitization work.
10-12
: The providedtest-openapi
target is not currently gated behind aninstall-cozystack
prerequisite, and thetest
target simply runstest-cluster
,test-openapi
, thentest-apps
. Without ensuringinstall-cozystack
runs first, the OpenAPI group may not exist yet, causing the raw GET check to 404.Please apply one of these fixes:
• Ensure
install-cozystack
runs beforetest-openapi
by updating the Makefile’s dependencies. For example, modify thetest
target:test: test-cluster test-openapi test-apps ## Run the end-to-end tests in existing sandbox + $(MAKE) install-cozystack
or add
install-cozystack
as a prerequisite totest-openapi
:-test-openapi: +test-openapi: install-cozystack• Alternatively, wrap the raw OpenAPI GET in a guard to skip the test when
apps.cozystack.io
isn’t yet installed:@test "Test OpenAPI v3 endpoint (apps.cozystack.io present)" { - kubectl get -v7 --raw '/openapi/v3/apis/apps.cozystack.io/v1alpha1' > /dev/null + if ! kubectl api-resources --api-group=apps.cozystack.io >/dev/null 2>&1; then + skip "apps.cozystack.io API group not installed" + fi + kubectl get -v7 --raw '/openapi/v3/apis/apps.cozystack.io/v1alpha1' > /dev/null }Either approach will prevent a false-negative when the API group is not yet present.
packages/core/testing/Makefile (1)
33-33
: Integrating test-openapi into the main test flow looks good.The high-level order makes sense, assuming the cluster has the cozystack APIs available by this point.
Please confirm whether test-cluster installs cozystack APIs. If not, consider either:
- test: test-cluster install-cozystack test-openapi test-apps
- or make test-openapi depend on install-cozystack.
Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
ae50eef
to
a2134ec
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (2)
.github/workflows/pull-requests.yaml (1)
257-261
: Harden OpenAPI test step with retries and backoff to reduce flakesThe new step can be transiently flaky (API not fully ready, kube-proxy hiccups). Mirror the retry pattern used elsewhere in this workflow to stabilize CI.
Apply this patch to wrap the command with retries and a small backoff:
- - name: Run OpenAPI tests - run: | - cd /tmp/$SANDBOX_NAME - make -C packages/core/testing SANDBOX_NAME=$SANDBOX_NAME test-openapi + - name: Run OpenAPI tests + run: | + set -euo pipefail + cd /tmp/$SANDBOX_NAME + attempt=0 + until make -C packages/core/testing SANDBOX_NAME=$SANDBOX_NAME test-openapi; do + attempt=$((attempt + 1)) + if [ $attempt -ge 3 ]; then + echo "❌ Attempt $attempt failed, exiting..." + exit 1 + fi + echo "❌ Attempt $attempt failed, retrying..." + sleep 10 + done + echo "✅ The task completed successfully after $attempt attempts."packages/core/testing/Makefile (1)
50-52
: Add help text and optional retry around the OpenAPI bats to aid DX and stability
- Add a help description so
make help
surfaces this target.- Optional: wrap in a simple retry here as well (even if the workflow retries), useful for local runs.
Proposed tweaks:
-test-openapi: - docker exec "${SANDBOX_NAME}" sh -c 'cd /workspace && hack/cozytest.sh hack/e2e-test-openapi.bats' +test-openapi: ## Run OpenAPI e2e tests (v2, v3, protobuf) inside the sandbox + @attempt=0; \ + until docker exec "${SANDBOX_NAME}" sh -c 'cd /workspace && hack/cozytest.sh hack/e2e-test-openapi.bats'; do \ + attempt=$$((attempt + 1)); \ + if [ $$attempt -ge 3 ]; then \ + echo "❌ Attempt $$attempt failed, exiting..."; exit 1; \ + fi; \ + echo "❌ Attempt $$attempt failed, retrying..."; sleep 5; \ + done; \ + echo "✅ The task completed successfully after $$attempt attempts."
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (4)
.github/workflows/pull-requests.yaml
(1 hunks)hack/e2e-test-openapi.bats
(1 hunks)packages/core/testing/Makefile
(2 hunks)pkg/cmd/server/openapi.go
(5 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
- hack/e2e-test-openapi.bats
- pkg/cmd/server/openapi.go
🧰 Additional context used
🪛 checkmake (0.2.2)
packages/core/testing/Makefile
[warning] 33-33: Target "test" should be declared PHONY.
(phonydeclared)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Build
Signed-off-by: Andrei Kvapil kvapss@gmail.com
What this PR does
fixes #1352 (comment)
Release note
Summary by CodeRabbit
Bug Fixes
Refactor
Tests
CI