Skip to content

Fix NPE in query param detection for $ref parameters#1882

Merged
TharmiganK merged 1 commit into
ballerina-platform:masterfrom
VellummyilumVinoth:fix-NPE
Mar 13, 2026
Merged

Fix NPE in query param detection for $ref parameters#1882
TharmiganK merged 1 commit into
ballerina-platform:masterfrom
VellummyilumVinoth:fix-NPE

Conversation

@VellummyilumVinoth

@VellummyilumVinoth VellummyilumVinoth commented Mar 13, 2026

Copy link
Copy Markdown
Contributor

Purpose

$Subject

Goals

Describe the solutions that this feature/fix will introduce to resolve the problems described above

Approach

Describe how you are implementing the solutions. Include an animated GIF or screenshot if the change affects the UI (email documentation@wso2.com to review all UI text). Include a link to a Markdown file or Google doc if the feature write-up is too long to paste here.

User stories

Summary of user stories addressed by this change>

Release note

Brief description of the new feature or bug fix as it will appear in the release notes

Documentation

Link(s) to product documentation that addresses the changes of this PR. If no doc impact, enter “N/A” plus brief explanation of why there’s no doc impact

Training

Link to the PR for changes to the training content in https://github.com/wso2/WSO2-Training, if applicable

Certification

Type “Sent” when you have provided new/updated certification questions, plus four answers for each question (correct answer highlighted in bold), based on this change. Certification questions/answers should be sent to certification@wso2.com and NOT pasted in this PR. If there is no impact on certification exams, type “N/A” and explain why.

Marketing

Link to drafts of marketing content that will describe and promote this feature, including product page changes, technical articles, blog posts, videos, etc., if applicable

Automation tests

  • Unit tests

    Code coverage information

  • Integration tests

    Details about the test cases and coverage

Security checks

Samples

Provide high-level details about the samples related to this feature

Related PRs

List any other related PRs

Migrations (if applicable)

Describe migration steps and platforms on which migration has been tested

Test environment

List all JDK versions, operating systems, databases, and browser/versions on which this feature/fix was tested

Learning

Describe the research phase and any blog posts, patterns, libraries, or add-ons you used to solve the problem.

Overview

This pull request fixes a NullPointerException encountered when the code generator processes OpenAPI parameters defined via $ref. The change improves stability by resolving referenced parameter definitions before inspecting their location.

Changes

  • ClientDocCommentGenerator.java
    • Resolve $ref parameters when computing whether an operation has query parameters and when iterating parameters to generate documentation. Uses extractReferenceType() via GeneratorMetaData and treats unresolved references as non-query to avoid NPEs.
  • Tests
    • Adds testRefQueryParameters unit test which supplies an OpenAPI spec with $ref-based path, query, and header parameters and validates the generated Ballerina client matches the expected output.

Impact

Prevents runtime exceptions during documentation/code generation for APIs that reference parameter definitions, ensuring correct detection of query parameters and more reliable generation behavior.

@coderabbitai

coderabbitai Bot commented Mar 13, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

Adds a unit test and OpenAPI test fixture, and updates ClientDocCommentGenerator to resolve $ref parameters when detecting and documenting query parameters.

Changes

Cohort / File(s) Summary
Test Infrastructure
openapi-cli/src/test/java/io/ballerina/openapi/CodeGeneratorTest.java, openapi-cli/src/test/resources/ref_query_parameters.yaml, openapi-cli/src/test/resources/expected_gen/ref_query_parameters.bal
Adds testRefQueryParameters() plus an OpenAPI spec using $ref for path/query/header params and the expected generated Ballerina client output.
Core Generator Logic
openapi-core/src/main/java/io/ballerina/openapi/core/generators/document/ClientDocCommentGenerator.java
Dereferences parameter $refs via GeneratorMetaData and extractReferenceType() when computing hasQueryParams and while iterating parameters; handles InvalidReferenceException by skipping unresolved refs.

Sequence Diagram(s)

(omitted)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • daneshk
  • shafreenAnfar

Poem

🐰 I hopped through refs and found the query light,
Paths, headers, whispers all now in sight,
I nudged the generator, tidy and spry,
Resolved each reference — a carrot-coded try! 🥕

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The pull request description is almost entirely composed of unfilled template placeholders with minimal substantive content beyond the title reference. Fill in critical sections: Purpose (explain the NPE issue), Goals (solution overview), Approach (how $ref parameters are now dereferenced), and relevant test/security sections with actual details rather than placeholder text.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: fixing an NPE that occurs during query parameter detection when parameters use $ref, which aligns with the actual code changes shown in the summary.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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 (1)
openapi-core/src/main/java/io/ballerina/openapi/core/generators/document/ClientDocCommentGenerator.java (1)

