Skip to content

Conversation

kvaps
Copy link
Member

@kvaps kvaps commented Aug 21, 2025

Signed-off-by: Andrei Kvapil kvapss@gmail.com

What this PR does

fixes #1352 (comment)

Release note

[cozystack-api] Fix: Sanitize v2 schema

Summary by CodeRabbit

  • Bug Fixes

    • Improved OpenAPI/Swagger v2 compatibility by normalizing schemas (handle int-or-string patterns, remove unsupported oneOf/anyOf, and fix empty additionalProperties), producing more consistent v2-compliant definitions.
  • Refactor

    • Added internal v2 post-processing to sanitize schemas across all definitions without changing public APIs.
  • Tests

    • Added end-to-end OpenAPI tests validating v2, v3 and protobuf v2 endpoints and integrated them into the test suite.
  • CI

    • Added an OpenAPI test step to the pull-request workflow so OpenAPI tests run during CI.

@kvaps kvaps requested review from lllamnyp and klinch0 as code owners August 21, 2025 10:58
Copy link
Contributor

coderabbitai bot commented Aug 21, 2025

Note

Other AI code review bot(s) detected

CodeRabbit 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.

Walkthrough

Normalize 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

Cohort / File(s) Summary
OpenAPI v2 sanitization & post-processing
pkg/cmd/server/openapi.go
Add sanitizeForV2 and hasIntAndStringAnyOf; sanitize per-kind schemas before adding definitions and sanitize all definitions after JSON unmarshal; simplify AdditionalProperties handling; minor refactor in cloning/unmarshal flow.
OpenAPI e2e tests
hack/e2e-test-openapi.bats
Add Bats tests for OpenAPI v2, OpenAPI v3 for a CRD group, and protobuf-encoded OpenAPI v2 endpoint.
Test orchestration (Makefile)
packages/core/testing/Makefile
Add test-openapi target and include it as a prerequisite of the test target so OpenAPI e2e tests run in sandbox via cozytest.
CI workflow integration
.github/workflows/pull-requests.yaml
Add "Run OpenAPI tests" step to the install_cozystack job to execute make -C packages/core/testing ... test-openapi in the sandbox during PR CI.

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
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Assessment against linked issues

Objective Addressed Explanation
Ensure OpenAPI served by the server is compatible and does not break clients that consume v2 schemas (e.g., ArgoCD) [#1352] Sanitization targets known problematic schema shapes (AnyOf int/string, OneOf, AdditionalProperties) but no end-to-end verification with ArgoCD or logs proving the original failure mode is resolved.

Out-of-scope changes

Code Change Explanation
Add Bats e2e tests (hack/e2e-test-openapi.bats) New test scaffolding and verification steps are test/CI additions and not directly required by the linked issue, which concerns runtime API incompatibility.
Add Makefile target test-openapi (packages/core/testing/Makefile) Test orchestration changes affect CI/test flow rather than server runtime or schema generation fixes requested in the issue.
Add CI step to run OpenAPI tests (.github/workflows/pull-requests.yaml) CI integration is operational/infrastructure scope and not a direct fix for server-side OpenAPI generation or ArgoCD failures described in the issue.

Possibly related PRs

Suggested labels

ok-to-test

Suggested reviewers

  • lllamnyp
  • klinch0

Poem

"I twitch my whiskers, tidy specs with care,
AnyOfs pruned, OneOfs vanish in air.
I flag int-or-string with a tiny hop,
straighten props and items — then stop.
A rabbit's patch; schemas sleep sound and fair." 🥕

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 Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch sanitize-openapi-v2

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@gemini-code-assist gemini-code-assist bot left a 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 for int-or-string: The sanitizeForV2 function now specifically handles AnyOf schemas that combine string and integer types. These are converted to a single type: "string" with the x-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 like OneOf are now explicitly removed from the schemas during the v2 sanitization process to ensure proper Swagger v2 generation.
  • Refinement of AdditionalProperties: The AdditionalProperties definition has been refined to remove redundant Schema objects when Allows 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

  1. 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.

Copy link

@gemini-code-assist gemini-code-assist bot left a 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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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 container

When raw is empty and .spec doesn't exist yet, we only set AdditionalProperties=true on an otherwise zero-value schema. Some consumers require type: object for additionalProperties 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 unions

The 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/OneOf

V2 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 surprises

We 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.

📥 Commits

Reviewing files that changed from the base of the PR and between 8b49e74 and 75c17e9.

📒 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: Defaulting AdditionalProperties to true for custom .spec is correct

OpenAPI defaults additionalProperties to true; making it explicit improves interoperability. LGTM.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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>&1

And 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.

📥 Commits

Reviewing files that changed from the base of the PR and between 75c17e9 and ae50eef.

📒 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 provided test-openapi target is not currently gated behind an install-cozystack prerequisite, and the test target simply runs test-cluster, test-openapi, then test-apps. Without ensuring install-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 before test-openapi by updating the Makefile’s dependencies. For example, modify the test 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 to test-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.

kvaps added 2 commits August 21, 2025 16:51
Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
@kvaps kvaps force-pushed the sanitize-openapi-v2 branch from ae50eef to a2134ec Compare August 21, 2025 14:51
Copy link
Contributor

@coderabbitai coderabbitai bot left a 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 flakes

The 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.

📥 Commits

Reviewing files that changed from the base of the PR and between ae50eef and a2134ec.

📒 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

@kvaps kvaps merged commit d3bce65 into main Aug 26, 2025
20 checks passed
@kvaps kvaps deleted the sanitize-openapi-v2 branch August 26, 2025 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fresh Install v0.35.1 => Kubernetes API not reachable in-cluster via ArgoCD, maybe CRD issues
2 participants