|
| 1 | +# Commit Message for PR |
| 2 | + |
| 3 | +## Title |
| 4 | +[Dart] Add Optional<T> support with patchOnly flag for PATCH operations |
| 5 | + |
| 6 | +## Description |
| 7 | + |
| 8 | +This PR adds support for `Optional<T>` wrapping in the Dart generator, specifically designed for PATCH operations that require three-state semantics (absent, present-null, present-value). |
| 9 | + |
| 10 | +### Problem |
| 11 | +PATCH operations need to distinguish between: |
| 12 | +- **Absent**: Field not included in request → Don't update |
| 13 | +- **Present(null)**: Field explicitly set to null → Clear/nullify field |
| 14 | +- **Present(value)**: Field set to a value → Update to value |
| 15 | + |
| 16 | +Regular nullable Dart types (`String?`) cannot distinguish between "absent" and "present-null". |
| 17 | + |
| 18 | +### Solution |
| 19 | +Added two new CLI flags: |
| 20 | +- `useOptional=true`: Enable Optional<T> support |
| 21 | +- `patchOnly=true`: Automatically apply Optional<T> only to PATCH request schemas |
| 22 | + |
| 23 | +When both flags are enabled, the generator: |
| 24 | +1. Scans the OpenAPI spec during preprocessing to identify all PATCH operations |
| 25 | +2. Tracks which schemas are used as PATCH request bodies |
| 26 | +3. Wraps optional properties in those schemas with `Optional<T>` |
| 27 | +4. Preserves regular nullable types (`T?`) for all other operations (GET, POST, PUT, DELETE) |
| 28 | + |
| 29 | +### Changes |
| 30 | + |
| 31 | +**Java Generator (`AbstractDartCodegen.java`)**: |
| 32 | +- Added `patchOnly` boolean flag and `patchRequestSchemas` Set |
| 33 | +- Implemented `preprocessOpenAPI()` to scan for PATCH operations |
| 34 | +- Modified `postProcessModels()` to conditionally apply Optional wrapping |
| 35 | +- Added `wrapPropertyWithOptional()` helper method |
| 36 | + |
| 37 | +**Supporting Files (`DartClientCodegen.java`, `DartDioClientCodegen.java`)**: |
| 38 | +- Added conditional inclusion of `optional.dart` supporting file when `useOptional=true` |
| 39 | + |
| 40 | +**Templates**: |
| 41 | +- Added `optional.mustache`: Defines the `Optional<T>` sealed class with `Absent` and `Present` subclasses |
| 42 | +- Modified `native_class.mustache`: Field declarations, toJson(), fromJson() to handle Optional |
| 43 | +- Modified `dart_constructor.mustache`: Constructor parameters for Optional fields |
| 44 | +- Modified enum templates: Support for Optional<Enum> types |
| 45 | +- Modified `apilib.mustache`: Export optional.dart when enabled |
| 46 | + |
| 47 | +### Usage |
| 48 | + |
| 49 | +```bash |
| 50 | +openapi-generator-cli generate \ |
| 51 | + -i spec.yaml \ |
| 52 | + -g dart \ |
| 53 | + -o output \ |
| 54 | + --additional-properties=useOptional=true,patchOnly=true |
| 55 | +``` |
| 56 | + |
| 57 | +**Generated Code Example**: |
| 58 | + |
| 59 | +Given a spec with POST and PATCH operations: |
| 60 | + |
| 61 | +```dart |
| 62 | +// POST request - regular nullable |
| 63 | +class CreateUserRequest { |
| 64 | + String? email; |
| 65 | + String? name; |
| 66 | +} |
| 67 | +
|
| 68 | +// PATCH request - uses Optional with nullable inner type |
| 69 | +class UpdateUserRequest { |
| 70 | + Optional<String?> email; |
| 71 | + Optional<String?> name; |
| 72 | +} |
| 73 | +
|
| 74 | +// Usage |
| 75 | +final update = UpdateUserRequest( |
| 76 | + email: Optional.present('[email protected]'), // Update field |
| 77 | + name: Optional.absent(), // Don't update field |
| 78 | +); |
| 79 | +``` |
| 80 | + |
| 81 | +**Note**: Inner types in `Optional<T?>` are always nullable to handle deserialization failures. |
| 82 | + |
| 83 | +### Backward Compatibility |
| 84 | +- All changes are opt-in via CLI flags |
| 85 | +- When flags are not set, generator behavior is unchanged |
| 86 | +- Existing generated code is not affected |
| 87 | + |
| 88 | +### Testing |
| 89 | +- Verified baseline behavior (no flags): 0 errors |
| 90 | +- Verified with patchOnly flag: Correctly identifies PATCH schemas and applies Optional |
| 91 | +- Tested with real project: 63 PATCH schemas identified automatically |
| 92 | +- All changes compile successfully |
| 93 | + |
| 94 | +### Related Issues |
| 95 | +Addresses the need for PATCH three-state semantics in Dart clients. |
0 commit comments