|
25 | 25 | import org.openapitools.codegen.CodegenProperty;
|
26 | 26 | import org.openapitools.codegen.DefaultCodegen;
|
27 | 27 | import org.openapitools.codegen.TestUtils;
|
| 28 | +import org.openapitools.codegen.languages.AspNetCoreServerCodegen; |
28 | 29 | import org.openapitools.codegen.languages.CSharpClientCodegen;
|
29 | 30 | import org.testng.Assert;
|
30 | 31 | import org.testng.annotations.Test;
|
@@ -264,6 +265,126 @@ public void nullablePropertyTest() {
|
264 | 265 | Assert.assertTrue(property3.isPrimitiveType);
|
265 | 266 | }
|
266 | 267 |
|
| 268 | + @Test(description = "convert a model with a nullable property without nullable annotation") |
| 269 | + public void nullablePropertyWithoutNullableReferenceTypesTest() { |
| 270 | + final Schema model = new Schema() |
| 271 | + .description("a sample model") |
| 272 | + .addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT).nullable(true)) |
| 273 | + .addProperties("urls", new ArraySchema() |
| 274 | + .items(new StringSchema()).nullable(true)) |
| 275 | + .addProperties("name", new StringSchema().nullable(true)) |
| 276 | + .addProperties("subObject", new Schema().addProperties("name", new StringSchema()).nullable(true)) |
| 277 | + .addRequiredItem("id"); |
| 278 | + final DefaultCodegen codegen = new AspNetCoreServerCodegen(); |
| 279 | + OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model); |
| 280 | + codegen.setOpenAPI(openAPI); |
| 281 | + final CodegenModel cm = codegen.fromModel("sample", model); |
| 282 | + |
| 283 | + Assert.assertEquals(cm.name, "sample"); |
| 284 | + Assert.assertEquals(cm.classname, "Sample"); |
| 285 | + Assert.assertEquals(cm.description, "a sample model"); |
| 286 | + Assert.assertEquals(cm.vars.size(), 4); |
| 287 | + |
| 288 | + final CodegenProperty property1 = cm.vars.get(0); |
| 289 | + Assert.assertEquals(property1.baseName, "id"); |
| 290 | + Assert.assertEquals(property1.dataType, "long?"); |
| 291 | + Assert.assertEquals(property1.name, "Id"); |
| 292 | + Assert.assertNull(property1.defaultValue); |
| 293 | + Assert.assertEquals(property1.baseType, "long?"); |
| 294 | + Assert.assertTrue(property1.required); |
| 295 | + Assert.assertTrue(property1.isPrimitiveType); |
| 296 | + |
| 297 | + final CodegenProperty property2 = cm.vars.get(1); |
| 298 | + Assert.assertEquals(property2.baseName, "urls"); |
| 299 | + Assert.assertEquals(property2.dataType, "List<string>"); |
| 300 | + Assert.assertEquals(property2.name, "Urls"); |
| 301 | + Assert.assertNull(property2.defaultValue); |
| 302 | + Assert.assertEquals(property2.baseType, "List"); |
| 303 | + Assert.assertEquals(property2.containerType, "array"); |
| 304 | + Assert.assertFalse(property2.required); |
| 305 | + Assert.assertTrue(property2.isPrimitiveType); |
| 306 | + Assert.assertTrue(property2.isContainer); |
| 307 | + |
| 308 | + final CodegenProperty property3 = cm.vars.get(2); |
| 309 | + Assert.assertEquals(property3.baseName, "name"); |
| 310 | + Assert.assertEquals(property3.dataType, "string"); |
| 311 | + Assert.assertEquals(property3.name, "Name"); |
| 312 | + Assert.assertNull(property3.defaultValue); |
| 313 | + Assert.assertEquals(property3.baseType, "string"); |
| 314 | + Assert.assertFalse(property3.required); |
| 315 | + Assert.assertTrue(property3.isPrimitiveType); |
| 316 | + |
| 317 | + final CodegenProperty property4 = cm.vars.get(3); |
| 318 | + Assert.assertEquals(property4.baseName, "subObject"); |
| 319 | + Assert.assertEquals(property4.dataType, "Object"); |
| 320 | + Assert.assertEquals(property4.name, "SubObject"); |
| 321 | + Assert.assertNull(property4.defaultValue); |
| 322 | + Assert.assertEquals(property4.baseType, "Object"); |
| 323 | + Assert.assertFalse(property4.required); |
| 324 | + Assert.assertTrue(property4.isPrimitiveType); |
| 325 | + } |
| 326 | + |
| 327 | + @Test(description = "convert a model with a nullable property using nullable annotation") |
| 328 | + public void nullablePropertyWithNullableReferenceTypesTest() { |
| 329 | + final Schema model = new Schema() |
| 330 | + .description("a sample model") |
| 331 | + .addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT).nullable(true)) |
| 332 | + .addProperties("urls", new ArraySchema() |
| 333 | + .items(new StringSchema()).nullable(true)) |
| 334 | + .addProperties("name", new StringSchema().nullable(true)) |
| 335 | + .addProperties("subObject", new Schema().addProperties("name", new StringSchema()).nullable(true)) |
| 336 | + .addRequiredItem("id"); |
| 337 | + final DefaultCodegen codegen = new AspNetCoreServerCodegen(); |
| 338 | + codegen.additionalProperties().put(AspNetCoreServerCodegen.NULLABLE_REFERENCE_TYPES, true); |
| 339 | + codegen.processOpts(); |
| 340 | + OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model); |
| 341 | + codegen.setOpenAPI(openAPI); |
| 342 | + final CodegenModel cm = codegen.fromModel("sample", model); |
| 343 | + |
| 344 | + Assert.assertEquals(cm.name, "sample"); |
| 345 | + Assert.assertEquals(cm.classname, "Sample"); |
| 346 | + Assert.assertEquals(cm.description, "a sample model"); |
| 347 | + Assert.assertEquals(cm.vars.size(), 4); |
| 348 | + |
| 349 | + final CodegenProperty property1 = cm.vars.get(0); |
| 350 | + Assert.assertEquals(property1.baseName, "id"); |
| 351 | + Assert.assertEquals(property1.dataType, "long?"); |
| 352 | + Assert.assertEquals(property1.name, "Id"); |
| 353 | + Assert.assertNull(property1.defaultValue); |
| 354 | + Assert.assertEquals(property1.baseType, "long?"); |
| 355 | + Assert.assertTrue(property1.required); |
| 356 | + Assert.assertTrue(property1.isPrimitiveType); |
| 357 | + |
| 358 | + final CodegenProperty property2 = cm.vars.get(1); |
| 359 | + Assert.assertEquals(property2.baseName, "urls"); |
| 360 | + Assert.assertEquals(property2.dataType, "List<string>"); |
| 361 | + Assert.assertEquals(property2.name, "Urls"); |
| 362 | + Assert.assertNull(property2.defaultValue); |
| 363 | + Assert.assertEquals(property2.baseType, "List?"); |
| 364 | + Assert.assertEquals(property2.containerType, "array"); |
| 365 | + Assert.assertFalse(property2.required); |
| 366 | + Assert.assertTrue(property2.isPrimitiveType); |
| 367 | + Assert.assertTrue(property2.isContainer); |
| 368 | + |
| 369 | + final CodegenProperty property3 = cm.vars.get(2); |
| 370 | + Assert.assertEquals(property3.baseName, "name"); |
| 371 | + Assert.assertEquals(property3.dataType, "string?"); |
| 372 | + Assert.assertEquals(property3.name, "Name"); |
| 373 | + Assert.assertNull(property3.defaultValue); |
| 374 | + Assert.assertEquals(property3.baseType, "string?"); |
| 375 | + Assert.assertFalse(property3.required); |
| 376 | + Assert.assertFalse(property3.isPrimitiveType); |
| 377 | + |
| 378 | + final CodegenProperty property4 = cm.vars.get(3); |
| 379 | + Assert.assertEquals(property4.baseName, "subObject"); |
| 380 | + Assert.assertEquals(property4.dataType, "Object?"); |
| 381 | + Assert.assertEquals(property4.name, "SubObject"); |
| 382 | + Assert.assertNull(property4.defaultValue); |
| 383 | + Assert.assertEquals(property4.baseType, "Object?"); |
| 384 | + Assert.assertFalse(property4.required); |
| 385 | + Assert.assertFalse(property4.isPrimitiveType); |
| 386 | + } |
| 387 | + |
267 | 388 | @Test(description = "convert a model with list property")
|
268 | 389 | public void listPropertyTest() {
|
269 | 390 | final Schema model = new Schema()
|
|
0 commit comments