Skip to content

Fix unaligned type name with spaces in generated records#1876

Merged
TharmiganK merged 4 commits intoballerina-platform:masterfrom
sathindudezoysa:fix-unaligned-type-names
Jan 30, 2026
Merged

Fix unaligned type name with spaces in generated records#1876
TharmiganK merged 4 commits intoballerina-platform:masterfrom
sathindudezoysa:fix-unaligned-type-names

Conversation

@sathindudezoysa
Copy link
Contributor

Purpose

Resolves ballerina-platform/ballerina-library#8217

This PR addresses an issue where OpenAPI schemas containing spaces in their title (e.g., title: "Collection of permission") resulted in unaligned and escaped Ballerina record names (e.g., public type Collection\ of \permission record). This caused readability issues and violated Ballerina naming conventions.

Goals

The goal is to ensure that generated Ballerina type names are valid, PascalCased identifiers, even when the input OpenAPI title contains spaces or other delimiters. Specifically, Collection of permission should be generated as CollectionOfPermission.

Approach

Modified the ESCAPE_PATTERN_FOR_MODIFIER constant in GeneratorConstants.java. I updated the regex to explicitly include whitespace \s at the beginning of the character class and added a + quantifier.

This ensures that:

  • The split method in GeneratorUtils.getValidName correctly identifies spaces as delimiters.

  • Consecutive delimiters are treated as a single split point.

  • The resulting parts are concatenated and PascalCased to form a valid identifier (e.g.Collection, Of, Permission -> CollectionOfPermission).

User stories

As a developer using the Ballerina OpenAPI tool, I want the generated record names from my OpenAPI definition to be clean, readable, and compliant with Ballerina coding standards, even if the original schema titles contain spaces.

Release note

Fixed an issue where OpenAPI schema titles with spaces resulted in escaped and unaligned Ballerina type names. They are now correctly sanitized to PascalCase (e.g., "Collection of permission" becomes "CollectionOfPermission").

Documentation

N/A

Training

N/A

Certification

N/A

Marketing

N/A

Automation tests

N/A

Security checks

Samples

N/A

Related PRs

N/A

Migrations (if applicable)

N/A

Test environment

JDK 21
Ubuntu Linux
Ballerina OpenAPI Tool CLI (Local Build)

Learning

Investigated the GeneratorUtils.getValidName logic and identified that the regex used for splitting identifiers was missing explicit space handling in the correct regex position.

@sathindudezoysa
Copy link
Contributor Author

Hi @TharmiganK,
Can you review my PR. It is just a quick change ESCAPE_PATTERN_FOR_MODIFIER constant to identify spaces in identifies correctly.

@TharmiganK
Copy link
Contributor

@sathindudezoysa We need to add a test case as well.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an issue where OpenAPI schema titles containing spaces (e.g., "Collection of permission") were incorrectly generating escaped Ballerina record names (e.g., Collection\ of\ permission) instead of properly sanitized PascalCase identifiers (e.g., CollectionOfPermission).

Changes:

  • Modified the ESCAPE_PATTERN_FOR_MODIFIER regex constant to properly handle spaces as delimiters
  • Changed from a capturing group pattern with alternation to a proper character class pattern with quantifier
  • Fixed an existing regex bug where pipe | was being treated as alternation instead of a literal character

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@sathindudezoysa
Copy link
Contributor Author

sathindudezoysa commented Jan 29, 2026

Hi @TharmiganK ,
I have a added a test case, can you please review it. Here is a result screenshot running the test

Screenshot from 2026-01-29 13-43-19

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@sathindudezoysa
Copy link
Contributor Author

Hi @TharmiganK
Did something in my code cauees fail?

@TharmiganK
Copy link
Contributor

TharmiganK commented Jan 30, 2026

Hi @TharmiganK Did something in my code cauees fail?

I don't think so. Let me check

Copy link
Contributor

@TharmiganK TharmiganK left a comment

Choose a reason for hiding this comment

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

LGTM!

@sonarqubecloud
Copy link

@TharmiganK TharmiganK merged commit 68d2a53 into ballerina-platform:master Jan 30, 2026
4 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.

Unaligned type name with spaces

3 participants

Comments