Carapacik / swagger_parser

Dart package that takes an OpenApi definition file and generates REST clients based on retrofit and data classes for your project.
https://pub.dev/packages/swagger_parser
MIT License
101 stars 47 forks source link

when define get request and singular path, export path replace camelCase #274

Open manabu0926 opened 1 week ago

manabu0926 commented 1 week ago

Steps to reproduce

1: define get request schema 2: dart run swagger_parser && dart run build_runner build -d 3: export error output

Expected results

export

// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint, unused_import

import 'package:dio/dio.dart';
import 'package:retrofit/retrofit.dart';

import '../models/driver_current_driver_response.dart';
import '../models/ok.dart';

part 'driver_api_current_driver_client.g.dart';

@RestApi()
abstract class DriverApiCurrentDriverClient {
  factory DriverApiCurrentDriverClient(Dio dio, {String? baseUrl}) = _DriverApiCurrentDriverClient;

  /// ログイン中のドライバーの情報を取得
  @GET('/api/driver_api/current_user')
  Future<HttpResponse<DriverCurrentDriverResponse>> getCurrentDriver();
}

Actual results

export

// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint, unused_import

import 'package:dio/dio.dart';
import 'package:retrofit/retrofit.dart';

import '../models/driver_current_driver_response.dart';
import '../models/ok.dart';

part 'driver_api_current_driver_client.g.dart';

@RestApi()
abstract class DriverApiCurrentDriverClient {
  factory DriverApiCurrentDriverClient(Dio dio, {String? baseUrl}) = _DriverApiCurrentDriverClient;

  /// ログイン中のドライバーの情報を取得
  @GET('/api/driver_api/CurrentDriver')
  Future<HttpResponse<DriverCurrentDriverResponse>> getCurrentDriver();
}

Your OpenApi snippet

openapi: 3.0.0
info:
  title: API
  description: knight navi Api definitions
  version: v1

servers:
  # docker-composeでprismをホストしているポート番号に変更
  - url: "http://localhost:8082/"

paths:
  /api/driver_api/current_driver:
    $ref: ./resources/paths/api/driver_api/current_driver.yml
get:
  tags:
    - DriverApi::CurrentDriver
  summary: ログイン中のドライバーの情報を取得
  operationId: getCurrentDriver
  security:
    - bearerAuth: []
  responses:
    "200":
      description: "OK"
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/driver_current_driver_response"

components:
  schemas:
    driver_current_driver_response:
      type: object
      properties:
        currentDriver:
          $ref: "../../../components/schemas/driver_api/driver.yml#/current_driver"

Code sample

Code sample ```dart [Paste your code here] ```

Logs

Logs ```console [Paste your logs here] ```

Dart version and used packages versions

retrofit 4.4.1 carapacik_lints: ^1.9.1 dart_mappable_builder: ^4.3.0 freezed: ^2.5.7 json_serializable: ^6.8.0 retrofit_generator: ^9.1.3 swagger_parser: ^1.19.2

manabu0926 commented 1 week ago

checkout this project and survey, this bug occured other same name type defined. e.g.

paths:
  /api/driver_api/current_driver:
.
.
.
components:
  schemas:
    current_driver: ← this schema type exists, `fileContent.replaceAllMapped` replaced "CurrentDriver" with path route.
      type: object
...

expected results exported this fix.

swagger_parser/lib/src/parser/corrector/open_api_corrector.dart:L54-62

 fileContent = fileContent.replaceAllMapped(
  RegExp('[ "\'/]$escapedType[ "\':]'),
  (match) {
    /// not replace paths `path end format`.
    return match[0]!.startsWith('/') &&
            match[0]!.endsWith(':')
        ? match[0]!
        : match[0]!.replaceAll(type, correctType);
  },
);

I would appreciate it if you could check it. :)