@@ -151,4 +151,154 @@ public void SourceGenerator_WithResolver_WithFormatAttribute_WithNullValues_Usin
151151 Assert . Null ( deserializedModel . NullableTimeOnlyProperty ) ;
152152 Assert . Equal ( expectedJson , json ) ;
153153 }
154+
155+ [ Fact ]
156+ public void SourceGenerator_WithoutResolver_WithFormatAttribute_DoesNotApplyFormat_UsingOptions ( )
157+ {
158+ // Arrange
159+ var sourceGenOptions = new JsonSerializerOptions
160+ {
161+ WriteIndented = true ,
162+ TypeInfoResolver = ResolverModelJsonSerializerContext . Default
163+ } ;
164+ var originalModel = new SourceGeneratorModelWithAttribute
165+ {
166+ DateTimeProperty = new DateTime ( 2023 , 10 , 1 , 12 , 0 , 0 , DateTimeKind . Utc ) ,
167+ NullableDateTimeProperty = new DateTime ( 2023 , 10 , 1 , 12 , 0 , 0 , DateTimeKind . Utc ) ,
168+ DateTimeOffsetProperty = new DateTimeOffset ( 2023 , 10 , 1 , 12 , 0 , 0 , TimeSpan . Zero ) ,
169+ NullableDateTimeOffsetProperty = new DateTimeOffset ( 2023 , 10 , 1 , 12 , 0 , 0 , TimeSpan . Zero ) ,
170+ DateOnlyProperty = new DateOnly ( 2023 , 10 , 1 ) ,
171+ NullableDateOnlyProperty = new DateOnly ( 2023 , 10 , 1 ) ,
172+ TimeOnlyProperty = new TimeOnly ( 14 , 30 , 45 ) ,
173+ NullableTimeOnlyProperty = new TimeOnly ( 14 , 30 , 45 )
174+ } ;
175+ // Expected JSON with DEFAULT formats (not the custom formats from JsonDateTimeFormat attribute)
176+ const string expectedJson = """
177+ {
178+ "DateTimeProperty": "2023-10-01T12:00:00Z",
179+ "NullableDateTimeProperty": "2023-10-01T12:00:00Z",
180+ "DateTimeOffsetProperty": "2023-10-01T12:00:00+00:00",
181+ "NullableDateTimeOffsetProperty": "2023-10-01T12:00:00+00:00",
182+ "DateOnlyProperty": "2023-10-01",
183+ "NullableDateOnlyProperty": "2023-10-01",
184+ "TimeOnlyProperty": "14:30:45",
185+ "NullableTimeOnlyProperty": "14:30:45"
186+ }
187+ """ ;
188+
189+ // Act
190+ var json = JsonSerializer . Serialize ( originalModel , sourceGenOptions ) ;
191+ var deserializedModel = JsonSerializer . Deserialize < SourceGeneratorModelWithAttribute > ( json , sourceGenOptions ) ;
192+
193+ // Assert
194+ Assert . NotNull ( deserializedModel ) ;
195+ Assert . Equal ( originalModel . DateTimeProperty , deserializedModel . DateTimeProperty ) ;
196+ Assert . Equal ( originalModel . NullableDateTimeProperty , deserializedModel . NullableDateTimeProperty ) ;
197+ Assert . Equal ( originalModel . DateTimeOffsetProperty , deserializedModel . DateTimeOffsetProperty ) ;
198+ Assert . Equal ( originalModel . NullableDateTimeOffsetProperty , deserializedModel . NullableDateTimeOffsetProperty ) ;
199+ Assert . Equal ( originalModel . DateOnlyProperty , deserializedModel . DateOnlyProperty ) ;
200+ Assert . Equal ( originalModel . NullableDateOnlyProperty , deserializedModel . NullableDateOnlyProperty ) ;
201+ Assert . Equal ( originalModel . TimeOnlyProperty , deserializedModel . TimeOnlyProperty ) ;
202+ Assert . Equal ( originalModel . NullableTimeOnlyProperty , deserializedModel . NullableTimeOnlyProperty ) ;
203+ Assert . Equal ( expectedJson , json ) ;
204+ }
205+
206+ [ Fact ]
207+ public void SourceGenerator_WithoutResolver_WithFormatAttribute_WithNullValues_DoesNotApplyFormat_UsingOptions ( )
208+ {
209+ // Arrange
210+ var sourceGenOptions = new JsonSerializerOptions
211+ {
212+ WriteIndented = true ,
213+ TypeInfoResolver = ResolverModelJsonSerializerContext . Default
214+ } ;
215+ var originalModel = new SourceGeneratorModelWithAttribute
216+ {
217+ DateTimeProperty = new DateTime ( 2023 , 10 , 1 , 12 , 0 , 0 , DateTimeKind . Utc ) ,
218+ NullableDateTimeProperty = null ,
219+ DateTimeOffsetProperty = new DateTimeOffset ( 2023 , 10 , 1 , 12 , 0 , 0 , TimeSpan . Zero ) ,
220+ NullableDateTimeOffsetProperty = null ,
221+ DateOnlyProperty = new DateOnly ( 2023 , 10 , 1 ) ,
222+ NullableDateOnlyProperty = null ,
223+ TimeOnlyProperty = new TimeOnly ( 14 , 30 , 45 ) ,
224+ NullableTimeOnlyProperty = null
225+ } ;
226+ // Expected JSON with DEFAULT formats (not the custom formats from JsonDateTimeFormat attribute)
227+ const string expectedJson = """
228+ {
229+ "DateTimeProperty": "2023-10-01T12:00:00Z",
230+ "NullableDateTimeProperty": null,
231+ "DateTimeOffsetProperty": "2023-10-01T12:00:00+00:00",
232+ "NullableDateTimeOffsetProperty": null,
233+ "DateOnlyProperty": "2023-10-01",
234+ "NullableDateOnlyProperty": null,
235+ "TimeOnlyProperty": "14:30:45",
236+ "NullableTimeOnlyProperty": null
237+ }
238+ """ ;
239+
240+ // Act
241+ var json = JsonSerializer . Serialize ( originalModel , sourceGenOptions ) ;
242+ var deserializedModel = JsonSerializer . Deserialize < SourceGeneratorModelWithAttribute > ( json , sourceGenOptions ) ;
243+
244+ // Assert
245+ Assert . NotNull ( deserializedModel ) ;
246+ Assert . Equal ( originalModel . DateTimeProperty , deserializedModel . DateTimeProperty ) ;
247+ Assert . Null ( deserializedModel . NullableDateTimeProperty ) ;
248+ Assert . Equal ( originalModel . DateTimeOffsetProperty , deserializedModel . DateTimeOffsetProperty ) ;
249+ Assert . Null ( deserializedModel . NullableDateTimeOffsetProperty ) ;
250+ Assert . Equal ( originalModel . DateOnlyProperty , deserializedModel . DateOnlyProperty ) ;
251+ Assert . Null ( deserializedModel . NullableDateOnlyProperty ) ;
252+ Assert . Equal ( originalModel . TimeOnlyProperty , deserializedModel . TimeOnlyProperty ) ;
253+ Assert . Null ( deserializedModel . NullableTimeOnlyProperty ) ;
254+ Assert . Equal ( expectedJson , json ) ;
255+ }
256+
257+ [ Fact ]
258+ public void SourceGenerator_WithoutResolver_WithFormatAttribute_DoesNotApplyFormat_UsingContext ( )
259+ {
260+ // Arrange
261+ var testModelType = typeof ( SourceGeneratorModelWithAttribute ) ;
262+ var context = ResolverModelJsonSerializerContext . Default ;
263+ var originalModel = new SourceGeneratorModelWithAttribute
264+ {
265+ DateTimeProperty = new DateTime ( 2023 , 10 , 1 , 12 , 0 , 0 , DateTimeKind . Utc ) ,
266+ NullableDateTimeProperty = new DateTime ( 2023 , 10 , 1 , 12 , 0 , 0 , DateTimeKind . Utc ) ,
267+ DateTimeOffsetProperty = new DateTimeOffset ( 2023 , 10 , 1 , 12 , 0 , 0 , TimeSpan . Zero ) ,
268+ NullableDateTimeOffsetProperty = new DateTimeOffset ( 2023 , 10 , 1 , 12 , 0 , 0 , TimeSpan . Zero ) ,
269+ DateOnlyProperty = new DateOnly ( 2023 , 10 , 1 ) ,
270+ NullableDateOnlyProperty = new DateOnly ( 2023 , 10 , 1 ) ,
271+ TimeOnlyProperty = new TimeOnly ( 14 , 30 , 45 ) ,
272+ NullableTimeOnlyProperty = new TimeOnly ( 14 , 30 , 45 )
273+ } ;
274+ // Expected JSON with DEFAULT formats (not the custom formats from JsonDateTimeFormat attribute)
275+ const string expectedJson = """
276+ {
277+ "DateTimeProperty": "2023-10-01T12:00:00Z",
278+ "NullableDateTimeProperty": "2023-10-01T12:00:00Z",
279+ "DateTimeOffsetProperty": "2023-10-01T12:00:00+00:00",
280+ "NullableDateTimeOffsetProperty": "2023-10-01T12:00:00+00:00",
281+ "DateOnlyProperty": "2023-10-01",
282+ "NullableDateOnlyProperty": "2023-10-01",
283+ "TimeOnlyProperty": "14:30:45",
284+ "NullableTimeOnlyProperty": "14:30:45"
285+ }
286+ """ ;
287+
288+ // Act
289+ var json = JsonSerializer . Serialize ( originalModel , testModelType , context ) ;
290+ var deserializedModel = ( SourceGeneratorModelWithAttribute ? ) JsonSerializer . Deserialize ( json , testModelType , context ) ;
291+
292+ // Assert
293+ Assert . NotNull ( deserializedModel ) ;
294+ Assert . Equal ( originalModel . DateTimeProperty , deserializedModel . DateTimeProperty ) ;
295+ Assert . Equal ( originalModel . NullableDateTimeProperty , deserializedModel . NullableDateTimeProperty ) ;
296+ Assert . Equal ( originalModel . DateTimeOffsetProperty , deserializedModel . DateTimeOffsetProperty ) ;
297+ Assert . Equal ( originalModel . NullableDateTimeOffsetProperty , deserializedModel . NullableDateTimeOffsetProperty ) ;
298+ Assert . Equal ( originalModel . DateOnlyProperty , deserializedModel . DateOnlyProperty ) ;
299+ Assert . Equal ( originalModel . NullableDateOnlyProperty , deserializedModel . NullableDateOnlyProperty ) ;
300+ Assert . Equal ( originalModel . TimeOnlyProperty , deserializedModel . TimeOnlyProperty ) ;
301+ Assert . Equal ( originalModel . NullableTimeOnlyProperty , deserializedModel . NullableTimeOnlyProperty ) ;
302+ Assert . Equal ( expectedJson , json ) ;
303+ }
154304}
0 commit comments