Skip to content

[cozystack-api] Refactor OpenAPI Schema - #1173

Merged
Andrei Kvapil (kvaps) merged 1 commit into
mainfrom
openapi
Jul 9, 2025
Merged

[cozystack-api] Refactor OpenAPI Schema#1173
Andrei Kvapil (kvaps) merged 1 commit into
mainfrom
openapi

Conversation

@kvaps

@kvaps Andrei Kvapil (kvaps) commented Jul 8, 2025

Copy link
Copy Markdown
Member

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

What this PR does

Release note

[cozystack-api] Fix updaing lists on cozystack objects
[cozystack-api] Refactor OpenAPI Schema
[cozystack-api] Support reading OpenAPI Schema from config

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Added support for dynamic OpenAPI schema post-processing for both OpenAPI v2 and v3 specifications, enabling custom schema injection per resource kind.
    • Introduced a new configuration field to allow specifying a custom OpenAPI schema.
  • Refactor

    • Streamlined OpenAPI schema handling by moving from inline logic to modular post-processing functions.
    • Implemented dynamic versioning for OpenAPI specs based on resource configuration changes.

@coderabbitai

coderabbitai Bot commented Jul 8, 2025

Copy link
Copy Markdown
Contributor

Caution

Review failed

The pull request is closed.

"""

Walkthrough

The changes modularize OpenAPI schema post-processing by introducing dedicated builder functions for both OpenAPI v2 and v3. Inline schema manipulation logic is removed from the server startup, replaced by dynamic versioning based on resource configuration and external post-processing functions. The ApplicationConfig struct is extended with an OpenAPISchema field.

Changes

File(s) Change Summary
pkg/cmd/server/openapi.go Added new file implementing modular OpenAPI v2/v3 schema post-processing functions and helpers.
pkg/cmd/server/start.go Removed inline OpenAPI post-processing logic; now uses new builder functions and dynamic versioning.
pkg/config/config.go Added OpenAPISchema string field to ApplicationConfig struct.

Sequence Diagram(s)

sequenceDiagram
    participant Server
    participant Config
    participant PostProcessorV2
    participant PostProcessorV3
    participant OpenAPI

    Server->>Config: Load resource configuration
    Config-->>Server: Return kind-to-schema map
    Server->>PostProcessorV2: Pass kind-to-schema map
    Server->>PostProcessorV3: Pass kind-to-schema map
    Server->>OpenAPI: Set version (with hash), assign post-processors
    OpenAPI-->>Server: Serve processed schemas
Loading

Possibly related PRs

Suggested labels

size:L

Suggested reviewers

  • lllamnyp
  • klinch0

Poem

A bunny with code in its paws,
Modularizes schemas without a pause.
From inline mess to helpers neat,
OpenAPI docs are now a treat!
With hashes and fields, the config grows—
In burrows of logic, efficiency flows.
🐇✨
"""


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 48bfdd1 and 91583a4.

📒 Files selected for processing (3)
  • pkg/cmd/server/openapi.go (1 hunks)
  • pkg/cmd/server/start.go (2 hunks)
  • pkg/config/config.go (1 hunks)
✨ Finishing Touches
  • 📝 Generate Docstrings

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.
    • Explain this complex logic.
    • 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. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

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

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

Documentation and Community

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (2)
pkg/cmd/server/openapi.go (2)

103-106: Document the fallback behavior for spec container.

When findSpecContainer returns nil, the code falls back to patching the root schema. This behavior should be documented.

 container := findSpecContainer(s)
-if container == nil { // fallback: use the root
+if container == nil { 
+    // fallback: use the root schema when spec property container is not found
+    // this handles cases where the schema structure differs from expected
     container = s
 }

52-67: Consider adding OpenAPI schema validation.

While the function validates JSON syntax, it doesn't validate that the provided schema is a valid OpenAPI schema. Invalid schemas could cause issues for API consumers.

Would you like me to help implement OpenAPI schema validation to ensure the user-provided schemas are semantically correct?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 699d38d and 21fbcd0.

📒 Files selected for processing (3)
  • pkg/cmd/server/openapi.go (1 hunks)
  • pkg/cmd/server/start.go (2 hunks)
  • pkg/config/config.go (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
pkg/cmd/server/start.go (4)
pkg/config/config.go (1)
  • ResourceConfig (27-29)
pkg/apis/apps/v1alpha1/types.go (1)
  • Application (56-64)
pkg/generated/applyconfiguration/apps/v1alpha1/application.go (1)
  • Application (39-46)
pkg/generated/openapi/zz_generated.openapi.go (1)
  • GetOpenAPIDefinitions (31-117)
⏰ 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 (5)
pkg/config/config.go (1)

38-44: LGTM! Field addition follows existing patterns.

The new OpenAPISchema field is appropriately placed and follows the same naming conventions as other fields in the struct.

pkg/cmd/server/start.go (2)

190-194: Efficient schema extraction approach.

Creating a map for fast lookups is a good optimization for the post-processing functions.


196-206: Clean modularization of OpenAPI post-processing.

The refactoring successfully separates concerns by delegating schema customization to dedicated post-processing functions.

pkg/cmd/server/openapi.go (2)

32-49: Well-implemented recursive schema traversal.

The function correctly handles all schema composition patterns (AllOf, OneOf, AnyOf) to find the spec property container.


124-175: Comprehensive OpenAPI v2 post-processing implementation.

The function correctly handles both individual resource schemas and their corresponding List schemas, properly updating the items reference.

Comment thread pkg/cmd/server/start.go
Comment thread pkg/cmd/server/openapi.go
@kvaps
Andrei Kvapil (kvaps) force-pushed the openapi branch 4 times, most recently from df21caa to b42ad59 Compare July 8, 2025 20:58
Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
@kvaps
Andrei Kvapil (kvaps) force-pushed the openapi branch 2 times, most recently from f63c0a0 to 91583a4 Compare July 9, 2025 16:30
@kvaps
Andrei Kvapil (kvaps) merged commit f32c642 into main Jul 9, 2025
2 checks passed
@kvaps
Andrei Kvapil (kvaps) deleted the openapi branch July 9, 2025 16:30
Andrei Kvapil (kvaps) added a commit that referenced this pull request Jul 9, 2025
Depends on #1173

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

<!-- Thank you for making a contribution! Here are some tips for you:
- Start the PR title with the [label] of Cozystack component:
- For system components: [platform], [system], [linstor], [cilium],
[kube-ovn], [dashboard], [cluster-api], etc.
- For managed apps: [apps], [tenant], [kubernetes], [postgres],
[virtual-machine] etc.
- For development and maintenance: [tests], [ci], [docs], [maintenance].
- If it's a work in progress, consider creating this PR as a draft.
- Don't hesistate to ask for opinion and review in the community chats,
even if it's still a draft.
- Add the label `backport` if it's a bugfix that needs to be backported
to a previous version.
-->

## What this PR does


### Release note

<!--  Write a release note:
- Explain what has changed internally and for users.
- Start with the same [label] as in the PR title
- Follow the guidelines at
https://github.com/kubernetes/community/blob/master/contributors/guide/release-notes.md.
-->

```release-note
[cozystack-api] Specify OpenAPI schema for apps
```
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.

2 participants