326-334: Existing code has same potential null safety issues.

For reference, the existing code block at lines 326-334 has the same pattern and potential NPE issues. The new fix is consistent with this existing implementation, but both would benefit from null checks. Consider addressing this as a follow-up refactor for the entire method.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@openapi-core/src/main/java/io/ballerina/openapi/core/generators/document/ClientDocCommentGenerator.java`
around lines 326 - 334, The loop over operation.getParameters() can still throw
NPEs when components or their parameters map is null; update the block handling
parameter references (the lambda using parameter.get$ref()) to defensively check
that GeneratorMetaData.getInstance().getOpenAPI() and its getComponents() and
getParameters() are non-null before calling get(...), and after lookup ensure
the resolved parameter is non-null before assigning back to the loop variable;
also keep the existing InvalidReferenceException catch but return only when
resolution truly fails. Use the existing methods extractReferenceType(...) and
InvalidReferenceException to locate the logic to change.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@openapi-core/src/main/java/io/ballerina/openapi/core/generators/document/ClientDocCommentGenerator.java`:
- Around line 315-325: In ClientDocCommentGenerator, guard the chained calls to
GeneratorMetaData.getInstance().getOpenAPI().getComponents() and
.getParameters() used when resolving a parameter $ref (the hasQueryParams lambda
and the subsequent forEach block) by first checking components and parameters
are non-null before calling .get(extractReferenceType(...)); if either is null,
treat it as a missing reference (return false in the predicate or skip
processing in the forEach). Keep the existing InvalidReferenceException
handling, and apply the same null-safe checks to both the hasQueryParams
stream.anyMatch lambda and the forEach block that follows so NPEs are prevented
when components or parameters are absent.

---

Nitpick comments:
In
`@openapi-core/src/main/java/io/ballerina/openapi/core/generators/document/ClientDocCommentGenerator.java`:
- Around line 326-334: The loop over operation.getParameters() can still throw
NPEs when components or their parameters map is null; update the block handling
parameter references (the lambda using parameter.get$ref()) to defensively check
that GeneratorMetaData.getInstance().getOpenAPI() and its getComponents() and
getParameters() are non-null before calling get(...), and after lookup ensure
the resolved parameter is non-null before assigning back to the loop variable;
also keep the existing InvalidReferenceException catch but return only when
resolution truly fails. Use the existing methods extractReferenceType(...) and
InvalidReferenceException to locate the logic to change.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d4eadaf7-0b08-46de-a261-f4e1c065078c

📥 Commits

Reviewing files that changed from the base of the PR and between d883936 and a55b33d.

📒 Files selected for processing (4)
  • openapi-cli/src/test/java/io/ballerina/openapi/CodeGeneratorTest.java
  • openapi-cli/src/test/resources/expected_gen/ref_query_parameters.bal
  • openapi-cli/src/test/resources/ref_query_parameters.yaml
  • openapi-core/src/main/java/io/ballerina/openapi/core/generators/document/ClientDocCommentGenerator.java

@sonarqubecloud

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@openapi-core/src/main/java/io/ballerina/openapi/core/generators/document/ClientDocCommentGenerator.java`:
- Around line 315-325: The lambda used to compute hasQueryParams can NPE when a
well-formed $ref is not present in components; after retrieving the referenced
parameter via
GeneratorMetaData.getInstance().getOpenAPI().getComponents().getParameters().get(extractReferenceType(parameter.get$ref())),
add a null check and treat a null result the same as catching
InvalidReferenceException (i.e., return false), so that you only call
parameter.getIn() when parameter is non-null; update the anonymous lambda in
ClientDocCommentGenerator's hasQueryParams computation accordingly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 037bd0c2-e973-4b57-8299-4653e41f79a8

📥 Commits

Reviewing files that changed from the base of the PR and between a55b33d and eb63d5b.

📒 Files selected for processing (4)
  • openapi-cli/src/test/java/io/ballerina/openapi/CodeGeneratorTest.java
  • openapi-cli/src/test/resources/expected_gen/ref_query_parameters.bal
  • openapi-cli/src/test/resources/ref_query_parameters.yaml
  • openapi-core/src/main/java/io/ballerina/openapi/core/generators/document/ClientDocCommentGenerator.java
🚧 Files skipped from review as they are similar to previous changes (2)
  • openapi-cli/src/test/resources/expected_gen/ref_query_parameters.bal
  • openapi-cli/src/test/java/io/ballerina/openapi/CodeGeneratorTest.java

@TharmiganK TharmiganK 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.

LGTM

@TharmiganK TharmiganK merged commit 5d377e5 into ballerina-platform:master Mar 13, 2026
5 of 6 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.

2 participants