Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions vaden_class_scanner/lib/src/setups/dto_setup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ final _jsonDefaultChecker = TypeChecker.typeNamed(
JsonDefault,
inPackage: 'vaden_core',
);
final _apiFieldFormatChecker = TypeChecker.typeNamed(
ApiFieldFormat,
inPackage: 'vaden_core',
);
final paramParseChecker = TypeChecker.typeNamed(
ParamParse,
inPackage: 'vaden_core',
Expand Down Expand Up @@ -67,9 +71,9 @@ String _toOpenApi(ClassElement classElement) {
var schema = '';
if (useParseChecker.hasAnnotationOf(field)) {
final parser = _getParseConverteType(field);
schema = _fieldToSchema(parser);
schema = _fieldToSchema(parser, field: field);
} else {
schema = _fieldToSchema(field.type);
schema = _fieldToSchema(field.type, field: field);
}
// Inject default into schema if @JsonDefault present
if (_jsonDefaultChecker.hasAnnotationOfExact(field)) {
Expand Down Expand Up @@ -151,7 +155,11 @@ String _literalToJson(DartObject obj) {
return '"${obj.toString()}"';
}

String _fieldToSchema(DartType type) {
String _fieldToSchema(DartType type, {FieldElement? field}) {
if (_isUuidField(field, type)) {
return '{"type": "string", "format": "uuid"}';
}

// Se Γ© tipo built-in suportado
if (isBuiltInSupported(type)) {
return _getBuiltInOpenApiSchema(type);
Expand Down Expand Up @@ -599,6 +607,27 @@ bool _isUri(DartType type) {
return type.getDisplayString().replaceAll('?', '') == 'Uri';
}

bool _isUuidField(FieldElement? field, DartType type) {
if (field == null || !type.isDartCoreString) {
return false;
}

if (!_apiFieldFormatChecker.hasAnnotationOf(field)) {
return false;
}

final annotation = _apiFieldFormatChecker.firstAnnotationOf(field);
final formatObject = annotation?.getField('format');

final byName = formatObject?.getField('name')?.toStringValue();
if (byName == 'uuid') {
return true;
}

final raw = formatObject?.toString();
return raw?.contains('uuid') == true;
}

String _getBuiltInSerializer(
DartType type,
String fieldAccess,
Expand Down
10 changes: 10 additions & 0 deletions vaden_core/lib/src/openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,13 @@ class ApiSecurity {
/// [schemes] - The list of security scheme names required for the endpoint.
const ApiSecurity(this.schemes);
}

class ApiFieldFormat {
final FieldFormat format;

const ApiFieldFormat(this.format);
}

enum FieldFormat {
uuid;
}