Skip to content

Conversation

@Sadhorsephile
Copy link
Contributor

Why

Sometimes swagger parameter names may contain inappropriate characters for Dart method parameter names (e.g. filters[name]). This leads to generated code with invalid parameter names:

@Query('filter[name]') String object1,

Solution

Additional replacement rules that will be applied to parameter names during method generation can solve this issue.

Usage

swagger_parser.yaml:

...
  replacement_rules_for_raw_schema:
   - pattern: "\\]\\["
     replacement: "_"
   - pattern: "\\[]\\["
     replacement: "_"
   - pattern: "\\["
     replacement: "_"
   - pattern: "\\]"
     replacement: "_"
Before After
 Future<ItemsPaginationResponse> getSomeItems({
  @Query('filters[name]') required String object1,
  @Query('filters[weight]') int? object2,
  @Query('filters[width]') int? object3,
  @Query('filters[height]') String? object4,
  @Query('filters[year]') int? object5,
});
Future<ItemsPaginationResponse> getSomeItems({
  @Query('filters[name]') required String filtersname,
  @Query('filters[weight]') int? filtersweight,
  @Query('filters[width]') int? filterswidth,
  @Query('filters[height]') String? filtersheight,
  @Query('filters[year]') int? filtersyear,
});

This is not an ideal solution, but definitely improves readability of generated methods. I will also be glad to get better solution just to solve this issue.

- Introduced `replacementRulesForRawSchema` in `SWPConfig` and `ParserConfig` to handle schema objects with potentially invalid Dart class names.
- Updated `OpenApiParser` to utilize these replacement rules when processing names.
@Carapacik Carapacik self-requested a review January 14, 2026 12:29
@Carapacik
Copy link
Owner

Very good

@Carapacik
Copy link
Owner

@Sadhorsephile please increase version and add changelog and example to readme

- Updated CHANGELOG for version 1.37.2 with new feature.
- Modified pubspec.yaml to reflect the new version.
- Added `replacement_rules_for_raw_schema` to README for configuration.
@Sadhorsephile
Copy link
Contributor Author

@Carapacik, done. I've already added example to readme before. Tell me if this isn't enough.

@Carapacik Carapacik merged commit 97c7a1a into Carapacik:main Jan 14, 2026
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