Skip to content

Implement ordering for parameters#15

Merged
kvaps merged 1 commit intomainfrom
ordering
Mar 16, 2026
Merged

Implement ordering for parameters#15
kvaps merged 1 commit intomainfrom
ordering

Conversation

@kvaps
Copy link
Copy Markdown
Member

@kvaps kvaps commented Sep 22, 2025

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

Summary by CodeRabbit

  • New Features

    • OpenAPI schemas now support customizable field ordering for improved documentation structure
    • Chart value schemas can now respect explicit parameter ordering
  • Refactor

    • Enhanced schema generation to enable better control over field organization

@kvaps kvaps marked this pull request as draft September 22, 2025 09:08
@gemini-code-assist
Copy link
Copy Markdown

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 enhances the OpenAPI schema generation by introducing a mechanism to explicitly order parameters. By adding an Order field to the internal Node representation and implementing a custom sorting function, the system can now generate OpenAPI schemas where parameters appear in a predictable and desired sequence, improving the consistency and readability of the generated output.

Highlights

  • Parameter Ordering: Introduced an Order field to the Node struct to explicitly store the sequence of parameters within the OpenAPI definition.
  • Order Assignment: The Build function now assigns an incremental orderCounter to parameters as they are processed, ensuring a defined order.
  • Custom Sorting Logic: Implemented a new sortedKeysByOrder function that sorts parameters primarily by their assigned Order value, falling back to lexicographical sorting for parameters with no explicit order.
  • Schema Generation Update: Modified the schema generation process in writeStruct and WriteValuesSchema to utilize the new sortedKeysByOrder function, ensuring that generated OpenAPI schemas reflect the intended parameter order.
  • New Schema Writer: Added WriteValuesSchemaWithOrder to allow for the generation of schemas with explicit parameter ordering, which is now called from main.go.
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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description

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.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Sep 22, 2025

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 40becb85-5fc4-451f-8eb6-cfc643d3407f

📥 Commits

Reviewing files that changed from the base of the PR and between 72c94a2 and f93233f.

📒 Files selected for processing (2)
  • internal/openapi/openapi.go
  • main.go

📝 Walkthrough

Walkthrough

These changes introduce per-field ordering for OpenAPI Node elements through a new Order field and orderCounter mechanism. The WriteValuesSchema function is refactored to accept an optional root parameter, enabling ordered emission of parameter fields based on explicit Order values or lexical fallback. Integration updated in main.go to pass the tree parameter.

Changes

Cohort / File(s) Summary
OpenAPI Ordering System
internal/openapi/openapi.go
Added Order int field to Node type, introduced orderCounter for sequential assignment, created sortedKeysByOrder() helper function, added NewGen() constructor, and implemented WriteValuesSchemaWithOrder() function. Modified WriteValuesSchema() to delegate to the new variant. Replaced multiple sortedKeys calls with sortedKeysByOrder throughout struct and values generation paths.
Main Entry Integration
main.go
Updated call to WriteValuesSchema() with new WriteValuesSchemaWithOrder() signature passing tree parameter, and added log line for JSON schema write status.

Sequence Diagram(s)

sequenceDiagram
    participant Main as main.go
    participant Gen as gen<br/>(OpenAPI)
    participant Node as Node<br/>(Tree)
    participant Schema as Output<br/>Schema

    Main->>Gen: WriteValuesSchemaWithOrder(crdBytes, outPath, tree)
    Gen->>Node: Traverse tree, read Order fields
    Node-->>Gen: Return nodes with Order values
    Gen->>Gen: sortedKeysByOrder(nodes)<br/>Sort by Order or lexical
    Gen->>Schema: Emit struct fields<br/>in determined order
    Gen->>Schema: Emit values schema<br/>respecting Order
    Schema-->>Main: JSON schema written
    Main->>Main: Log: "write JSON schema: %s"
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Fields now dance in ordered stride,
No lexical chaos left to hide,
The tree declares its sequence true,
Each parameter knows just what to do!
Order reigns where sorting once did play. 🌿

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch ordering
📝 Coding Plan
  • Generate coding plan for human review comments

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

You can disable sequence diagrams in the walkthrough.

Disable the reviews.sequence_diagrams setting to disable sequence diagrams in the walkthrough.

Copy link
Copy Markdown

@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 ordering for parameters, which is then used to generate an ordered values.schema.json. The implementation adds an Order field to the Node struct and populates it as parameters are parsed. My review identified a critical bug in the sorting logic that fails to correctly order parameters and a medium-severity issue with JSON formatting that results in misaligned output. I've provided code suggestions to address both of these issues.

Comment on lines +583 to +590
sort.Slice(keys, func(i, j int) bool {
nodeI := nodes[keys[i]]
nodeJ := nodes[keys[j]]
if nodeI.Order != 0 || nodeJ.Order != 0 {
return nodeI.Order < nodeJ.Order
}
return keys[i] < keys[j]
})
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

The sorting logic in sortedKeysByOrder is flawed and doesn't correctly order parameters.

  1. The first parameter is assigned Order = 0, which is the same as the default for non-parameter fields. When comparing this parameter with a non-parameter, the if nodeI.Order != 0 || nodeJ.Order != 0 condition is false, causing it to fall back to alphabetical sorting. This is incorrect.
  2. When comparing a parameter with Order > 0 against a non-parameter with Order = 0, the condition nodeI.Order < nodeJ.Order will be false, causing the non-parameter to be sorted first.

A more robust approach is to use the IsParam boolean field, which clearly distinguishes parameters from other fields. This ensures parameters are always sorted before non-parameters, and then by their specified order.

sort.Slice(keys, func(i, j int) bool {
			nodeI := nodes[keys[i]]
			nodeJ := nodes[keys[j]]
			if nodeI.IsParam && nodeJ.IsParam {
				return nodeI.Order < nodeJ.Order
			}
			if nodeI.IsParam {
				return true
			}
			if nodeJ.IsParam {
				return false
			}
			return keys[i] < keys[j]
		})

Comment on lines +809 to +814
propJSON, err := json.MarshalIndent(prop, " ", " ")
if err != nil {
return err
}

buf.WriteString(fmt.Sprintf(" \"%s\": %s", key, string(propJSON)))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The manual JSON generation for properties results in incorrect indentation in the output values.schema.json file. Using json.MarshalIndent with a hardcoded prefix for the property value causes misalignment because the prefix is applied to every line of the marshaled object, including the first one.

To produce a correctly formatted JSON, you can marshal the property without a prefix and then manually handle the indentation to align it properly under its key.

propJSON, err := json.MarshalIndent(prop, "", "  ")
						if err != nil {
							return err
						}

						// Manually indent the property JSON to align correctly.
						lines := strings.Split(string(propJSON), "\n")
						buf.WriteString(fmt.Sprintf("    \"%s\": %s", key, lines[0]))
						for _, line := range lines[1:] {
							buf.WriteString("\n    " + line)
						}

Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
@kvaps kvaps marked this pull request as ready for review March 16, 2026 09:45
@kvaps kvaps merged commit 3a0c467 into main Mar 16, 2026
2 checks passed
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.

1 participant