Fix unaligned type name with spaces in generated records#1876
Conversation
|
Hi @TharmiganK, |
|
@sathindudezoysa We need to add a test case as well. |
There was a problem hiding this comment.
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_MODIFIERregex 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.
openapi-core/src/main/java/io/ballerina/openapi/core/generators/common/GeneratorConstants.java
Show resolved
Hide resolved
|
Hi @TharmiganK ,
|
There was a problem hiding this comment.
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.
|
Hi @TharmiganK |
I don't think so. Let me check |
|




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 permissionshould be generated asCollectionOfPermission.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
splitmethod inGeneratorUtils.getValidNamecorrectly 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.getValidNamelogic and identified that the regex used for splitting identifiers was missing explicit space handling in the correct regex position.