dart-lang / code_builder

A fluent API for generating valid Dart source code
https://pub.dev/packages/code_builder
BSD 3-Clause "New" or "Revised" License
423 stars 66 forks source link

DartObject `getField` doesn't retrieve field from parent #375

Closed RicardoRB closed 1 year ago

RicardoRB commented 1 year ago

Having an annotation that extends from another abstract annotation with fields, isn't possible to get the field from the children with _parentAnnotationTypeChecker.firstAnnotationOf(methodElement).

Acceptance Criteria:

GIVEN

abstract class Bind {
  const Bind([this.path = '']);

  /// The path to bind the property to.
  /// If not set, the entire route would be by [Controller].
  final String path;
}

class Get extends Bind {
  const Get([super.path]);
}

WHEN city_controller.dart

@Controller('/cities')
class CityController {
  CityController(this._cityService);

  final CityService _cityService;

  @Get()
  Response getCities(Request request) {
    return Response.ok(jsonEncode(_cityService.getCities()));
  }

  @Get('/<id>')
  Response getCity(@PathParam() int id) {
    return Response.ok(_cityService.getCity(id));
  }
}

controller_generator.dart

const _bindType = TypeChecker.fromRuntime(Bind);
final controllerPath = controllerAnnotation.read('path').stringValue;
final bind = _bindType.firstAnnotationOf(methodElement);
final path = '$controllerPath${bind?.getField('path')?.toStringValue() ?? ''}';

OUTPUT ['/cities','/cities']

EXPECTED ['/cities','/cities/<id>']

srawlins commented 1 year ago

Is this referring to the DartObject class defined in package:analyzer?

RicardoRB commented 1 year ago

Is this referring to the DartObject class defined in package:analyzer?

You are right, closing the issue here, thanks!