Skip to content

Incorrect import path with trailing underscore when client_postfix is empty #445

@mateusfccp-cos

Description

@mateusfccp-cos

Steps to reproduce

Description

When client_postfix is set to an empty string in swagger_parser.yaml, the generated root client file produces an incorrect import path with a trailing underscore.

swagger_parser version

1.43.0

Configuration

swagger_parser:
  name: my_api
  root_client: true
  root_client_name: MyApiClient
  client_postfix: ''
  put_in_folder: false

This causes a compilation error because the actual file is named my_api.dart.

Root cause

In lib/src/generator/templates/dart_root_client_template.dart, line 61 unconditionally adds an underscore between the name and postfix:

"${snakeName}_${postfix.toSnake}.dart"

When postfix is empty, this produces my_api_.dart instead of my_api.dart.

Minimal reproduction

swagger_parser.yaml

swagger_parser:
  schema_path: api.yaml
  output_directory: lib/generated
  name: my_api
  language: dart
  root_client: true
  root_client_name: MyApiClient
  client_postfix: ''
  put_in_folder: false

Steps:

  1. Run dart pub get
  2. Run dart run swagger_parser
  3. Check lib/generated/my_api_client.dart - import has trailing underscore

Expected results

The generated import should be:

import 'my_api/my_api.dart';

Actual results

The generated import is:

import 'my_api/my_api_.dart';

Your OpenApi snippet

openapi: 3.0.0
info:
  title: Test API
  version: '1.0'
paths:
  /users:
    get:
      operationId: getUsers
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object

Code sample

Not relevant.

Logs

Suggested fix

Handle empty postfix in the template:

// Current:
"${snakeName}_${postfix.toSnake}.dart"

// Fixed:
"${snakeName}${postfix.isEmpty ? '' : '_${postfix.toSnake}'}.dart"

Dart version and used packages versions

pubspec.yaml

name: swagger_parser_bug_repro
environment:
  sdk: ^3.0.0
dependencies:
  dio: ^5.0.0
  retrofit: ^4.0.0
dev_dependencies:
  swagger_parser: ^1.43.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions