diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/AdditionalpropertiesAllowsASchemaWhichShouldValidate.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/AdditionalpropertiesAllowsASchemaWhichShouldValidate.md index 812f528e5a7..f8d8493dc3b 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/AdditionalpropertiesAllowsASchemaWhichShouldValidate.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/AdditionalpropertiesAllowsASchemaWhichShouldValidate.md @@ -59,33 +59,33 @@ AdditionalpropertiesAllowsASchemaWhichShouldValidate.AdditionalpropertiesAllowsA ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [AdditionalpropertiesAllowsASchemaWhichShouldValidateMap](#additionalpropertiesallowsaschemawhichshouldvalidatemap) | validate([Map](#additionalpropertiesallowsaschemawhichshouldvalidatemapinput) arg, SchemaConfiguration configuration) | +| static [AdditionalpropertiesAllowsASchemaWhichShouldValidateMap](#additionalpropertiesallowsaschemawhichshouldvalidatemap) | validate([Map](#additionalpropertiesallowsaschemawhichshouldvalidatemapinput) arg, SchemaConfiguration configuration) | ## AdditionalpropertiesAllowsASchemaWhichShouldValidateMapInput public class AdditionalpropertiesAllowsASchemaWhichShouldValidateMapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type ## Input Map Keys | Key | Type | Description | Notes | | --- | ---- | ------------ | ----- | -| **foo** | Object | | [optional] | -| **bar** | Object | | [optional] | +| **foo** | @Nullable Object | | [optional] | +| **bar** | @Nullable Object | | [optional] | | **anyStringName** | boolean | any string name can be used but the value must be the correct type | [optional] | ## AdditionalpropertiesAllowsASchemaWhichShouldValidateMap public static class AdditionalpropertiesAllowsASchemaWhichShouldValidateMap
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [AdditionalpropertiesAllowsASchemaWhichShouldValidateMap](#additionalpropertiesallowsaschemawhichshouldvalidatemap) | of([Map](#additionalpropertiesallowsaschemawhichshouldvalidatemapinput) arg, SchemaConfiguration configuration) | -| Object | foo()
[optional] | -| Object | bar()
[optional] | +| static [AdditionalpropertiesAllowsASchemaWhichShouldValidateMap](#additionalpropertiesallowsaschemawhichshouldvalidatemap) | of([Map](#additionalpropertiesallowsaschemawhichshouldvalidatemapinput) arg, SchemaConfiguration configuration) | +| @Nullable Object | foo()
[optional] | +| @Nullable Object | bar()
[optional] | | boolean | getAdditionalProperty(String name)
provides type safety for additional properties | ## Bar diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/AdditionalpropertiesAreAllowedByDefault.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/AdditionalpropertiesAreAllowedByDefault.md index aee08c84d47..445e3d72a1c 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/AdditionalpropertiesAreAllowedByDefault.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/AdditionalpropertiesAreAllowedByDefault.md @@ -39,34 +39,34 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [AdditionalpropertiesAreAllowedByDefaultMap](#additionalpropertiesareallowedbydefaultmap) | validate([Map](#additionalpropertiesareallowedbydefaultmapinput) arg, SchemaConfiguration configuration) | +| static [AdditionalpropertiesAreAllowedByDefaultMap](#additionalpropertiesareallowedbydefaultmap) | validate([Map](#additionalpropertiesareallowedbydefaultmapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## AdditionalpropertiesAreAllowedByDefaultMapInput public class AdditionalpropertiesAreAllowedByDefaultMapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type ## Input Map Keys | Key | Type | Description | Notes | | --- | ---- | ------------ | ----- | -| **foo** | Object | | [optional] | -| **bar** | Object | | [optional] | +| **foo** | @Nullable Object | | [optional] | +| **bar** | @Nullable Object | | [optional] | | **anyStringName** | Object | any string name can be used but the value must be the correct type | [optional] | ## AdditionalpropertiesAreAllowedByDefaultMap public static class AdditionalpropertiesAreAllowedByDefaultMap
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [AdditionalpropertiesAreAllowedByDefaultMap](#additionalpropertiesareallowedbydefaultmap) | of([Map](#additionalpropertiesareallowedbydefaultmapinput) arg, SchemaConfiguration configuration) | -| Object | foo()
[optional] | -| Object | bar()
[optional] | +| static [AdditionalpropertiesAreAllowedByDefaultMap](#additionalpropertiesareallowedbydefaultmap) | of([Map](#additionalpropertiesareallowedbydefaultmapinput) arg, SchemaConfiguration configuration) | +| @Nullable Object | foo()
[optional] | +| @Nullable Object | bar()
[optional] | | Object | getAdditionalProperty(String name)
provides type safety for additional properties | ## Bar diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/AdditionalpropertiesShouldNotLookInApplicators.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/AdditionalpropertiesShouldNotLookInApplicators.md index 771dcf155a2..6230a58936e 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/AdditionalpropertiesShouldNotLookInApplicators.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/AdditionalpropertiesShouldNotLookInApplicators.md @@ -90,32 +90,32 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [Schema0Map](#schema0map) | validate([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | +| static [Schema0Map](#schema0map) | validate([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## Schema0MapInput public class Schema0MapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type ## Input Map Keys | Key | Type | Description | Notes | | --- | ---- | ------------ | ----- | -| **foo** | Object | | [optional] | +| **foo** | @Nullable Object | | [optional] | | **anyStringName** | Object | any string name can be used but the value must be the correct type | [optional] | ## Schema0Map public static class Schema0Map
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [Schema0Map](#schema0map) | of([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | -| Object | foo()
[optional] | +| static [Schema0Map](#schema0map) | of([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | +| @Nullable Object | foo()
[optional] | | Object | getAdditionalProperty(String name)
provides type safety for additional properties | ## Foo diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/Allof.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/Allof.md index f3fd5969d50..0131c2ac187 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/Allof.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/Allof.md @@ -68,12 +68,12 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [Schema1Map](#schema1map) | validate([Map](#schema1mapinput) arg, SchemaConfiguration configuration) | +| static [Schema1Map](#schema1map) | validate([Map](#schema1mapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## Schema1MapInput public class Schema1MapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type @@ -85,14 +85,14 @@ A class that builds the Map input type ## Schema1Map public static class Schema1Map
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [Schema1Map](#schema1map) | of([Map](#schema1mapinput) arg, SchemaConfiguration configuration) | +| static [Schema1Map](#schema1map) | of([Map](#schema1mapinput) arg, SchemaConfiguration configuration) | | String | foo()
| | Object | getAdditionalProperty(String name)
provides type safety for additional properties | @@ -128,12 +128,12 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [Schema0Map](#schema0map) | validate([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | +| static [Schema0Map](#schema0map) | validate([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## Schema0MapInput public class Schema0MapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type @@ -145,14 +145,14 @@ A class that builds the Map input type ## Schema0Map public static class Schema0Map
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [Schema0Map](#schema0map) | of([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | +| static [Schema0Map](#schema0map) | of([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | | long | bar()
| | Object | getAdditionalProperty(String name)
provides type safety for additional properties | diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/AllofWithBaseSchema.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/AllofWithBaseSchema.md index 4f5ca1116cd..846e1926309 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/AllofWithBaseSchema.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/AllofWithBaseSchema.md @@ -48,12 +48,12 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [AllofWithBaseSchemaMap](#allofwithbaseschemamap) | validate([Map](#allofwithbaseschemamapinput) arg, SchemaConfiguration configuration) | +| static [AllofWithBaseSchemaMap](#allofwithbaseschemamap) | validate([Map](#allofwithbaseschemamapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## AllofWithBaseSchemaMapInput public class AllofWithBaseSchemaMapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type @@ -65,14 +65,14 @@ A class that builds the Map input type ## AllofWithBaseSchemaMap public static class AllofWithBaseSchemaMap
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [AllofWithBaseSchemaMap](#allofwithbaseschemamap) | of([Map](#allofwithbaseschemamapinput) arg, SchemaConfiguration configuration) | +| static [AllofWithBaseSchemaMap](#allofwithbaseschemamap) | of([Map](#allofwithbaseschemamapinput) arg, SchemaConfiguration configuration) | | long | bar()
| | Object | getAdditionalProperty(String name)
provides type safety for additional properties | @@ -108,12 +108,12 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [Schema1Map](#schema1map) | validate([Map](#schema1mapinput) arg, SchemaConfiguration configuration) | +| static [Schema1Map](#schema1map) | validate([Map](#schema1mapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## Schema1MapInput public class Schema1MapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type @@ -125,14 +125,14 @@ A class that builds the Map input type ## Schema1Map public static class Schema1Map
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [Schema1Map](#schema1map) | of([Map](#schema1mapinput) arg, SchemaConfiguration configuration) | +| static [Schema1Map](#schema1map) | of([Map](#schema1mapinput) arg, SchemaConfiguration configuration) | | Void | baz()
| | Object | getAdditionalProperty(String name)
provides type safety for additional properties | @@ -168,12 +168,12 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [Schema0Map](#schema0map) | validate([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | +| static [Schema0Map](#schema0map) | validate([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## Schema0MapInput public class Schema0MapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type @@ -185,14 +185,14 @@ A class that builds the Map input type ## Schema0Map public static class Schema0Map
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [Schema0Map](#schema0map) | of([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | +| static [Schema0Map](#schema0map) | of([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | | String | foo()
| | Object | getAdditionalProperty(String name)
provides type safety for additional properties | diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/AnyofComplexTypes.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/AnyofComplexTypes.md index 01a619c4022..755eebe7647 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/AnyofComplexTypes.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/AnyofComplexTypes.md @@ -68,12 +68,12 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [Schema1Map](#schema1map) | validate([Map](#schema1mapinput) arg, SchemaConfiguration configuration) | +| static [Schema1Map](#schema1map) | validate([Map](#schema1mapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## Schema1MapInput public class Schema1MapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type @@ -85,14 +85,14 @@ A class that builds the Map input type ## Schema1Map public static class Schema1Map
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [Schema1Map](#schema1map) | of([Map](#schema1mapinput) arg, SchemaConfiguration configuration) | +| static [Schema1Map](#schema1map) | of([Map](#schema1mapinput) arg, SchemaConfiguration configuration) | | String | foo()
| | Object | getAdditionalProperty(String name)
provides type safety for additional properties | @@ -128,12 +128,12 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [Schema0Map](#schema0map) | validate([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | +| static [Schema0Map](#schema0map) | validate([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## Schema0MapInput public class Schema0MapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type @@ -145,14 +145,14 @@ A class that builds the Map input type ## Schema0Map public static class Schema0Map
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [Schema0Map](#schema0map) | of([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | +| static [Schema0Map](#schema0map) | of([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | | long | bar()
| | Object | getAdditionalProperty(String name)
provides type safety for additional properties | diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/ArrayTypeMatchesArrays.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/ArrayTypeMatchesArrays.md index eadf9051449..fd2323f5204 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/ArrayTypeMatchesArrays.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/ArrayTypeMatchesArrays.md @@ -56,29 +56,29 @@ ArrayTypeMatchesArrays.ArrayTypeMatchesArraysList validatedPayload = ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [ArrayTypeMatchesArraysList](#arraytypematchesarrayslist) | validate([List](#arraytypematchesarrayslistinput) arg, SchemaConfiguration configuration) | +| static [ArrayTypeMatchesArraysList](#arraytypematchesarrayslist) | validate([List<@Nullable Object>](#arraytypematchesarrayslistinput) arg, SchemaConfiguration configuration) | ## ArrayTypeMatchesArraysListInput public class ArrayTypeMatchesArraysListInput
-builder for `List` +builder for `List<@Nullable Object>` A class that builds the List input type ## Input List Items List Item Type | Description | Notes -------------------- | ------------- | ------------- -Object | | +@Nullable Object | | ## ArrayTypeMatchesArraysList public class ArrayTypeMatchesArraysList
-extends `FrozenList` +extends `FrozenList<@Nullable Object>` A class to store validated List payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [ArrayTypeMatchesArraysList](#arraytypematchesarrayslist) | of([List](#arraytypematchesarrayslistinput) arg, SchemaConfiguration configuration) | +| static [ArrayTypeMatchesArraysList](#arraytypematchesarrayslist) | of([List<@Nullable Object>](#arraytypematchesarrayslistinput) arg, SchemaConfiguration configuration) | ## Items public static class Items
diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/EnumsInProperties.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/EnumsInProperties.md index 1791e90253b..e76a01bdb13 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/EnumsInProperties.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/EnumsInProperties.md @@ -66,11 +66,11 @@ EnumsInProperties.EnumsInPropertiesMap validatedPayload = ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [EnumsInPropertiesMap](#enumsinpropertiesmap) | validate([Map](#enumsinpropertiesmapinput) arg, SchemaConfiguration configuration) | +| static [EnumsInPropertiesMap](#enumsinpropertiesmap) | validate([Map](#enumsinpropertiesmapinput) arg, SchemaConfiguration configuration) | ## EnumsInPropertiesMapInput public class EnumsInPropertiesMapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type @@ -83,14 +83,14 @@ A class that builds the Map input type ## EnumsInPropertiesMap public static class EnumsInPropertiesMap
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [EnumsInPropertiesMap](#enumsinpropertiesmap) | of([Map](#enumsinpropertiesmapinput) arg, SchemaConfiguration configuration) | +| static [EnumsInPropertiesMap](#enumsinpropertiesmap) | of([Map](#enumsinpropertiesmapinput) arg, SchemaConfiguration configuration) | | String | bar()
must be one of ["bar"] | | String | foo()
[optional] must be one of ["foo"] | | Object | getAdditionalProperty(String name)
provides type safety for additional properties | diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/ForbiddenProperty.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/ForbiddenProperty.md index cf51444902a..3a91701112d 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/ForbiddenProperty.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/ForbiddenProperty.md @@ -38,32 +38,32 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [ForbiddenPropertyMap](#forbiddenpropertymap) | validate([Map](#forbiddenpropertymapinput) arg, SchemaConfiguration configuration) | +| static [ForbiddenPropertyMap](#forbiddenpropertymap) | validate([Map](#forbiddenpropertymapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## ForbiddenPropertyMapInput public class ForbiddenPropertyMapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type ## Input Map Keys | Key | Type | Description | Notes | | --- | ---- | ------------ | ----- | -| **foo** | Object | | [optional] | +| **foo** | @Nullable Object | | [optional] | | **anyStringName** | Object | any string name can be used but the value must be the correct type | [optional] | ## ForbiddenPropertyMap public static class ForbiddenPropertyMap
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [ForbiddenPropertyMap](#forbiddenpropertymap) | of([Map](#forbiddenpropertymapinput) arg, SchemaConfiguration configuration) | -| Object | foo()
[optional] | +| static [ForbiddenPropertyMap](#forbiddenpropertymap) | of([Map](#forbiddenpropertymapinput) arg, SchemaConfiguration configuration) | +| @Nullable Object | foo()
[optional] | | Object | getAdditionalProperty(String name)
provides type safety for additional properties | ## Foo diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/InvalidStringValueForDefault.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/InvalidStringValueForDefault.md index 82d432b975b..35d5f79b0be 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/InvalidStringValueForDefault.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/InvalidStringValueForDefault.md @@ -38,12 +38,12 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [InvalidStringValueForDefaultMap](#invalidstringvaluefordefaultmap) | validate([Map](#invalidstringvaluefordefaultmapinput) arg, SchemaConfiguration configuration) | +| static [InvalidStringValueForDefaultMap](#invalidstringvaluefordefaultmap) | validate([Map](#invalidstringvaluefordefaultmapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## InvalidStringValueForDefaultMapInput public class InvalidStringValueForDefaultMapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type @@ -55,14 +55,14 @@ A class that builds the Map input type ## InvalidStringValueForDefaultMap public static class InvalidStringValueForDefaultMap
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [InvalidStringValueForDefaultMap](#invalidstringvaluefordefaultmap) | of([Map](#invalidstringvaluefordefaultmapinput) arg, SchemaConfiguration configuration) | +| static [InvalidStringValueForDefaultMap](#invalidstringvaluefordefaultmap) | of([Map](#invalidstringvaluefordefaultmapinput) arg, SchemaConfiguration configuration) | | String | bar()
[optional] if omitted the server will use the default value of bad | | Object | getAdditionalProperty(String name)
provides type safety for additional properties | diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/NotMoreComplexSchema.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/NotMoreComplexSchema.md index 158c174dc5b..6daf781dfb2 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/NotMoreComplexSchema.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/NotMoreComplexSchema.md @@ -85,11 +85,11 @@ NotMoreComplexSchema.NotMap validatedPayload = ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [NotMap](#notmap) | validate([Map](#notmapinput) arg, SchemaConfiguration configuration) | +| static [NotMap](#notmap) | validate([Map](#notmapinput) arg, SchemaConfiguration configuration) | ## NotMapInput public class NotMapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type @@ -101,14 +101,14 @@ A class that builds the Map input type ## NotMap public static class NotMap
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [NotMap](#notmap) | of([Map](#notmapinput) arg, SchemaConfiguration configuration) | +| static [NotMap](#notmap) | of([Map](#notmapinput) arg, SchemaConfiguration configuration) | | String | foo()
[optional] | | Object | getAdditionalProperty(String name)
provides type safety for additional properties | diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/ObjectPropertiesValidation.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/ObjectPropertiesValidation.md index 35aef63c36a..289def46da2 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/ObjectPropertiesValidation.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/ObjectPropertiesValidation.md @@ -39,12 +39,12 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [ObjectPropertiesValidationMap](#objectpropertiesvalidationmap) | validate([Map](#objectpropertiesvalidationmapinput) arg, SchemaConfiguration configuration) | +| static [ObjectPropertiesValidationMap](#objectpropertiesvalidationmap) | validate([Map](#objectpropertiesvalidationmapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## ObjectPropertiesValidationMapInput public class ObjectPropertiesValidationMapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type @@ -57,14 +57,14 @@ A class that builds the Map input type ## ObjectPropertiesValidationMap public static class ObjectPropertiesValidationMap
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [ObjectPropertiesValidationMap](#objectpropertiesvalidationmap) | of([Map](#objectpropertiesvalidationmapinput) arg, SchemaConfiguration configuration) | +| static [ObjectPropertiesValidationMap](#objectpropertiesvalidationmap) | of([Map](#objectpropertiesvalidationmapinput) arg, SchemaConfiguration configuration) | | long | foo()
[optional] | | String | bar()
[optional] | | Object | getAdditionalProperty(String name)
provides type safety for additional properties | diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/OneofComplexTypes.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/OneofComplexTypes.md index c054597d115..dc5c1ec536d 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/OneofComplexTypes.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/OneofComplexTypes.md @@ -68,12 +68,12 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [Schema1Map](#schema1map) | validate([Map](#schema1mapinput) arg, SchemaConfiguration configuration) | +| static [Schema1Map](#schema1map) | validate([Map](#schema1mapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## Schema1MapInput public class Schema1MapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type @@ -85,14 +85,14 @@ A class that builds the Map input type ## Schema1Map public static class Schema1Map
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [Schema1Map](#schema1map) | of([Map](#schema1mapinput) arg, SchemaConfiguration configuration) | +| static [Schema1Map](#schema1map) | of([Map](#schema1mapinput) arg, SchemaConfiguration configuration) | | String | foo()
| | Object | getAdditionalProperty(String name)
provides type safety for additional properties | @@ -128,12 +128,12 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [Schema0Map](#schema0map) | validate([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | +| static [Schema0Map](#schema0map) | validate([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## Schema0MapInput public class Schema0MapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type @@ -145,14 +145,14 @@ A class that builds the Map input type ## Schema0Map public static class Schema0Map
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [Schema0Map](#schema0map) | of([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | +| static [Schema0Map](#schema0map) | of([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | | long | bar()
| | Object | getAdditionalProperty(String name)
provides type safety for additional properties | diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/OneofWithRequired.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/OneofWithRequired.md index 392d3468ed4..ccc4df8ef33 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/OneofWithRequired.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/OneofWithRequired.md @@ -58,34 +58,34 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [Schema1Map](#schema1map) | validate([Map](#schema1mapinput) arg, SchemaConfiguration configuration) | +| static [Schema1Map](#schema1map) | validate([Map](#schema1mapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## Schema1MapInput public class Schema1MapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type ## Input Map Keys | Key | Type | Description | Notes | | --- | ---- | ------------ | ----- | -| **baz** | Object | | | -| **foo** | Object | | | +| **baz** | @Nullable Object | | | +| **foo** | @Nullable Object | | | | **anyStringName** | Object | any string name can be used but the value must be the correct type | [optional] | ## Schema1Map public static class Schema1Map
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [Schema1Map](#schema1map) | of([Map](#schema1mapinput) arg, SchemaConfiguration configuration) | -| Object | baz()
| -| Object | foo()
| +| static [Schema1Map](#schema1map) | of([Map](#schema1mapinput) arg, SchemaConfiguration configuration) | +| @Nullable Object | baz()
| +| @Nullable Object | foo()
| | Object | getAdditionalProperty(String name)
provides type safety for additional properties | ## Schema0 @@ -109,34 +109,34 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [Schema0Map](#schema0map) | validate([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | +| static [Schema0Map](#schema0map) | validate([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## Schema0MapInput public class Schema0MapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type ## Input Map Keys | Key | Type | Description | Notes | | --- | ---- | ------------ | ----- | -| **bar** | Object | | | -| **foo** | Object | | | +| **bar** | @Nullable Object | | | +| **foo** | @Nullable Object | | | | **anyStringName** | Object | any string name can be used but the value must be the correct type | [optional] | ## Schema0Map public static class Schema0Map
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [Schema0Map](#schema0map) | of([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | -| Object | bar()
| -| Object | foo()
| +| static [Schema0Map](#schema0map) | of([Map](#schema0mapinput) arg, SchemaConfiguration configuration) | +| @Nullable Object | bar()
| +| @Nullable Object | foo()
| | Object | getAdditionalProperty(String name)
provides type safety for additional properties | [[Back to top]](#top) [[Back to Component Schemas]](../../../README.md#Component-Schemas) [[Back to README]](../../../README.md) diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/PropertiesWithEscapedCharacters.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/PropertiesWithEscapedCharacters.md index 3f3747836de..b022ff1fa96 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/PropertiesWithEscapedCharacters.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/PropertiesWithEscapedCharacters.md @@ -43,12 +43,12 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [PropertiesWithEscapedCharactersMap](#propertieswithescapedcharactersmap) | validate([Map](#propertieswithescapedcharactersmapinput) arg, SchemaConfiguration configuration) | +| static [PropertiesWithEscapedCharactersMap](#propertieswithescapedcharactersmap) | validate([Map](#propertieswithescapedcharactersmapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## PropertiesWithEscapedCharactersMapInput public class PropertiesWithEscapedCharactersMapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type @@ -65,15 +65,15 @@ A class that builds the Map input type ## PropertiesWithEscapedCharactersMap public static class PropertiesWithEscapedCharactersMap
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [PropertiesWithEscapedCharactersMap](#propertieswithescapedcharactersmap) | of([Map](#propertieswithescapedcharactersmapinput) arg, SchemaConfiguration configuration) | -| Object | get(String key)
This schema has invalid Java names so this method must be used when you access instance["foo\nbar"], instance["foo\"bar"], instance["foo\\bar"], instance["foo\rbar"], instance["foo\tbar"], instance["foo\fbar"], | +| static [PropertiesWithEscapedCharactersMap](#propertieswithescapedcharactersmap) | of([Map](#propertieswithescapedcharactersmapinput) arg, SchemaConfiguration configuration) | +| @Nullable Object | get(String key)
This schema has invalid Java names so this method must be used when you access instance["foo\nbar"], instance["foo\"bar"], instance["foo\\bar"], instance["foo\rbar"], instance["foo\tbar"], instance["foo\fbar"], | | Object | getAdditionalProperty(String name)
provides type safety for additional properties | ## Foofbar diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/PropertyNamedRefThatIsNotAReference.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/PropertyNamedRefThatIsNotAReference.md index b9c77d04ba6..25046539ee5 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/PropertyNamedRefThatIsNotAReference.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/PropertyNamedRefThatIsNotAReference.md @@ -38,12 +38,12 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [PropertyNamedRefThatIsNotAReferenceMap](#propertynamedrefthatisnotareferencemap) | validate([Map](#propertynamedrefthatisnotareferencemapinput) arg, SchemaConfiguration configuration) | +| static [PropertyNamedRefThatIsNotAReferenceMap](#propertynamedrefthatisnotareferencemap) | validate([Map](#propertynamedrefthatisnotareferencemapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## PropertyNamedRefThatIsNotAReferenceMapInput public class PropertyNamedRefThatIsNotAReferenceMapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type @@ -55,15 +55,15 @@ A class that builds the Map input type ## PropertyNamedRefThatIsNotAReferenceMap public static class PropertyNamedRefThatIsNotAReferenceMap
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [PropertyNamedRefThatIsNotAReferenceMap](#propertynamedrefthatisnotareferencemap) | of([Map](#propertynamedrefthatisnotareferencemapinput) arg, SchemaConfiguration configuration) | -| Object | get(String key)
This schema has invalid Java names so this method must be used when you access instance["$ref"], | +| static [PropertyNamedRefThatIsNotAReferenceMap](#propertynamedrefthatisnotareferencemap) | of([Map](#propertynamedrefthatisnotareferencemapinput) arg, SchemaConfiguration configuration) | +| @Nullable Object | get(String key)
This schema has invalid Java names so this method must be used when you access instance["$ref"], | | Object | getAdditionalProperty(String name)
provides type safety for additional properties | ## Ref diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/RefInAdditionalproperties.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/RefInAdditionalproperties.md index 038081b3c0a..1c4b0692c21 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/RefInAdditionalproperties.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/RefInAdditionalproperties.md @@ -55,29 +55,29 @@ RefInAdditionalproperties.RefInAdditionalpropertiesMap validatedPayload = ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [RefInAdditionalpropertiesMap](#refinadditionalpropertiesmap) | validate([Map](#refinadditionalpropertiesmapinput) arg, SchemaConfiguration configuration) | +| static [RefInAdditionalpropertiesMap](#refinadditionalpropertiesmap) | validate([Map](#refinadditionalpropertiesmapinput) arg, SchemaConfiguration configuration) | ## RefInAdditionalpropertiesMapInput public class RefInAdditionalpropertiesMapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type ## Input Map Keys | Key | Type | Description | Notes | | --- | ---- | ------------ | ----- | -| **anyStringName** | Object | any string name can be used but the value must be the correct type | [optional] | +| **anyStringName** | @Nullable Object | any string name can be used but the value must be the correct type | [optional] | ## RefInAdditionalpropertiesMap public static class RefInAdditionalpropertiesMap
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [RefInAdditionalpropertiesMap](#refinadditionalpropertiesmap) | of([Map](#refinadditionalpropertiesmapinput) arg, SchemaConfiguration configuration) | -| Object | getAdditionalProperty(String name)
provides type safety for additional properties | +| static [RefInAdditionalpropertiesMap](#refinadditionalpropertiesmap) | of([Map](#refinadditionalpropertiesmapinput) arg, SchemaConfiguration configuration) | +| @Nullable Object | getAdditionalProperty(String name)
provides type safety for additional properties | [[Back to top]](#top) [[Back to Component Schemas]](../../../README.md#Component-Schemas) [[Back to README]](../../../README.md) diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/RefInItems.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/RefInItems.md index 1857920b2d9..2b61c9b9a0a 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/RefInItems.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/RefInItems.md @@ -55,28 +55,28 @@ RefInItems.RefInItemsList validatedPayload = ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [RefInItemsList](#refinitemslist) | validate([List](#refinitemslistinput) arg, SchemaConfiguration configuration) | +| static [RefInItemsList](#refinitemslist) | validate([List<@Nullable Object>](#refinitemslistinput) arg, SchemaConfiguration configuration) | ## RefInItemsListInput public class RefInItemsListInput
-builder for `List` +builder for `List<@Nullable Object>` A class that builds the List input type ## Input List Items List Item Type | Description | Notes -------------------- | ------------- | ------------- -Object | | +@Nullable Object | | ## RefInItemsList public class RefInItemsList
-extends `FrozenList` +extends `FrozenList<@Nullable Object>` A class to store validated List payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [RefInItemsList](#refinitemslist) | of([List](#refinitemslistinput) arg, SchemaConfiguration configuration) | +| static [RefInItemsList](#refinitemslist) | of([List<@Nullable Object>](#refinitemslistinput) arg, SchemaConfiguration configuration) | [[Back to top]](#top) [[Back to Component Schemas]](../../../README.md#Component-Schemas) [[Back to README]](../../../README.md) diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/RefInProperty.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/RefInProperty.md index 52c955f04e9..ce4bff04641 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/RefInProperty.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/RefInProperty.md @@ -37,32 +37,32 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [RefInPropertyMap](#refinpropertymap) | validate([Map](#refinpropertymapinput) arg, SchemaConfiguration configuration) | +| static [RefInPropertyMap](#refinpropertymap) | validate([Map](#refinpropertymapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## RefInPropertyMapInput public class RefInPropertyMapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type ## Input Map Keys | Key | Type | Description | Notes | | --- | ---- | ------------ | ----- | -| **a** | Object | | [optional] | +| **a** | @Nullable Object | | [optional] | | **anyStringName** | Object | any string name can be used but the value must be the correct type | [optional] | ## RefInPropertyMap public static class RefInPropertyMap
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [RefInPropertyMap](#refinpropertymap) | of([Map](#refinpropertymapinput) arg, SchemaConfiguration configuration) | -| Object | a()
[optional] | +| static [RefInPropertyMap](#refinpropertymap) | of([Map](#refinpropertymapinput) arg, SchemaConfiguration configuration) | +| @Nullable Object | a()
[optional] | | Object | getAdditionalProperty(String name)
provides type safety for additional properties | [[Back to top]](#top) [[Back to Component Schemas]](../../../README.md#Component-Schemas) [[Back to README]](../../../README.md) diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/RequiredDefaultValidation.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/RequiredDefaultValidation.md index e36f45add70..1b30185fa24 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/RequiredDefaultValidation.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/RequiredDefaultValidation.md @@ -38,32 +38,32 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [RequiredDefaultValidationMap](#requireddefaultvalidationmap) | validate([Map](#requireddefaultvalidationmapinput) arg, SchemaConfiguration configuration) | +| static [RequiredDefaultValidationMap](#requireddefaultvalidationmap) | validate([Map](#requireddefaultvalidationmapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## RequiredDefaultValidationMapInput public class RequiredDefaultValidationMapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type ## Input Map Keys | Key | Type | Description | Notes | | --- | ---- | ------------ | ----- | -| **foo** | Object | | [optional] | +| **foo** | @Nullable Object | | [optional] | | **anyStringName** | Object | any string name can be used but the value must be the correct type | [optional] | ## RequiredDefaultValidationMap public static class RequiredDefaultValidationMap
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [RequiredDefaultValidationMap](#requireddefaultvalidationmap) | of([Map](#requireddefaultvalidationmapinput) arg, SchemaConfiguration configuration) | -| Object | foo()
[optional] | +| static [RequiredDefaultValidationMap](#requireddefaultvalidationmap) | of([Map](#requireddefaultvalidationmapinput) arg, SchemaConfiguration configuration) | +| @Nullable Object | foo()
[optional] | | Object | getAdditionalProperty(String name)
provides type safety for additional properties | ## Foo diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/RequiredValidation.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/RequiredValidation.md index c6af00a6cfe..dd7ec4b532a 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/RequiredValidation.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/RequiredValidation.md @@ -40,34 +40,34 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [RequiredValidationMap](#requiredvalidationmap) | validate([Map](#requiredvalidationmapinput) arg, SchemaConfiguration configuration) | +| static [RequiredValidationMap](#requiredvalidationmap) | validate([Map](#requiredvalidationmapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## RequiredValidationMapInput public class RequiredValidationMapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type ## Input Map Keys | Key | Type | Description | Notes | | --- | ---- | ------------ | ----- | -| **foo** | Object | | | -| **bar** | Object | | [optional] | +| **foo** | @Nullable Object | | | +| **bar** | @Nullable Object | | [optional] | | **anyStringName** | Object | any string name can be used but the value must be the correct type | [optional] | ## RequiredValidationMap public static class RequiredValidationMap
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [RequiredValidationMap](#requiredvalidationmap) | of([Map](#requiredvalidationmapinput) arg, SchemaConfiguration configuration) | -| Object | foo()
| -| Object | bar()
[optional] | +| static [RequiredValidationMap](#requiredvalidationmap) | of([Map](#requiredvalidationmapinput) arg, SchemaConfiguration configuration) | +| @Nullable Object | foo()
| +| @Nullable Object | bar()
[optional] | | Object | getAdditionalProperty(String name)
provides type safety for additional properties | ## Bar diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/RequiredWithEmptyArray.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/RequiredWithEmptyArray.md index e3f6e97f8f5..15ef13cce71 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/RequiredWithEmptyArray.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/RequiredWithEmptyArray.md @@ -38,32 +38,32 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [RequiredWithEmptyArrayMap](#requiredwithemptyarraymap) | validate([Map](#requiredwithemptyarraymapinput) arg, SchemaConfiguration configuration) | +| static [RequiredWithEmptyArrayMap](#requiredwithemptyarraymap) | validate([Map](#requiredwithemptyarraymapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## RequiredWithEmptyArrayMapInput public class RequiredWithEmptyArrayMapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type ## Input Map Keys | Key | Type | Description | Notes | | --- | ---- | ------------ | ----- | -| **foo** | Object | | [optional] | +| **foo** | @Nullable Object | | [optional] | | **anyStringName** | Object | any string name can be used but the value must be the correct type | [optional] | ## RequiredWithEmptyArrayMap public static class RequiredWithEmptyArrayMap
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [RequiredWithEmptyArrayMap](#requiredwithemptyarraymap) | of([Map](#requiredwithemptyarraymapinput) arg, SchemaConfiguration configuration) | -| Object | foo()
[optional] | +| static [RequiredWithEmptyArrayMap](#requiredwithemptyarraymap) | of([Map](#requiredwithemptyarraymapinput) arg, SchemaConfiguration configuration) | +| @Nullable Object | foo()
[optional] | | Object | getAdditionalProperty(String name)
provides type safety for additional properties | ## Foo diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/RequiredWithEscapedCharacters.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/RequiredWithEscapedCharacters.md index cac3bac680a..5183d13e257 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/RequiredWithEscapedCharacters.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/RequiredWithEscapedCharacters.md @@ -37,37 +37,37 @@ A schema class that validates payloads | static float | validate(float arg, SchemaConfiguration configuration) | | static double | validate(double arg, SchemaConfiguration configuration) | | static boolean | validate(boolean arg, SchemaConfiguration configuration) | -| static [RequiredWithEscapedCharactersMap](#requiredwithescapedcharactersmap) | validate([Map](#requiredwithescapedcharactersmapinput) arg, SchemaConfiguration configuration) | +| static [RequiredWithEscapedCharactersMap](#requiredwithescapedcharactersmap) | validate([Map](#requiredwithescapedcharactersmapinput) arg, SchemaConfiguration configuration) | | FrozenList | validate(List arg, SchemaConfiguration configuration) | ## RequiredWithEscapedCharactersMapInput public class RequiredWithEscapedCharactersMapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type ## Input Map Keys | Key | Type | Description | Notes | | --- | ---- | ------------ | ----- | -| **foo\tbar** | Object | | | -| **foo\nbar** | Object | | | -| **foo\fbar** | Object | | | -| **foo\rbar** | Object | | | -| **foo\"bar** | Object | | | -| **foo\\bar** | Object | | | +| **foo\tbar** | @Nullable Object | | | +| **foo\nbar** | @Nullable Object | | | +| **foo\fbar** | @Nullable Object | | | +| **foo\rbar** | @Nullable Object | | | +| **foo\"bar** | @Nullable Object | | | +| **foo\\bar** | @Nullable Object | | | | **anyStringName** | Object | any string name can be used but the value must be the correct type | [optional] | ## RequiredWithEscapedCharactersMap public static class RequiredWithEscapedCharactersMap
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [RequiredWithEscapedCharactersMap](#requiredwithescapedcharactersmap) | of([Map](#requiredwithescapedcharactersmapinput) arg, SchemaConfiguration configuration) | -| Object | get(String key)
This schema has invalid Java names so this method must be used when you access instance["foo\tbar"], instance["foo\nbar"], instance["foo\fbar"], instance["foo\rbar"], instance["foo\"bar"], instance["foo\\bar"], | +| static [RequiredWithEscapedCharactersMap](#requiredwithescapedcharactersmap) | of([Map](#requiredwithescapedcharactersmapinput) arg, SchemaConfiguration configuration) | +| @Nullable Object | get(String key)
This schema has invalid Java names so this method must be used when you access instance["foo\tbar"], instance["foo\nbar"], instance["foo\fbar"], instance["foo\rbar"], instance["foo\"bar"], instance["foo\\bar"], | | Object | getAdditionalProperty(String name)
provides type safety for additional properties | [[Back to top]](#top) [[Back to Component Schemas]](../../../README.md#Component-Schemas) [[Back to README]](../../../README.md) diff --git a/samples/client/3_0_3_unit_test/java/docs/components/schemas/TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing.md b/samples/client/3_0_3_unit_test/java/docs/components/schemas/TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing.md index ad4a9b4e867..9bf122f0920 100644 --- a/samples/client/3_0_3_unit_test/java/docs/components/schemas/TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing.md +++ b/samples/client/3_0_3_unit_test/java/docs/components/schemas/TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing.md @@ -60,11 +60,11 @@ TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing.TheDefaultKeywordDoesNo ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMap](#thedefaultkeyworddoesnotdoanythingifthepropertyismissingmap) | validate([Map](#thedefaultkeyworddoesnotdoanythingifthepropertyismissingmapinput) arg, SchemaConfiguration configuration) | +| static [TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMap](#thedefaultkeyworddoesnotdoanythingifthepropertyismissingmap) | validate([Map](#thedefaultkeyworddoesnotdoanythingifthepropertyismissingmapinput) arg, SchemaConfiguration configuration) | ## TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMapInput public class TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMapInput
-builder for `Map` +builder for `Map` A class that builds the Map input type @@ -76,14 +76,14 @@ A class that builds the Map input type ## TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMap public static class TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMap
-extends FrozenMap +extends FrozenMap A class to store validated Map payloads ### Method Summary | Modifier and Type | Method and Description | | ----------------- | ---------------------- | -| static [TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMap](#thedefaultkeyworddoesnotdoanythingifthepropertyismissingmap) | of([Map](#thedefaultkeyworddoesnotdoanythingifthepropertyismissingmapinput) arg, SchemaConfiguration configuration) | +| static [TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMap](#thedefaultkeyworddoesnotdoanythingifthepropertyismissingmap) | of([Map](#thedefaultkeyworddoesnotdoanythingifthepropertyismissingmapinput) arg, SchemaConfiguration configuration) | | Number | alpha()
[optional] if omitted the server will use the default value of 5.0 | | Object | getAdditionalProperty(String name)
provides type safety for additional properties | diff --git a/samples/client/3_0_3_unit_test/java/pom.xml b/samples/client/3_0_3_unit_test/java/pom.xml index 4b3a6048553..8d59416158a 100644 --- a/samples/client/3_0_3_unit_test/java/pom.xml +++ b/samples/client/3_0_3_unit_test/java/pom.xml @@ -43,13 +43,34 @@ true 128m 512m - - -Xlint:all + -Xlint:all + + -Awarns -J-Xss4m + -J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED + -J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED + + + org.checkerframework + checker + ${checker-version} + + + + + org.checkerframework.checker.nullness.NullnessChecker + + - org.apache.maven.plugins maven-enforcer-plugin @@ -213,13 +234,17 @@ ${swagger-annotations-version} - + - com.google.code.findbugs - jsr305 - 3.0.2 + org.checkerframework + checker-qual + ${checker-version} + + + org.checkerframework + checker + ${checker-version} - @@ -277,6 +302,7 @@ 17 UTF-8 1.6.3 + 3.34.0 1.19.4 2.12.6 2.12.6.1 diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesAllowsASchemaWhichShouldValidate.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesAllowsASchemaWhichShouldValidate.java index 4e255e6be6d..62403005d7a 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesAllowsASchemaWhichShouldValidate.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesAllowsASchemaWhichShouldValidate.java @@ -7,9 +7,13 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; import org.openapijsonschematools.client.schemas.BooleanJsonSchema; @@ -34,8 +38,8 @@ public static class Foo extends AnyTypeJsonSchema {} public static class Bar extends AnyTypeJsonSchema {} - public static class AdditionalpropertiesAllowsASchemaWhichShouldValidateMap extends FrozenMap { - protected AdditionalpropertiesAllowsASchemaWhichShouldValidateMap(FrozenMap m) { + public static class AdditionalpropertiesAllowsASchemaWhichShouldValidateMap extends FrozenMap<@Nullable Object> { + protected AdditionalpropertiesAllowsASchemaWhichShouldValidateMap(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of(); @@ -43,25 +47,30 @@ protected AdditionalpropertiesAllowsASchemaWhichShouldValidateMap(FrozenMap arg, SchemaConfiguration configuration) throws ValidationException { + public static AdditionalpropertiesAllowsASchemaWhichShouldValidateMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { return AdditionalpropertiesAllowsASchemaWhichShouldValidate1.getInstance().validate(arg, configuration); } - public Object foo() { + public @Nullable Object foo() throws UnsetPropertyException { String key = "foo"; throwIfKeyNotPresent(key); return get(key); } - public Object bar() { + public @Nullable Object bar() throws UnsetPropertyException { String key = "bar"; throwIfKeyNotPresent(key); return get(key); } - public boolean getAdditionalProperty(String name) { + public boolean getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { + throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); - return (boolean) get(name); + @Nullable Object value = get(name); + if (!(value instanceof Boolean)) { + throw new InvalidTypeException("Invalid value stored for " + name); + } + return (boolean) value; } } public static class AdditionalpropertiesAllowsASchemaWhichShouldValidateMapInput { @@ -69,14 +78,14 @@ public static class AdditionalpropertiesAllowsASchemaWhichShouldValidateMapInput } - public static class AdditionalpropertiesAllowsASchemaWhichShouldValidate1 extends JsonSchema implements MapSchemaValidator { + public static class AdditionalpropertiesAllowsASchemaWhichShouldValidate1 extends JsonSchema implements MapSchemaValidator<@Nullable Object, AdditionalpropertiesAllowsASchemaWhichShouldValidateMap> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static AdditionalpropertiesAllowsASchemaWhichShouldValidate1 instance; + private static @Nullable AdditionalpropertiesAllowsASchemaWhichShouldValidate1 instance = null; protected AdditionalpropertiesAllowsASchemaWhichShouldValidate1() { super(new JsonSchemaInfo() @@ -97,22 +106,33 @@ public static AdditionalpropertiesAllowsASchemaWhichShouldValidate1 getInstance( } public AdditionalpropertiesAllowsASchemaWhichShouldValidateMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new AdditionalpropertiesAllowsASchemaWhichShouldValidateMap(castProperties); } @Override - public AdditionalpropertiesAllowsASchemaWhichShouldValidateMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public AdditionalpropertiesAllowsASchemaWhichShouldValidateMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -124,11 +144,11 @@ public AdditionalpropertiesAllowsASchemaWhichShouldValidateMap validate(Map pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesAreAllowedByDefault.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesAreAllowedByDefault.java index 2b707c31875..3f035ee63d4 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesAreAllowedByDefault.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesAreAllowedByDefault.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; @@ -39,8 +43,8 @@ public static class Foo extends AnyTypeJsonSchema {} public static class Bar extends AnyTypeJsonSchema {} - public static class AdditionalpropertiesAreAllowedByDefaultMap extends FrozenMap { - protected AdditionalpropertiesAreAllowedByDefaultMap(FrozenMap m) { + public static class AdditionalpropertiesAreAllowedByDefaultMap extends FrozenMap<@Nullable Object> { + protected AdditionalpropertiesAreAllowedByDefaultMap(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of(); @@ -48,23 +52,23 @@ protected AdditionalpropertiesAreAllowedByDefaultMap(FrozenMap m) { "foo", "bar" ); - public static AdditionalpropertiesAreAllowedByDefaultMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static AdditionalpropertiesAreAllowedByDefaultMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { return AdditionalpropertiesAreAllowedByDefault1.getInstance().validate(arg, configuration); } - public Object foo() { + public @Nullable Object foo() throws UnsetPropertyException { String key = "foo"; throwIfKeyNotPresent(key); return get(key); } - public Object bar() { + public @Nullable Object bar() throws UnsetPropertyException { String key = "bar"; throwIfKeyNotPresent(key); return get(key); } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -75,14 +79,14 @@ public static class AdditionalpropertiesAreAllowedByDefaultMapInput { } - public static class AdditionalpropertiesAreAllowedByDefault1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { + public static class AdditionalpropertiesAreAllowedByDefault1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, AdditionalpropertiesAreAllowedByDefaultMap> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static AdditionalpropertiesAreAllowedByDefault1 instance; + private static @Nullable AdditionalpropertiesAreAllowedByDefault1 instance = null; protected AdditionalpropertiesAreAllowedByDefault1() { super(new JsonSchemaInfo() @@ -181,23 +185,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -209,22 +220,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public AdditionalpropertiesAreAllowedByDefaultMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new AdditionalpropertiesAreAllowedByDefaultMap(castProperties); } @Override - public AdditionalpropertiesAreAllowedByDefaultMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public AdditionalpropertiesAreAllowedByDefaultMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -237,7 +259,7 @@ public AdditionalpropertiesAreAllowedByDefaultMap validate(Map a } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -252,7 +274,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesCanExistByItself.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesCanExistByItself.java index a9f4d359ae7..0cd7de194f5 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesCanExistByItself.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesCanExistByItself.java @@ -7,9 +7,13 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.BooleanJsonSchema; import org.openapijsonschematools.client.schemas.validation.FrozenMap; @@ -36,9 +40,13 @@ public static AdditionalpropertiesCanExistByItselfMap of(Map ar return AdditionalpropertiesCanExistByItself1.getInstance().validate(arg, configuration); } - public boolean getAdditionalProperty(String name) { + public boolean getAdditionalProperty(String name) throws UnsetPropertyException { throwIfKeyNotPresent(name); - return get(name); + Boolean value = get(name); + if (value == null) { + throw new InvalidTypeException("Value may not be null"); + } + return (boolean) value; } } public static class AdditionalpropertiesCanExistByItselfMapInput { @@ -53,7 +61,7 @@ public static class AdditionalpropertiesCanExistByItself1 extends JsonSchema imp Do not edit the class manually. */ - private static AdditionalpropertiesCanExistByItself1 instance; + private static @Nullable AdditionalpropertiesCanExistByItself1 instance = null; protected AdditionalpropertiesCanExistByItself1() { super(new JsonSchemaInfo() @@ -72,13 +80,24 @@ public static AdditionalpropertiesCanExistByItself1 getInstance() { public AdditionalpropertiesCanExistByItselfMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Boolean castValue = (Boolean) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Boolean)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (Boolean) propertyInstance); } FrozenMap castProperties = new FrozenMap<>(properties); return new AdditionalpropertiesCanExistByItselfMap(castProperties); @@ -97,11 +116,11 @@ public AdditionalpropertiesCanExistByItselfMap validate(Map arg @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesShouldNotLookInApplicators.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesShouldNotLookInApplicators.java index 6f25b0b4d47..67323fb3f4a 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesShouldNotLookInApplicators.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesShouldNotLookInApplicators.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; import org.openapijsonschematools.client.schemas.BooleanJsonSchema; @@ -40,25 +44,25 @@ public static class AdditionalProperties extends BooleanJsonSchema {} public static class Foo extends AnyTypeJsonSchema {} - public static class Schema0Map extends FrozenMap { - protected Schema0Map(FrozenMap m) { + public static class Schema0Map extends FrozenMap<@Nullable Object> { + protected Schema0Map(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of(); public static final Set optionalKeys = Set.of( "foo" ); - public static Schema0Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static Schema0Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { return Schema0.getInstance().validate(arg, configuration); } - public Object foo() { + public @Nullable Object foo() throws UnsetPropertyException { String key = "foo"; throwIfKeyNotPresent(key); return get(key); } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -69,8 +73,8 @@ public static class Schema0MapInput { } - public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { - private static Schema0 instance; + public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, Schema0Map> { + private static @Nullable Schema0 instance = null; protected Schema0() { super(new JsonSchemaInfo() @@ -168,23 +172,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -196,22 +207,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public Schema0Map getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new Schema0Map(castProperties); } @Override - public Schema0Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public Schema0Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -224,7 +246,7 @@ public Schema0Map validate(Map arg, SchemaConfiguration configur } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -239,7 +261,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } @@ -253,9 +275,13 @@ public static AdditionalpropertiesShouldNotLookInApplicatorsMap of(Map>, MapSchemaValidator { + public static class AdditionalpropertiesShouldNotLookInApplicators1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static AdditionalpropertiesShouldNotLookInApplicators1 instance; + private static @Nullable AdditionalpropertiesShouldNotLookInApplicators1 instance = null; protected AdditionalpropertiesShouldNotLookInApplicators1() { super(new JsonSchemaInfo() @@ -369,23 +395,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -399,13 +432,24 @@ public FrozenList validate(List arg, SchemaConfiguration configu public AdditionalpropertiesShouldNotLookInApplicatorsMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Boolean castValue = (Boolean) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Boolean)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (Boolean) propertyInstance); } FrozenMap castProperties = new FrozenMap<>(properties); return new AdditionalpropertiesShouldNotLookInApplicatorsMap(castProperties); @@ -425,7 +469,7 @@ public AdditionalpropertiesShouldNotLookInApplicatorsMap validate(Map pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -440,7 +484,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Allof.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Allof.java index cc8695a185e..6e06afc76e9 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Allof.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Allof.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.IntJsonSchema; import org.openapijsonschematools.client.schemas.StringJsonSchema; @@ -37,23 +41,27 @@ public class Allof { public static class Bar extends IntJsonSchema {} - public static class Schema0Map extends FrozenMap { - protected Schema0Map(FrozenMap m) { + public static class Schema0Map extends FrozenMap<@Nullable Object> { + protected Schema0Map(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of( "bar" ); public static final Set optionalKeys = Set.of(); - public static Schema0Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static Schema0Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { return Schema0.getInstance().validate(arg, configuration); } public long bar() { - return (long) get("bar"); + @Nullable Object value = get("bar"); + if (!(value instanceof Long)) { + throw new InvalidTypeException("Invalid value stored for bar"); + } + return (long) value; } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -64,8 +72,8 @@ public static class Schema0MapInput { } - public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { - private static Schema0 instance; + public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, Schema0Map> { + private static @Nullable Schema0 instance = null; protected Schema0() { super(new JsonSchemaInfo() @@ -166,23 +174,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -194,22 +209,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public Schema0Map getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new Schema0Map(castProperties); } @Override - public Schema0Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public Schema0Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -222,7 +248,7 @@ public Schema0Map validate(Map arg, SchemaConfiguration configur } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -237,30 +263,34 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } public static class Foo extends StringJsonSchema {} - public static class Schema1Map extends FrozenMap { - protected Schema1Map(FrozenMap m) { + public static class Schema1Map extends FrozenMap<@Nullable Object> { + protected Schema1Map(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of( "foo" ); public static final Set optionalKeys = Set.of(); - public static Schema1Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static Schema1Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { return Schema1.getInstance().validate(arg, configuration); } public String foo() { - return (String) get("foo"); + @Nullable Object value = get("foo"); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for foo"); + } + return (String) value; } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -271,8 +301,8 @@ public static class Schema1MapInput { } - public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { - private static Schema1 instance; + public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, Schema1Map> { + private static @Nullable Schema1 instance = null; protected Schema1() { super(new JsonSchemaInfo() @@ -373,23 +403,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -401,22 +438,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public Schema1Map getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new Schema1Map(castProperties); } @Override - public Schema1Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public Schema1Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -429,7 +477,7 @@ public Schema1Map validate(Map arg, SchemaConfiguration configur } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -444,18 +492,18 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class Allof1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class Allof1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static Allof1 instance; + private static @Nullable Allof1 instance = null; protected Allof1() { super(new JsonSchemaInfo() @@ -554,23 +602,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -581,23 +636,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -610,7 +676,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -625,7 +691,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofCombinedWithAnyofOneof.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofCombinedWithAnyofOneof.java index 4291a6bfff3..46ed4f198fd 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofCombinedWithAnyofOneof.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofCombinedWithAnyofOneof.java @@ -11,9 +11,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -32,8 +36,8 @@ public class AllofCombinedWithAnyofOneof { // nest classes so all schemas and input/output classes can be public - public static class Schema02 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static Schema02 instance; + public static class Schema02 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { + private static @Nullable Schema02 instance = null; protected Schema02() { super(new JsonSchemaInfo() @@ -129,23 +133,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -156,23 +167,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -185,7 +207,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -200,12 +222,12 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class Schema01 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static Schema01 instance; + public static class Schema01 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { + private static @Nullable Schema01 instance = null; protected Schema01() { super(new JsonSchemaInfo() @@ -301,23 +323,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -328,23 +357,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -357,7 +397,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -372,12 +412,12 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static Schema0 instance; + public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { + private static @Nullable Schema0 instance = null; protected Schema0() { super(new JsonSchemaInfo() @@ -473,23 +513,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -500,23 +547,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -529,7 +587,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -544,18 +602,18 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class AllofCombinedWithAnyofOneof1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class AllofCombinedWithAnyofOneof1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static AllofCombinedWithAnyofOneof1 instance; + private static @Nullable AllofCombinedWithAnyofOneof1 instance = null; protected AllofCombinedWithAnyofOneof1() { super(new JsonSchemaInfo() @@ -659,23 +717,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -686,23 +751,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -715,7 +791,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -730,7 +806,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofSimpleTypes.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofSimpleTypes.java index c56c9dbe3eb..50a13e47085 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofSimpleTypes.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofSimpleTypes.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,8 +35,8 @@ public class AllofSimpleTypes { // nest classes so all schemas and input/output classes can be public - public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static Schema0 instance; + public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { + private static @Nullable Schema0 instance = null; protected Schema0() { super(new JsonSchemaInfo() @@ -128,23 +132,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -155,23 +166,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -184,7 +206,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -199,12 +221,12 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static Schema1 instance; + public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { + private static @Nullable Schema1 instance = null; protected Schema1() { super(new JsonSchemaInfo() @@ -300,23 +322,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -327,23 +356,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -356,7 +396,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -371,18 +411,18 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class AllofSimpleTypes1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class AllofSimpleTypes1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static AllofSimpleTypes1 instance; + private static @Nullable AllofSimpleTypes1 instance = null; protected AllofSimpleTypes1() { super(new JsonSchemaInfo() @@ -481,23 +521,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -508,23 +555,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -537,7 +595,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -552,7 +610,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofWithBaseSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofWithBaseSchema.java index f161abb0582..237c0b36ce7 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofWithBaseSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofWithBaseSchema.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.IntJsonSchema; import org.openapijsonschematools.client.schemas.NullJsonSchema; @@ -38,23 +42,27 @@ public class AllofWithBaseSchema { public static class Foo extends StringJsonSchema {} - public static class Schema0Map extends FrozenMap { - protected Schema0Map(FrozenMap m) { + public static class Schema0Map extends FrozenMap<@Nullable Object> { + protected Schema0Map(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of( "foo" ); public static final Set optionalKeys = Set.of(); - public static Schema0Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static Schema0Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { return Schema0.getInstance().validate(arg, configuration); } public String foo() { - return (String) get("foo"); + @Nullable Object value = get("foo"); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for foo"); + } + return (String) value; } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -65,8 +73,8 @@ public static class Schema0MapInput { } - public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { - private static Schema0 instance; + public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, Schema0Map> { + private static @Nullable Schema0 instance = null; protected Schema0() { super(new JsonSchemaInfo() @@ -167,23 +175,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -195,22 +210,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public Schema0Map getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new Schema0Map(castProperties); } @Override - public Schema0Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public Schema0Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -223,7 +249,7 @@ public Schema0Map validate(Map arg, SchemaConfiguration configur } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -238,30 +264,34 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } public static class Baz extends NullJsonSchema {} - public static class Schema1Map extends FrozenMap { - protected Schema1Map(FrozenMap m) { + public static class Schema1Map extends FrozenMap<@Nullable Object> { + protected Schema1Map(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of( "baz" ); public static final Set optionalKeys = Set.of(); - public static Schema1Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static Schema1Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { return Schema1.getInstance().validate(arg, configuration); } public Void baz() { - return (Void) get("baz"); + @Nullable Object value = get("baz"); + if (!(value instanceof Void)) { + throw new InvalidTypeException("Invalid value stored for baz"); + } + return (Void) value; } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -272,8 +302,8 @@ public static class Schema1MapInput { } - public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { - private static Schema1 instance; + public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, Schema1Map> { + private static @Nullable Schema1 instance = null; protected Schema1() { super(new JsonSchemaInfo() @@ -374,23 +404,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -402,22 +439,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public Schema1Map getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new Schema1Map(castProperties); } @Override - public Schema1Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public Schema1Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -430,7 +478,7 @@ public Schema1Map validate(Map arg, SchemaConfiguration configur } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -445,30 +493,34 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } public static class Bar extends IntJsonSchema {} - public static class AllofWithBaseSchemaMap extends FrozenMap { - protected AllofWithBaseSchemaMap(FrozenMap m) { + public static class AllofWithBaseSchemaMap extends FrozenMap<@Nullable Object> { + protected AllofWithBaseSchemaMap(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of( "bar" ); public static final Set optionalKeys = Set.of(); - public static AllofWithBaseSchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static AllofWithBaseSchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { return AllofWithBaseSchema1.getInstance().validate(arg, configuration); } public long bar() { - return (long) get("bar"); + @Nullable Object value = get("bar"); + if (!(value instanceof Long)) { + throw new InvalidTypeException("Invalid value stored for bar"); + } + return (long) value; } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -479,14 +531,14 @@ public static class AllofWithBaseSchemaMapInput { } - public static class AllofWithBaseSchema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { + public static class AllofWithBaseSchema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, AllofWithBaseSchemaMap> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static AllofWithBaseSchema1 instance; + private static @Nullable AllofWithBaseSchema1 instance = null; protected AllofWithBaseSchema1() { super(new JsonSchemaInfo() @@ -591,23 +643,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -619,22 +678,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public AllofWithBaseSchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new AllofWithBaseSchemaMap(castProperties); } @Override - public AllofWithBaseSchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public AllofWithBaseSchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -647,7 +717,7 @@ public AllofWithBaseSchemaMap validate(Map arg, SchemaConfigurat } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -662,7 +732,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofWithOneEmptySchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofWithOneEmptySchema.java index f61a417dbcc..be54677f54b 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofWithOneEmptySchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofWithOneEmptySchema.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; @@ -35,14 +39,14 @@ public class AllofWithOneEmptySchema { public static class Schema0 extends AnyTypeJsonSchema {} - public static class AllofWithOneEmptySchema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class AllofWithOneEmptySchema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static AllofWithOneEmptySchema1 instance; + private static @Nullable AllofWithOneEmptySchema1 instance = null; protected AllofWithOneEmptySchema1() { super(new JsonSchemaInfo() @@ -140,23 +144,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -167,23 +178,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -196,7 +218,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -211,7 +233,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofWithTheFirstEmptySchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofWithTheFirstEmptySchema.java index f24ef12c83e..56dc6a12c35 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofWithTheFirstEmptySchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofWithTheFirstEmptySchema.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; import org.openapijsonschematools.client.schemas.NumberJsonSchema; @@ -39,14 +43,14 @@ public static class Schema0 extends AnyTypeJsonSchema {} public static class Schema1 extends NumberJsonSchema {} - public static class AllofWithTheFirstEmptySchema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class AllofWithTheFirstEmptySchema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static AllofWithTheFirstEmptySchema1 instance; + private static @Nullable AllofWithTheFirstEmptySchema1 instance = null; protected AllofWithTheFirstEmptySchema1() { super(new JsonSchemaInfo() @@ -145,23 +149,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -172,23 +183,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -201,7 +223,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -216,7 +238,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofWithTheLastEmptySchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofWithTheLastEmptySchema.java index abd770f8a65..a22de51a891 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofWithTheLastEmptySchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofWithTheLastEmptySchema.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; import org.openapijsonschematools.client.schemas.NumberJsonSchema; @@ -39,14 +43,14 @@ public static class Schema0 extends NumberJsonSchema {} public static class Schema1 extends AnyTypeJsonSchema {} - public static class AllofWithTheLastEmptySchema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class AllofWithTheLastEmptySchema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static AllofWithTheLastEmptySchema1 instance; + private static @Nullable AllofWithTheLastEmptySchema1 instance = null; protected AllofWithTheLastEmptySchema1() { super(new JsonSchemaInfo() @@ -145,23 +149,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -172,23 +183,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -201,7 +223,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -216,7 +238,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofWithTwoEmptySchemas.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofWithTwoEmptySchemas.java index d8cbcbd2dd3..6a0ad0d813c 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofWithTwoEmptySchemas.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AllofWithTwoEmptySchemas.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; @@ -38,14 +42,14 @@ public static class Schema0 extends AnyTypeJsonSchema {} public static class Schema1 extends AnyTypeJsonSchema {} - public static class AllofWithTwoEmptySchemas1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class AllofWithTwoEmptySchemas1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static AllofWithTwoEmptySchemas1 instance; + private static @Nullable AllofWithTwoEmptySchemas1 instance = null; protected AllofWithTwoEmptySchemas1() { super(new JsonSchemaInfo() @@ -144,23 +148,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -171,23 +182,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -200,7 +222,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -215,7 +237,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Anyof.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Anyof.java index 3b29887c888..2cb38b9cfc5 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Anyof.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Anyof.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.IntJsonSchema; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; @@ -35,8 +39,8 @@ public class Anyof { public static class Schema0 extends IntJsonSchema {} - public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static Schema1 instance; + public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { + private static @Nullable Schema1 instance = null; protected Schema1() { super(new JsonSchemaInfo() @@ -132,23 +136,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -159,23 +170,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -188,7 +210,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -203,18 +225,18 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class Anyof1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class Anyof1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static Anyof1 instance; + private static @Nullable Anyof1 instance = null; protected Anyof1() { super(new JsonSchemaInfo() @@ -313,23 +335,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -340,23 +369,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -369,7 +409,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -384,7 +424,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AnyofComplexTypes.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AnyofComplexTypes.java index 576237e5a4f..8cc875ec166 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AnyofComplexTypes.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AnyofComplexTypes.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.IntJsonSchema; import org.openapijsonschematools.client.schemas.StringJsonSchema; @@ -37,23 +41,27 @@ public class AnyofComplexTypes { public static class Bar extends IntJsonSchema {} - public static class Schema0Map extends FrozenMap { - protected Schema0Map(FrozenMap m) { + public static class Schema0Map extends FrozenMap<@Nullable Object> { + protected Schema0Map(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of( "bar" ); public static final Set optionalKeys = Set.of(); - public static Schema0Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static Schema0Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { return Schema0.getInstance().validate(arg, configuration); } public long bar() { - return (long) get("bar"); + @Nullable Object value = get("bar"); + if (!(value instanceof Long)) { + throw new InvalidTypeException("Invalid value stored for bar"); + } + return (long) value; } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -64,8 +72,8 @@ public static class Schema0MapInput { } - public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { - private static Schema0 instance; + public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, Schema0Map> { + private static @Nullable Schema0 instance = null; protected Schema0() { super(new JsonSchemaInfo() @@ -166,23 +174,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -194,22 +209,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public Schema0Map getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new Schema0Map(castProperties); } @Override - public Schema0Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public Schema0Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -222,7 +248,7 @@ public Schema0Map validate(Map arg, SchemaConfiguration configur } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -237,30 +263,34 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } public static class Foo extends StringJsonSchema {} - public static class Schema1Map extends FrozenMap { - protected Schema1Map(FrozenMap m) { + public static class Schema1Map extends FrozenMap<@Nullable Object> { + protected Schema1Map(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of( "foo" ); public static final Set optionalKeys = Set.of(); - public static Schema1Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static Schema1Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { return Schema1.getInstance().validate(arg, configuration); } public String foo() { - return (String) get("foo"); + @Nullable Object value = get("foo"); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for foo"); + } + return (String) value; } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -271,8 +301,8 @@ public static class Schema1MapInput { } - public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { - private static Schema1 instance; + public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, Schema1Map> { + private static @Nullable Schema1 instance = null; protected Schema1() { super(new JsonSchemaInfo() @@ -373,23 +403,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -401,22 +438,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public Schema1Map getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new Schema1Map(castProperties); } @Override - public Schema1Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public Schema1Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -429,7 +477,7 @@ public Schema1Map validate(Map arg, SchemaConfiguration configur } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -444,18 +492,18 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class AnyofComplexTypes1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class AnyofComplexTypes1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static AnyofComplexTypes1 instance; + private static @Nullable AnyofComplexTypes1 instance = null; protected AnyofComplexTypes1() { super(new JsonSchemaInfo() @@ -554,23 +602,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -581,23 +636,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -610,7 +676,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -625,7 +691,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AnyofWithBaseSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AnyofWithBaseSchema.java index e69d8fc5378..55e4c4a43b4 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AnyofWithBaseSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AnyofWithBaseSchema.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,8 +35,8 @@ public class AnyofWithBaseSchema { // nest classes so all schemas and input/output classes can be public - public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static Schema0 instance; + public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { + private static @Nullable Schema0 instance = null; protected Schema0() { super(new JsonSchemaInfo() @@ -128,23 +132,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -155,23 +166,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -184,7 +206,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -199,12 +221,12 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static Schema1 instance; + public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { + private static @Nullable Schema1 instance = null; protected Schema1() { super(new JsonSchemaInfo() @@ -300,23 +322,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -327,23 +356,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -356,7 +396,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -371,7 +411,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } @@ -382,7 +422,7 @@ public static class AnyofWithBaseSchema1 extends JsonSchema implements StringSch Do not edit the class manually. */ - private static AnyofWithBaseSchema1 instance; + private static @Nullable AnyofWithBaseSchema1 instance = null; protected AnyofWithBaseSchema1() { super(new JsonSchemaInfo() @@ -415,11 +455,11 @@ public String validate(String arg, SchemaConfiguration configuration) throws Val } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof String) { return getNewInstance((String) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AnyofWithOneEmptySchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AnyofWithOneEmptySchema.java index 24805befbaa..61a3149cb84 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AnyofWithOneEmptySchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/AnyofWithOneEmptySchema.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; import org.openapijsonschematools.client.schemas.NumberJsonSchema; @@ -39,14 +43,14 @@ public static class Schema0 extends NumberJsonSchema {} public static class Schema1 extends AnyTypeJsonSchema {} - public static class AnyofWithOneEmptySchema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class AnyofWithOneEmptySchema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static AnyofWithOneEmptySchema1 instance; + private static @Nullable AnyofWithOneEmptySchema1 instance = null; protected AnyofWithOneEmptySchema1() { super(new JsonSchemaInfo() @@ -145,23 +149,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -172,23 +183,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -201,7 +223,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -216,7 +238,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/ArrayTypeMatchesArrays.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/ArrayTypeMatchesArrays.java index 2c679401d9a..62c7262651c 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/ArrayTypeMatchesArrays.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/ArrayTypeMatchesArrays.java @@ -7,6 +7,8 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; import org.openapijsonschematools.client.exceptions.InvalidTypeException; @@ -26,28 +28,28 @@ public class ArrayTypeMatchesArrays { public static class Items extends AnyTypeJsonSchema {} - public static class ArrayTypeMatchesArraysList extends FrozenList { - protected ArrayTypeMatchesArraysList(FrozenList m) { + public static class ArrayTypeMatchesArraysList extends FrozenList<@Nullable Object> { + protected ArrayTypeMatchesArraysList(FrozenList<@Nullable Object> m) { super(m); } - public static ArrayTypeMatchesArraysList of(List arg, SchemaConfiguration configuration) throws ValidationException { + public static ArrayTypeMatchesArraysList of(List<@Nullable Object> arg, SchemaConfiguration configuration) throws ValidationException { return ArrayTypeMatchesArrays1.getInstance().validate(arg, configuration); } } public static class ArrayTypeMatchesArraysListInput { - // class to build List + // class to build List<@Nullable Object> } - public static class ArrayTypeMatchesArrays1 extends JsonSchema implements ListSchemaValidator { + public static class ArrayTypeMatchesArrays1 extends JsonSchema implements ListSchemaValidator<@Nullable Object, ArrayTypeMatchesArraysList> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static ArrayTypeMatchesArrays1 instance; + private static @Nullable ArrayTypeMatchesArrays1 instance = null; protected ArrayTypeMatchesArrays1() { super(new JsonSchemaInfo() @@ -65,22 +67,29 @@ public static ArrayTypeMatchesArrays1 getInstance() { @Override public ArrayTypeMatchesArraysList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return new ArrayTypeMatchesArraysList(newInstanceItems); } @Override - public ArrayTypeMatchesArraysList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public ArrayTypeMatchesArraysList validate(List<@Nullable Object> arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -91,11 +100,11 @@ public ArrayTypeMatchesArraysList validate(List arg, SchemaConfiguration } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof List) { return getNewInstance((List) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/ByInt.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/ByInt.java index 1617fa2f4da..9f57779cb16 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/ByInt.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/ByInt.java @@ -11,9 +11,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -32,14 +36,14 @@ public class ByInt { // nest classes so all schemas and input/output classes can be public - public static class ByInt1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class ByInt1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static ByInt1 instance; + private static @Nullable ByInt1 instance = null; protected ByInt1() { super(new JsonSchemaInfo() @@ -135,23 +139,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -162,23 +173,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -191,7 +213,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -206,7 +228,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/ByNumber.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/ByNumber.java index 10511f576ec..4c5201e0ed1 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/ByNumber.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/ByNumber.java @@ -11,9 +11,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -32,14 +36,14 @@ public class ByNumber { // nest classes so all schemas and input/output classes can be public - public static class ByNumber1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class ByNumber1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static ByNumber1 instance; + private static @Nullable ByNumber1 instance = null; protected ByNumber1() { super(new JsonSchemaInfo() @@ -135,23 +139,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -162,23 +173,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -191,7 +213,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -206,7 +228,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/BySmallNumber.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/BySmallNumber.java index 2e4775f02a0..0263d3eeb92 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/BySmallNumber.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/BySmallNumber.java @@ -11,9 +11,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -32,14 +36,14 @@ public class BySmallNumber { // nest classes so all schemas and input/output classes can be public - public static class BySmallNumber1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class BySmallNumber1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static BySmallNumber1 instance; + private static @Nullable BySmallNumber1 instance = null; protected BySmallNumber1() { super(new JsonSchemaInfo() @@ -135,23 +139,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -162,23 +173,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -191,7 +213,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -206,7 +228,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/DateTimeFormat.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/DateTimeFormat.java index b4664dd703f..ca71bc66a27 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/DateTimeFormat.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/DateTimeFormat.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class DateTimeFormat { // nest classes so all schemas and input/output classes can be public - public static class DateTimeFormat1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class DateTimeFormat1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static DateTimeFormat1 instance; + private static @Nullable DateTimeFormat1 instance = null; protected DateTimeFormat1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EmailFormat.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EmailFormat.java index 499ad160224..2c6c233fd1e 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EmailFormat.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EmailFormat.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class EmailFormat { // nest classes so all schemas and input/output classes can be public - public static class EmailFormat1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class EmailFormat1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static EmailFormat1 instance; + private static @Nullable EmailFormat1 instance = null; protected EmailFormat1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumWith0DoesNotMatchFalse.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumWith0DoesNotMatchFalse.java index 0f9c461affe..c84eff9ac01 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumWith0DoesNotMatchFalse.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumWith0DoesNotMatchFalse.java @@ -7,6 +7,8 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; import org.openapijsonschematools.client.exceptions.InvalidTypeException; @@ -29,7 +31,7 @@ public static class EnumWith0DoesNotMatchFalse1 extends JsonSchema implements Nu Do not edit the class manually. */ - private static EnumWith0DoesNotMatchFalse1 instance; + private static @Nullable EnumWith0DoesNotMatchFalse1 instance = null; protected EnumWith0DoesNotMatchFalse1() { super(new JsonSchemaInfo() @@ -79,11 +81,11 @@ public double validate(double arg, SchemaConfiguration configuration) throws Val } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Number) { return getNewInstance((Number) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumWith1DoesNotMatchTrue.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumWith1DoesNotMatchTrue.java index 3b547c80445..6e1924a70fb 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumWith1DoesNotMatchTrue.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumWith1DoesNotMatchTrue.java @@ -7,6 +7,8 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; import org.openapijsonschematools.client.exceptions.InvalidTypeException; @@ -29,7 +31,7 @@ public static class EnumWith1DoesNotMatchTrue1 extends JsonSchema implements Num Do not edit the class manually. */ - private static EnumWith1DoesNotMatchTrue1 instance; + private static @Nullable EnumWith1DoesNotMatchTrue1 instance = null; protected EnumWith1DoesNotMatchTrue1() { super(new JsonSchemaInfo() @@ -79,11 +81,11 @@ public double validate(double arg, SchemaConfiguration configuration) throws Val } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Number) { return getNewInstance((Number) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumWithEscapedCharacters.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumWithEscapedCharacters.java index aa0789ea6fa..243d74eb59a 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumWithEscapedCharacters.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumWithEscapedCharacters.java @@ -7,6 +7,8 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; import org.openapijsonschematools.client.exceptions.InvalidTypeException; @@ -29,7 +31,7 @@ public static class EnumWithEscapedCharacters1 extends JsonSchema implements Str Do not edit the class manually. */ - private static EnumWithEscapedCharacters1 instance; + private static @Nullable EnumWithEscapedCharacters1 instance = null; protected EnumWithEscapedCharacters1() { super(new JsonSchemaInfo() @@ -62,11 +64,11 @@ public String validate(String arg, SchemaConfiguration configuration) throws Val } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof String) { return getNewInstance((String) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumWithFalseDoesNotMatch0.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumWithFalseDoesNotMatch0.java index 21f0fadfcaa..0cded3cd165 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumWithFalseDoesNotMatch0.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumWithFalseDoesNotMatch0.java @@ -7,6 +7,8 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; import org.openapijsonschematools.client.exceptions.InvalidTypeException; @@ -29,7 +31,7 @@ public static class EnumWithFalseDoesNotMatch01 extends JsonSchema implements Bo Do not edit the class manually. */ - private static EnumWithFalseDoesNotMatch01 instance; + private static @Nullable EnumWithFalseDoesNotMatch01 instance = null; protected EnumWithFalseDoesNotMatch01() { super(new JsonSchemaInfo() @@ -59,12 +61,12 @@ public boolean validate(boolean arg, SchemaConfiguration configuration) throws V } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Boolean) { boolean boolArg = (Boolean) arg; return getNewInstance(boolArg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumWithTrueDoesNotMatch1.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumWithTrueDoesNotMatch1.java index 74682194919..ad28158bc5e 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumWithTrueDoesNotMatch1.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumWithTrueDoesNotMatch1.java @@ -7,6 +7,8 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; import org.openapijsonschematools.client.exceptions.InvalidTypeException; @@ -29,7 +31,7 @@ public static class EnumWithTrueDoesNotMatch11 extends JsonSchema implements Boo Do not edit the class manually. */ - private static EnumWithTrueDoesNotMatch11 instance; + private static @Nullable EnumWithTrueDoesNotMatch11 instance = null; protected EnumWithTrueDoesNotMatch11() { super(new JsonSchemaInfo() @@ -59,12 +61,12 @@ public boolean validate(boolean arg, SchemaConfiguration configuration) throws V } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Boolean) { boolean boolArg = (Boolean) arg; return getNewInstance(boolArg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumsInProperties.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumsInProperties.java index eb918ac4462..709caa19f55 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumsInProperties.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/EnumsInProperties.java @@ -7,9 +7,13 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.SetMaker; import org.openapijsonschematools.client.schemas.validation.FrozenMap; @@ -26,7 +30,7 @@ public class EnumsInProperties { public static class Foo extends JsonSchema implements StringSchemaValidator { - private static Foo instance; + private static @Nullable Foo instance = null; protected Foo() { super(new JsonSchemaInfo() @@ -58,16 +62,16 @@ public String validate(String arg, SchemaConfiguration configuration) throws Val } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof String) { return getNewInstance((String) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } public static class Bar extends JsonSchema implements StringSchemaValidator { - private static Bar instance; + private static @Nullable Bar instance = null; protected Bar() { super(new JsonSchemaInfo() @@ -99,16 +103,16 @@ public String validate(String arg, SchemaConfiguration configuration) throws Val } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof String) { return getNewInstance((String) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class EnumsInPropertiesMap extends FrozenMap { - protected EnumsInPropertiesMap(FrozenMap m) { + public static class EnumsInPropertiesMap extends FrozenMap<@Nullable Object> { + protected EnumsInPropertiesMap(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of( @@ -117,21 +121,29 @@ protected EnumsInPropertiesMap(FrozenMap m) { public static final Set optionalKeys = Set.of( "foo" ); - public static EnumsInPropertiesMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static EnumsInPropertiesMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { return EnumsInProperties1.getInstance().validate(arg, configuration); } public String bar() { - return (String) get("bar"); + @Nullable Object value = get("bar"); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for bar"); + } + return (String) value; } - public String foo() { + public String foo() throws UnsetPropertyException { String key = "foo"; throwIfKeyNotPresent(key); - return (String) get(key); + @Nullable Object value = get(key); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for foo"); + } + return (String) value; } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -142,14 +154,14 @@ public static class EnumsInPropertiesMapInput { } - public static class EnumsInProperties1 extends JsonSchema implements MapSchemaValidator { + public static class EnumsInProperties1 extends JsonSchema implements MapSchemaValidator<@Nullable Object, EnumsInPropertiesMap> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static EnumsInProperties1 instance; + private static @Nullable EnumsInProperties1 instance = null; protected EnumsInProperties1() { super(new JsonSchemaInfo() @@ -172,22 +184,33 @@ public static EnumsInProperties1 getInstance() { } public EnumsInPropertiesMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new EnumsInPropertiesMap(castProperties); } @Override - public EnumsInPropertiesMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public EnumsInPropertiesMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -199,11 +222,11 @@ public EnumsInPropertiesMap validate(Map arg, SchemaConfiguratio @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/ForbiddenProperty.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/ForbiddenProperty.java index 31dfb4f1f5c..57aecaf436f 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/ForbiddenProperty.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/ForbiddenProperty.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; import org.openapijsonschematools.client.schemas.NotAnyTypeJsonSchema; @@ -38,25 +42,25 @@ public static class Foo extends NotAnyTypeJsonSchema {} // NotAnyTypeSchema - public static class ForbiddenPropertyMap extends FrozenMap { - protected ForbiddenPropertyMap(FrozenMap m) { + public static class ForbiddenPropertyMap extends FrozenMap<@Nullable Object> { + protected ForbiddenPropertyMap(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of(); public static final Set optionalKeys = Set.of( "foo" ); - public static ForbiddenPropertyMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static ForbiddenPropertyMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { return ForbiddenProperty1.getInstance().validate(arg, configuration); } - public Object foo() { + public @Nullable Object foo() throws UnsetPropertyException { String key = "foo"; throwIfKeyNotPresent(key); return get(key); } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -67,14 +71,14 @@ public static class ForbiddenPropertyMapInput { } - public static class ForbiddenProperty1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { + public static class ForbiddenProperty1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, ForbiddenPropertyMap> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static ForbiddenProperty1 instance; + private static @Nullable ForbiddenProperty1 instance = null; protected ForbiddenProperty1() { super(new JsonSchemaInfo() @@ -172,23 +176,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -200,22 +211,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public ForbiddenPropertyMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new ForbiddenPropertyMap(castProperties); } @Override - public ForbiddenPropertyMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public ForbiddenPropertyMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -228,7 +250,7 @@ public ForbiddenPropertyMap validate(Map arg, SchemaConfiguratio } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -243,7 +265,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/HostnameFormat.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/HostnameFormat.java index 8232003f6cf..6f06667e3e9 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/HostnameFormat.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/HostnameFormat.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class HostnameFormat { // nest classes so all schemas and input/output classes can be public - public static class HostnameFormat1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class HostnameFormat1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static HostnameFormat1 instance; + private static @Nullable HostnameFormat1 instance = null; protected HostnameFormat1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/InvalidInstanceShouldNotRaiseErrorWhenFloatDivisionInf.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/InvalidInstanceShouldNotRaiseErrorWhenFloatDivisionInf.java index 567e856ee3a..a5e037660ec 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/InvalidInstanceShouldNotRaiseErrorWhenFloatDivisionInf.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/InvalidInstanceShouldNotRaiseErrorWhenFloatDivisionInf.java @@ -8,6 +8,8 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; import org.openapijsonschematools.client.exceptions.InvalidTypeException; @@ -29,7 +31,7 @@ public static class InvalidInstanceShouldNotRaiseErrorWhenFloatDivisionInf1 exte Do not edit the class manually. */ - private static InvalidInstanceShouldNotRaiseErrorWhenFloatDivisionInf1 instance; + private static @Nullable InvalidInstanceShouldNotRaiseErrorWhenFloatDivisionInf1 instance = null; protected InvalidInstanceShouldNotRaiseErrorWhenFloatDivisionInf1() { super(new JsonSchemaInfo() @@ -78,11 +80,11 @@ public double validate(double arg, SchemaConfiguration configuration) throws Val } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Number) { return getNewInstance((Number) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/InvalidStringValueForDefault.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/InvalidStringValueForDefault.java index 90cb0806e06..9c23584e447 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/InvalidStringValueForDefault.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/InvalidStringValueForDefault.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -33,7 +37,7 @@ public class InvalidStringValueForDefault { public static class Bar extends JsonSchema implements StringSchemaValidator { - private static Bar instance; + private static @Nullable Bar instance = null; protected Bar() { super(new JsonSchemaInfo() @@ -63,33 +67,37 @@ public String validate(String arg, SchemaConfiguration configuration) throws Val } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof String) { return getNewInstance((String) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class InvalidStringValueForDefaultMap extends FrozenMap { - protected InvalidStringValueForDefaultMap(FrozenMap m) { + public static class InvalidStringValueForDefaultMap extends FrozenMap<@Nullable Object> { + protected InvalidStringValueForDefaultMap(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of(); public static final Set optionalKeys = Set.of( "bar" ); - public static InvalidStringValueForDefaultMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static InvalidStringValueForDefaultMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { return InvalidStringValueForDefault1.getInstance().validate(arg, configuration); } - public String bar() { + public String bar() throws UnsetPropertyException { String key = "bar"; throwIfKeyNotPresent(key); - return (String) get(key); + @Nullable Object value = get(key); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for bar"); + } + return (String) value; } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -100,14 +108,14 @@ public static class InvalidStringValueForDefaultMapInput { } - public static class InvalidStringValueForDefault1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { + public static class InvalidStringValueForDefault1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, InvalidStringValueForDefaultMap> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static InvalidStringValueForDefault1 instance; + private static @Nullable InvalidStringValueForDefault1 instance = null; protected InvalidStringValueForDefault1() { super(new JsonSchemaInfo() @@ -205,23 +213,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -233,22 +248,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public InvalidStringValueForDefaultMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new InvalidStringValueForDefaultMap(castProperties); } @Override - public InvalidStringValueForDefaultMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public InvalidStringValueForDefaultMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -261,7 +287,7 @@ public InvalidStringValueForDefaultMap validate(Map arg, SchemaC } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -276,7 +302,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Ipv4Format.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Ipv4Format.java index 9b7d1ee5945..1b301ab802b 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Ipv4Format.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Ipv4Format.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class Ipv4Format { // nest classes so all schemas and input/output classes can be public - public static class Ipv4Format1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class Ipv4Format1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static Ipv4Format1 instance; + private static @Nullable Ipv4Format1 instance = null; protected Ipv4Format1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Ipv6Format.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Ipv6Format.java index 7f783d25783..3026ad5b009 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Ipv6Format.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Ipv6Format.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class Ipv6Format { // nest classes so all schemas and input/output classes can be public - public static class Ipv6Format1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class Ipv6Format1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static Ipv6Format1 instance; + private static @Nullable Ipv6Format1 instance = null; protected Ipv6Format1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/JsonPointerFormat.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/JsonPointerFormat.java index 01bd1f3866b..d2e2e4214f1 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/JsonPointerFormat.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/JsonPointerFormat.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class JsonPointerFormat { // nest classes so all schemas and input/output classes can be public - public static class JsonPointerFormat1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class JsonPointerFormat1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static JsonPointerFormat1 instance; + private static @Nullable JsonPointerFormat1 instance = null; protected JsonPointerFormat1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MaximumValidation.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MaximumValidation.java index 59e9a5da39b..9dda17d0640 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MaximumValidation.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MaximumValidation.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class MaximumValidation { // nest classes so all schemas and input/output classes can be public - public static class MaximumValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class MaximumValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static MaximumValidation1 instance; + private static @Nullable MaximumValidation1 instance = null; protected MaximumValidation1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MaximumValidationWithUnsignedInteger.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MaximumValidationWithUnsignedInteger.java index 3ac95d2fa6d..17639327670 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MaximumValidationWithUnsignedInteger.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MaximumValidationWithUnsignedInteger.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class MaximumValidationWithUnsignedInteger { // nest classes so all schemas and input/output classes can be public - public static class MaximumValidationWithUnsignedInteger1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class MaximumValidationWithUnsignedInteger1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static MaximumValidationWithUnsignedInteger1 instance; + private static @Nullable MaximumValidationWithUnsignedInteger1 instance = null; protected MaximumValidationWithUnsignedInteger1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MaxitemsValidation.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MaxitemsValidation.java index 4aba4518783..0634fc92865 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MaxitemsValidation.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MaxitemsValidation.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class MaxitemsValidation { // nest classes so all schemas and input/output classes can be public - public static class MaxitemsValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class MaxitemsValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static MaxitemsValidation1 instance; + private static @Nullable MaxitemsValidation1 instance = null; protected MaxitemsValidation1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MaxlengthValidation.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MaxlengthValidation.java index 5661e5e0495..3b7c27dc500 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MaxlengthValidation.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MaxlengthValidation.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class MaxlengthValidation { // nest classes so all schemas and input/output classes can be public - public static class MaxlengthValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class MaxlengthValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static MaxlengthValidation1 instance; + private static @Nullable MaxlengthValidation1 instance = null; protected MaxlengthValidation1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Maxproperties0MeansTheObjectIsEmpty.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Maxproperties0MeansTheObjectIsEmpty.java index 1fa9056537c..4ec6d0b5225 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Maxproperties0MeansTheObjectIsEmpty.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Maxproperties0MeansTheObjectIsEmpty.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class Maxproperties0MeansTheObjectIsEmpty { // nest classes so all schemas and input/output classes can be public - public static class Maxproperties0MeansTheObjectIsEmpty1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class Maxproperties0MeansTheObjectIsEmpty1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static Maxproperties0MeansTheObjectIsEmpty1 instance; + private static @Nullable Maxproperties0MeansTheObjectIsEmpty1 instance = null; protected Maxproperties0MeansTheObjectIsEmpty1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MaxpropertiesValidation.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MaxpropertiesValidation.java index add351a25ca..ff98b6cb035 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MaxpropertiesValidation.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MaxpropertiesValidation.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class MaxpropertiesValidation { // nest classes so all schemas and input/output classes can be public - public static class MaxpropertiesValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class MaxpropertiesValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static MaxpropertiesValidation1 instance; + private static @Nullable MaxpropertiesValidation1 instance = null; protected MaxpropertiesValidation1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MinimumValidation.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MinimumValidation.java index 4048ad860b8..4edeb94f9c1 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MinimumValidation.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MinimumValidation.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class MinimumValidation { // nest classes so all schemas and input/output classes can be public - public static class MinimumValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class MinimumValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static MinimumValidation1 instance; + private static @Nullable MinimumValidation1 instance = null; protected MinimumValidation1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MinimumValidationWithSignedInteger.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MinimumValidationWithSignedInteger.java index b9d3966da9d..f0ae73bcbb1 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MinimumValidationWithSignedInteger.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MinimumValidationWithSignedInteger.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class MinimumValidationWithSignedInteger { // nest classes so all schemas and input/output classes can be public - public static class MinimumValidationWithSignedInteger1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class MinimumValidationWithSignedInteger1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static MinimumValidationWithSignedInteger1 instance; + private static @Nullable MinimumValidationWithSignedInteger1 instance = null; protected MinimumValidationWithSignedInteger1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MinitemsValidation.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MinitemsValidation.java index 9e14a51baf7..fe4ea46765f 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MinitemsValidation.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MinitemsValidation.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class MinitemsValidation { // nest classes so all schemas and input/output classes can be public - public static class MinitemsValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class MinitemsValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static MinitemsValidation1 instance; + private static @Nullable MinitemsValidation1 instance = null; protected MinitemsValidation1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MinlengthValidation.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MinlengthValidation.java index 1745c290a71..53675489452 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MinlengthValidation.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MinlengthValidation.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class MinlengthValidation { // nest classes so all schemas and input/output classes can be public - public static class MinlengthValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class MinlengthValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static MinlengthValidation1 instance; + private static @Nullable MinlengthValidation1 instance = null; protected MinlengthValidation1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MinpropertiesValidation.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MinpropertiesValidation.java index d6f2fdd3bd8..6bd419fdae6 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MinpropertiesValidation.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/MinpropertiesValidation.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class MinpropertiesValidation { // nest classes so all schemas and input/output classes can be public - public static class MinpropertiesValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class MinpropertiesValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static MinpropertiesValidation1 instance; + private static @Nullable MinpropertiesValidation1 instance = null; protected MinpropertiesValidation1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NestedAllofToCheckValidationSemantics.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NestedAllofToCheckValidationSemantics.java index 301858c8372..7233175e9fb 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NestedAllofToCheckValidationSemantics.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NestedAllofToCheckValidationSemantics.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.NullJsonSchema; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; @@ -35,8 +39,8 @@ public class NestedAllofToCheckValidationSemantics { public static class Schema01 extends NullJsonSchema {} - public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static Schema0 instance; + public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { + private static @Nullable Schema0 instance = null; protected Schema0() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,18 +227,18 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class NestedAllofToCheckValidationSemantics1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class NestedAllofToCheckValidationSemantics1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static NestedAllofToCheckValidationSemantics1 instance; + private static @Nullable NestedAllofToCheckValidationSemantics1 instance = null; protected NestedAllofToCheckValidationSemantics1() { super(new JsonSchemaInfo() @@ -314,23 +336,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -341,23 +370,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -370,7 +410,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -385,7 +425,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NestedAnyofToCheckValidationSemantics.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NestedAnyofToCheckValidationSemantics.java index f3a891e49e3..36e2a49bbaa 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NestedAnyofToCheckValidationSemantics.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NestedAnyofToCheckValidationSemantics.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.NullJsonSchema; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; @@ -35,8 +39,8 @@ public class NestedAnyofToCheckValidationSemantics { public static class Schema01 extends NullJsonSchema {} - public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static Schema0 instance; + public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { + private static @Nullable Schema0 instance = null; protected Schema0() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,18 +227,18 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class NestedAnyofToCheckValidationSemantics1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class NestedAnyofToCheckValidationSemantics1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static NestedAnyofToCheckValidationSemantics1 instance; + private static @Nullable NestedAnyofToCheckValidationSemantics1 instance = null; protected NestedAnyofToCheckValidationSemantics1() { super(new JsonSchemaInfo() @@ -314,23 +336,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -341,23 +370,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -370,7 +410,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -385,7 +425,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NestedItems.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NestedItems.java index 7e9ee5ee119..edc63c92f2b 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NestedItems.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NestedItems.java @@ -7,6 +7,8 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; import org.openapijsonschematools.client.exceptions.InvalidTypeException; @@ -41,7 +43,7 @@ public static class ItemsListInput { public static class Items2 extends JsonSchema implements ListSchemaValidator { - private static Items2 instance; + private static @Nullable Items2 instance = null; protected Items2() { super(new JsonSchemaInfo() @@ -64,9 +66,16 @@ public ItemsList getNewInstance(List arg, List pathToItem, PathToSche for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Number castItem = (Number) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Number)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((Number) itemInstance); i += 1; } FrozenList newInstanceItems = new FrozenList<>(items); @@ -85,11 +94,11 @@ public ItemsList validate(List arg, SchemaConfiguration configuration) t } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof List) { return getNewInstance((List) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } @@ -108,7 +117,7 @@ public static class ItemsListInput1 { public static class Items1 extends JsonSchema implements ListSchemaValidator, ItemsList1> { - private static Items1 instance; + private static @Nullable Items1 instance = null; protected Items1() { super(new JsonSchemaInfo() @@ -131,9 +140,16 @@ public ItemsList1 getNewInstance(List arg, List pathToItem, PathToSch for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - ItemsList castItem = (ItemsList) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof ItemsList)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((ItemsList) itemInstance); i += 1; } FrozenList newInstanceItems = new FrozenList<>(items); @@ -152,11 +168,11 @@ public ItemsList1 validate(List> arg, SchemaConfiguration configura } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof List) { return getNewInstance((List) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } @@ -175,7 +191,7 @@ public static class ItemsListInput2 { public static class Items extends JsonSchema implements ListSchemaValidator>, ItemsList2> { - private static Items instance; + private static @Nullable Items instance = null; protected Items() { super(new JsonSchemaInfo() @@ -198,9 +214,16 @@ public ItemsList2 getNewInstance(List arg, List pathToItem, PathToSch for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - ItemsList1 castItem = (ItemsList1) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof ItemsList1)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((ItemsList1) itemInstance); i += 1; } FrozenList newInstanceItems = new FrozenList<>(items); @@ -219,11 +242,11 @@ public ItemsList2 validate(List>> arg, SchemaConfiguration con } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof List) { return getNewInstance((List) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } @@ -248,7 +271,7 @@ public static class NestedItems1 extends JsonSchema implements ListSchemaValidat Do not edit the class manually. */ - private static NestedItems1 instance; + private static @Nullable NestedItems1 instance = null; protected NestedItems1() { super(new JsonSchemaInfo() @@ -271,9 +294,16 @@ public NestedItemsList getNewInstance(List arg, List pathToItem, Path for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - ItemsList2 castItem = (ItemsList2) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof ItemsList2)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((ItemsList2) itemInstance); i += 1; } FrozenList newInstanceItems = new FrozenList<>(items); @@ -292,11 +322,11 @@ public NestedItemsList validate(List>>> arg, SchemaConfig } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof List) { return getNewInstance((List) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NestedOneofToCheckValidationSemantics.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NestedOneofToCheckValidationSemantics.java index 1ce3354a98f..f2b3236b967 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NestedOneofToCheckValidationSemantics.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NestedOneofToCheckValidationSemantics.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.NullJsonSchema; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; @@ -35,8 +39,8 @@ public class NestedOneofToCheckValidationSemantics { public static class Schema01 extends NullJsonSchema {} - public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static Schema0 instance; + public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { + private static @Nullable Schema0 instance = null; protected Schema0() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,18 +227,18 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class NestedOneofToCheckValidationSemantics1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class NestedOneofToCheckValidationSemantics1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static NestedOneofToCheckValidationSemantics1 instance; + private static @Nullable NestedOneofToCheckValidationSemantics1 instance = null; protected NestedOneofToCheckValidationSemantics1() { super(new JsonSchemaInfo() @@ -314,23 +336,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -341,23 +370,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -370,7 +410,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -385,7 +425,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Not.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Not.java index 2a441ff1480..1dd677f5591 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Not.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Not.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.IntJsonSchema; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; @@ -35,14 +39,14 @@ public class Not { public static class Not2 extends IntJsonSchema {} - public static class Not1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class Not1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static Not1 instance; + private static @Nullable Not1 instance = null; protected Not1() { super(new JsonSchemaInfo() @@ -138,23 +142,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -165,23 +176,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -194,7 +216,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -209,7 +231,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NotMoreComplexSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NotMoreComplexSchema.java index 81080a1aabc..d0ce9f64db7 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NotMoreComplexSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NotMoreComplexSchema.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.StringJsonSchema; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; @@ -36,25 +40,29 @@ public class NotMoreComplexSchema { public static class Foo extends StringJsonSchema {} - public static class NotMap extends FrozenMap { - protected NotMap(FrozenMap m) { + public static class NotMap extends FrozenMap<@Nullable Object> { + protected NotMap(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of(); public static final Set optionalKeys = Set.of( "foo" ); - public static NotMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static NotMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { return Not.getInstance().validate(arg, configuration); } - public String foo() { + public String foo() throws UnsetPropertyException { String key = "foo"; throwIfKeyNotPresent(key); - return (String) get(key); + @Nullable Object value = get(key); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for foo"); + } + return (String) value; } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -65,8 +73,8 @@ public static class NotMapInput { } - public static class Not extends JsonSchema implements MapSchemaValidator { - private static Not instance; + public static class Not extends JsonSchema implements MapSchemaValidator<@Nullable Object, NotMap> { + private static @Nullable Not instance = null; protected Not() { super(new JsonSchemaInfo() @@ -85,22 +93,33 @@ public static Not getInstance() { } public NotMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new NotMap(castProperties); } @Override - public NotMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public NotMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -112,23 +131,23 @@ public NotMap validate(Map arg, SchemaConfiguration configuratio @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class NotMoreComplexSchema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class NotMoreComplexSchema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static NotMoreComplexSchema1 instance; + private static @Nullable NotMoreComplexSchema1 instance = null; protected NotMoreComplexSchema1() { super(new JsonSchemaInfo() @@ -224,23 +243,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -251,23 +277,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -280,7 +317,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -295,7 +332,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NulCharactersInStrings.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NulCharactersInStrings.java index 1238ba53d50..a29cfc8d980 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NulCharactersInStrings.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/NulCharactersInStrings.java @@ -7,6 +7,8 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; import org.openapijsonschematools.client.exceptions.InvalidTypeException; @@ -29,7 +31,7 @@ public static class NulCharactersInStrings1 extends JsonSchema implements String Do not edit the class manually. */ - private static NulCharactersInStrings1 instance; + private static @Nullable NulCharactersInStrings1 instance = null; protected NulCharactersInStrings1() { super(new JsonSchemaInfo() @@ -61,11 +63,11 @@ public String validate(String arg, SchemaConfiguration configuration) throws Val } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof String) { return getNewInstance((String) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/ObjectPropertiesValidation.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/ObjectPropertiesValidation.java index 9015e6a9f26..7167aa2d44e 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/ObjectPropertiesValidation.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/ObjectPropertiesValidation.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.IntJsonSchema; import org.openapijsonschematools.client.schemas.StringJsonSchema; @@ -40,8 +44,8 @@ public static class Foo extends IntJsonSchema {} public static class Bar extends StringJsonSchema {} - public static class ObjectPropertiesValidationMap extends FrozenMap { - protected ObjectPropertiesValidationMap(FrozenMap m) { + public static class ObjectPropertiesValidationMap extends FrozenMap<@Nullable Object> { + protected ObjectPropertiesValidationMap(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of(); @@ -49,23 +53,31 @@ protected ObjectPropertiesValidationMap(FrozenMap m) { "foo", "bar" ); - public static ObjectPropertiesValidationMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static ObjectPropertiesValidationMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { return ObjectPropertiesValidation1.getInstance().validate(arg, configuration); } - public long foo() { + public long foo() throws UnsetPropertyException { String key = "foo"; throwIfKeyNotPresent(key); - return (long) get(key); + @Nullable Object value = get(key); + if (!(value instanceof Long)) { + throw new InvalidTypeException("Invalid value stored for foo"); + } + return (long) value; } - public String bar() { + public String bar() throws UnsetPropertyException { String key = "bar"; throwIfKeyNotPresent(key); - return (String) get(key); + @Nullable Object value = get(key); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for bar"); + } + return (String) value; } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -76,14 +88,14 @@ public static class ObjectPropertiesValidationMapInput { } - public static class ObjectPropertiesValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { + public static class ObjectPropertiesValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, ObjectPropertiesValidationMap> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static ObjectPropertiesValidation1 instance; + private static @Nullable ObjectPropertiesValidation1 instance = null; protected ObjectPropertiesValidation1() { super(new JsonSchemaInfo() @@ -182,23 +194,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -210,22 +229,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public ObjectPropertiesValidationMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new ObjectPropertiesValidationMap(castProperties); } @Override - public ObjectPropertiesValidationMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public ObjectPropertiesValidationMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -238,7 +268,7 @@ public ObjectPropertiesValidationMap validate(Map arg, SchemaCon } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -253,7 +283,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Oneof.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Oneof.java index 892c768aef5..be5af91a643 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Oneof.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/Oneof.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.IntJsonSchema; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; @@ -35,8 +39,8 @@ public class Oneof { public static class Schema0 extends IntJsonSchema {} - public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static Schema1 instance; + public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { + private static @Nullable Schema1 instance = null; protected Schema1() { super(new JsonSchemaInfo() @@ -132,23 +136,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -159,23 +170,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -188,7 +210,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -203,18 +225,18 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class Oneof1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class Oneof1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static Oneof1 instance; + private static @Nullable Oneof1 instance = null; protected Oneof1() { super(new JsonSchemaInfo() @@ -313,23 +335,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -340,23 +369,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -369,7 +409,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -384,7 +424,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/OneofComplexTypes.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/OneofComplexTypes.java index d0a909be7c4..30eaee8d662 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/OneofComplexTypes.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/OneofComplexTypes.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.IntJsonSchema; import org.openapijsonschematools.client.schemas.StringJsonSchema; @@ -37,23 +41,27 @@ public class OneofComplexTypes { public static class Bar extends IntJsonSchema {} - public static class Schema0Map extends FrozenMap { - protected Schema0Map(FrozenMap m) { + public static class Schema0Map extends FrozenMap<@Nullable Object> { + protected Schema0Map(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of( "bar" ); public static final Set optionalKeys = Set.of(); - public static Schema0Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static Schema0Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { return Schema0.getInstance().validate(arg, configuration); } public long bar() { - return (long) get("bar"); + @Nullable Object value = get("bar"); + if (!(value instanceof Long)) { + throw new InvalidTypeException("Invalid value stored for bar"); + } + return (long) value; } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -64,8 +72,8 @@ public static class Schema0MapInput { } - public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { - private static Schema0 instance; + public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, Schema0Map> { + private static @Nullable Schema0 instance = null; protected Schema0() { super(new JsonSchemaInfo() @@ -166,23 +174,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -194,22 +209,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public Schema0Map getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new Schema0Map(castProperties); } @Override - public Schema0Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public Schema0Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -222,7 +248,7 @@ public Schema0Map validate(Map arg, SchemaConfiguration configur } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -237,30 +263,34 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } public static class Foo extends StringJsonSchema {} - public static class Schema1Map extends FrozenMap { - protected Schema1Map(FrozenMap m) { + public static class Schema1Map extends FrozenMap<@Nullable Object> { + protected Schema1Map(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of( "foo" ); public static final Set optionalKeys = Set.of(); - public static Schema1Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static Schema1Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { return Schema1.getInstance().validate(arg, configuration); } public String foo() { - return (String) get("foo"); + @Nullable Object value = get("foo"); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for foo"); + } + return (String) value; } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -271,8 +301,8 @@ public static class Schema1MapInput { } - public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { - private static Schema1 instance; + public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, Schema1Map> { + private static @Nullable Schema1 instance = null; protected Schema1() { super(new JsonSchemaInfo() @@ -373,23 +403,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -401,22 +438,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public Schema1Map getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new Schema1Map(castProperties); } @Override - public Schema1Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public Schema1Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -429,7 +477,7 @@ public Schema1Map validate(Map arg, SchemaConfiguration configur } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -444,18 +492,18 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class OneofComplexTypes1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class OneofComplexTypes1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static OneofComplexTypes1 instance; + private static @Nullable OneofComplexTypes1 instance = null; protected OneofComplexTypes1() { super(new JsonSchemaInfo() @@ -554,23 +602,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -581,23 +636,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -610,7 +676,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -625,7 +691,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/OneofWithBaseSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/OneofWithBaseSchema.java index 83fe1b39b69..7e571ae0cb1 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/OneofWithBaseSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/OneofWithBaseSchema.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,8 +35,8 @@ public class OneofWithBaseSchema { // nest classes so all schemas and input/output classes can be public - public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static Schema0 instance; + public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { + private static @Nullable Schema0 instance = null; protected Schema0() { super(new JsonSchemaInfo() @@ -128,23 +132,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -155,23 +166,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -184,7 +206,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -199,12 +221,12 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static Schema1 instance; + public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { + private static @Nullable Schema1 instance = null; protected Schema1() { super(new JsonSchemaInfo() @@ -300,23 +322,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -327,23 +356,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -356,7 +396,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -371,7 +411,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } @@ -382,7 +422,7 @@ public static class OneofWithBaseSchema1 extends JsonSchema implements StringSch Do not edit the class manually. */ - private static OneofWithBaseSchema1 instance; + private static @Nullable OneofWithBaseSchema1 instance = null; protected OneofWithBaseSchema1() { super(new JsonSchemaInfo() @@ -415,11 +455,11 @@ public String validate(String arg, SchemaConfiguration configuration) throws Val } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof String) { return getNewInstance((String) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/OneofWithEmptySchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/OneofWithEmptySchema.java index 0defb2ce1fb..eb31dbd7e59 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/OneofWithEmptySchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/OneofWithEmptySchema.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; import org.openapijsonschematools.client.schemas.NumberJsonSchema; @@ -39,14 +43,14 @@ public static class Schema0 extends NumberJsonSchema {} public static class Schema1 extends AnyTypeJsonSchema {} - public static class OneofWithEmptySchema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class OneofWithEmptySchema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static OneofWithEmptySchema1 instance; + private static @Nullable OneofWithEmptySchema1 instance = null; protected OneofWithEmptySchema1() { super(new JsonSchemaInfo() @@ -145,23 +149,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -172,23 +183,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -201,7 +223,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -216,7 +238,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/OneofWithRequired.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/OneofWithRequired.java index 27e8589e059..c2c71d07ca3 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/OneofWithRequired.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/OneofWithRequired.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,8 +35,8 @@ public class OneofWithRequired { // nest classes so all schemas and input/output classes can be public - public static class Schema0Map extends FrozenMap { - protected Schema0Map(FrozenMap m) { + public static class Schema0Map extends FrozenMap<@Nullable Object> { + protected Schema0Map(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of( @@ -40,19 +44,19 @@ protected Schema0Map(FrozenMap m) { "foo" ); public static final Set optionalKeys = Set.of(); - public static Schema0Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static Schema0Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { return Schema0.getInstance().validate(arg, configuration); } - public Object bar() { + public @Nullable Object bar() { return get("bar"); } - public Object foo() { + public @Nullable Object foo() { return get("foo"); } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -63,8 +67,8 @@ public static class Schema0MapInput { } - public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { - private static Schema0 instance; + public static class Schema0 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, Schema0Map> { + private static @Nullable Schema0 instance = null; protected Schema0() { super(new JsonSchemaInfo() @@ -163,23 +167,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -191,22 +202,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public Schema0Map getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new Schema0Map(castProperties); } @Override - public Schema0Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public Schema0Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -219,7 +241,7 @@ public Schema0Map validate(Map arg, SchemaConfiguration configur } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -234,12 +256,12 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class Schema1Map extends FrozenMap { - protected Schema1Map(FrozenMap m) { + public static class Schema1Map extends FrozenMap<@Nullable Object> { + protected Schema1Map(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of( @@ -247,19 +269,19 @@ protected Schema1Map(FrozenMap m) { "foo" ); public static final Set optionalKeys = Set.of(); - public static Schema1Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static Schema1Map of(Map arg, SchemaConfiguration configuration) throws ValidationException { return Schema1.getInstance().validate(arg, configuration); } - public Object baz() { + public @Nullable Object baz() { return get("baz"); } - public Object foo() { + public @Nullable Object foo() { return get("foo"); } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -270,8 +292,8 @@ public static class Schema1MapInput { } - public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { - private static Schema1 instance; + public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, Schema1Map> { + private static @Nullable Schema1 instance = null; protected Schema1() { super(new JsonSchemaInfo() @@ -370,23 +392,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -398,22 +427,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public Schema1Map getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new Schema1Map(castProperties); } @Override - public Schema1Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public Schema1Map validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -426,7 +466,7 @@ public Schema1Map validate(Map arg, SchemaConfiguration configur } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -441,18 +481,18 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class OneofWithRequired1 extends JsonSchema implements MapSchemaValidator> { + public static class OneofWithRequired1 extends JsonSchema implements MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static OneofWithRequired1 instance; + private static @Nullable OneofWithRequired1 instance = null; protected OneofWithRequired1() { super(new JsonSchemaInfo() @@ -471,23 +511,34 @@ public static OneofWithRequired1 getInstance() { return instance; } - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -499,11 +550,11 @@ public FrozenMap validate(Map arg, SchemaConfiguration c @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/PatternIsNotAnchored.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/PatternIsNotAnchored.java index 26c01a11cd9..954c7857375 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/PatternIsNotAnchored.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/PatternIsNotAnchored.java @@ -11,9 +11,13 @@ import java.util.Set; import java.util.UUID; import java.util.regex.Pattern; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -32,14 +36,14 @@ public class PatternIsNotAnchored { // nest classes so all schemas and input/output classes can be public - public static class PatternIsNotAnchored1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class PatternIsNotAnchored1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static PatternIsNotAnchored1 instance; + private static @Nullable PatternIsNotAnchored1 instance = null; protected PatternIsNotAnchored1() { super(new JsonSchemaInfo() @@ -137,23 +141,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -164,23 +175,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -193,7 +215,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -208,7 +230,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/PatternValidation.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/PatternValidation.java index ca8e5fbc088..52cded92e58 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/PatternValidation.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/PatternValidation.java @@ -11,9 +11,13 @@ import java.util.Set; import java.util.UUID; import java.util.regex.Pattern; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -32,14 +36,14 @@ public class PatternValidation { // nest classes so all schemas and input/output classes can be public - public static class PatternValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class PatternValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static PatternValidation1 instance; + private static @Nullable PatternValidation1 instance = null; protected PatternValidation1() { super(new JsonSchemaInfo() @@ -137,23 +141,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -164,23 +175,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -193,7 +215,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -208,7 +230,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/PropertiesWithEscapedCharacters.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/PropertiesWithEscapedCharacters.java index 21f9a8a381a..d99673b9a30 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/PropertiesWithEscapedCharacters.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/PropertiesWithEscapedCharacters.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.NumberJsonSchema; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; @@ -51,8 +55,8 @@ public static class Footbar extends NumberJsonSchema {} public static class Foofbar extends NumberJsonSchema {} - public static class PropertiesWithEscapedCharactersMap extends FrozenMap { - protected PropertiesWithEscapedCharactersMap(FrozenMap m) { + public static class PropertiesWithEscapedCharactersMap extends FrozenMap<@Nullable Object> { + protected PropertiesWithEscapedCharactersMap(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of(); @@ -64,11 +68,11 @@ protected PropertiesWithEscapedCharactersMap(FrozenMap m) { "foo\tbar", "foo\fbar" ); - public static PropertiesWithEscapedCharactersMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static PropertiesWithEscapedCharactersMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { return PropertiesWithEscapedCharacters1.getInstance().validate(arg, configuration); } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -79,14 +83,14 @@ public static class PropertiesWithEscapedCharactersMapInput { } - public static class PropertiesWithEscapedCharacters1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { + public static class PropertiesWithEscapedCharacters1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, PropertiesWithEscapedCharactersMap> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static PropertiesWithEscapedCharacters1 instance; + private static @Nullable PropertiesWithEscapedCharacters1 instance = null; protected PropertiesWithEscapedCharacters1() { super(new JsonSchemaInfo() @@ -189,23 +193,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -217,22 +228,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public PropertiesWithEscapedCharactersMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new PropertiesWithEscapedCharactersMap(castProperties); } @Override - public PropertiesWithEscapedCharactersMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public PropertiesWithEscapedCharactersMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -245,7 +267,7 @@ public PropertiesWithEscapedCharactersMap validate(Map arg, Sche } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -260,7 +282,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/PropertyNamedRefThatIsNotAReference.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/PropertyNamedRefThatIsNotAReference.java index e411d623af2..e405f9c53c0 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/PropertyNamedRefThatIsNotAReference.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/PropertyNamedRefThatIsNotAReference.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.StringJsonSchema; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; @@ -36,19 +40,19 @@ public class PropertyNamedRefThatIsNotAReference { public static class Ref extends StringJsonSchema {} - public static class PropertyNamedRefThatIsNotAReferenceMap extends FrozenMap { - protected PropertyNamedRefThatIsNotAReferenceMap(FrozenMap m) { + public static class PropertyNamedRefThatIsNotAReferenceMap extends FrozenMap<@Nullable Object> { + protected PropertyNamedRefThatIsNotAReferenceMap(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of(); public static final Set optionalKeys = Set.of( "$ref" ); - public static PropertyNamedRefThatIsNotAReferenceMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static PropertyNamedRefThatIsNotAReferenceMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { return PropertyNamedRefThatIsNotAReference1.getInstance().validate(arg, configuration); } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -59,14 +63,14 @@ public static class PropertyNamedRefThatIsNotAReferenceMapInput { } - public static class PropertyNamedRefThatIsNotAReference1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { + public static class PropertyNamedRefThatIsNotAReference1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, PropertyNamedRefThatIsNotAReferenceMap> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static PropertyNamedRefThatIsNotAReference1 instance; + private static @Nullable PropertyNamedRefThatIsNotAReference1 instance = null; protected PropertyNamedRefThatIsNotAReference1() { super(new JsonSchemaInfo() @@ -164,23 +168,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -192,22 +203,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public PropertyNamedRefThatIsNotAReferenceMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new PropertyNamedRefThatIsNotAReferenceMap(castProperties); } @Override - public PropertyNamedRefThatIsNotAReferenceMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public PropertyNamedRefThatIsNotAReferenceMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -220,7 +242,7 @@ public PropertyNamedRefThatIsNotAReferenceMap validate(Map arg, } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -235,7 +257,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInAdditionalproperties.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInAdditionalproperties.java index 6cb7dad2acf..7ec239e49fe 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInAdditionalproperties.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInAdditionalproperties.java @@ -7,9 +7,13 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.FrozenMap; import org.openapijsonschematools.client.schemas.validation.JsonSchema; @@ -22,17 +26,17 @@ public class RefInAdditionalproperties { // nest classes so all schemas and input/output classes can be public - public static class RefInAdditionalpropertiesMap extends FrozenMap { - protected RefInAdditionalpropertiesMap(FrozenMap m) { + public static class RefInAdditionalpropertiesMap extends FrozenMap<@Nullable Object> { + protected RefInAdditionalpropertiesMap(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of(); public static final Set optionalKeys = Set.of(); - public static RefInAdditionalpropertiesMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static RefInAdditionalpropertiesMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { return RefInAdditionalproperties1.getInstance().validate(arg, configuration); } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException { throwIfKeyNotPresent(name); return get(name); } @@ -42,14 +46,14 @@ public static class RefInAdditionalpropertiesMapInput { } - public static class RefInAdditionalproperties1 extends JsonSchema implements MapSchemaValidator { + public static class RefInAdditionalproperties1 extends JsonSchema implements MapSchemaValidator<@Nullable Object, RefInAdditionalpropertiesMap> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static RefInAdditionalproperties1 instance; + private static @Nullable RefInAdditionalproperties1 instance = null; protected RefInAdditionalproperties1() { super(new JsonSchemaInfo() @@ -66,22 +70,33 @@ public static RefInAdditionalproperties1 getInstance() { } public RefInAdditionalpropertiesMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new RefInAdditionalpropertiesMap(castProperties); } @Override - public RefInAdditionalpropertiesMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public RefInAdditionalpropertiesMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -93,11 +108,11 @@ public RefInAdditionalpropertiesMap validate(Map arg, SchemaConf @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInAllof.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInAllof.java index 9342f3430fa..c4a36a525f2 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInAllof.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInAllof.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class RefInAllof { // nest classes so all schemas and input/output classes can be public - public static class RefInAllof1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class RefInAllof1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static RefInAllof1 instance; + private static @Nullable RefInAllof1 instance = null; protected RefInAllof1() { super(new JsonSchemaInfo() @@ -136,23 +140,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -163,23 +174,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -192,7 +214,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -207,7 +229,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInAnyof.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInAnyof.java index 7584c5eb76e..236552fbf70 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInAnyof.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInAnyof.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class RefInAnyof { // nest classes so all schemas and input/output classes can be public - public static class RefInAnyof1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class RefInAnyof1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static RefInAnyof1 instance; + private static @Nullable RefInAnyof1 instance = null; protected RefInAnyof1() { super(new JsonSchemaInfo() @@ -136,23 +140,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -163,23 +174,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -192,7 +214,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -207,7 +229,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInItems.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInItems.java index 5732b7bd049..ef6a6ef9127 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInItems.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInItems.java @@ -7,6 +7,8 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; import org.openapijsonschematools.client.exceptions.InvalidTypeException; @@ -22,28 +24,28 @@ public class RefInItems { // nest classes so all schemas and input/output classes can be public - public static class RefInItemsList extends FrozenList { - protected RefInItemsList(FrozenList m) { + public static class RefInItemsList extends FrozenList<@Nullable Object> { + protected RefInItemsList(FrozenList<@Nullable Object> m) { super(m); } - public static RefInItemsList of(List arg, SchemaConfiguration configuration) throws ValidationException { + public static RefInItemsList of(List<@Nullable Object> arg, SchemaConfiguration configuration) throws ValidationException { return RefInItems1.getInstance().validate(arg, configuration); } } public static class RefInItemsListInput { - // class to build List + // class to build List<@Nullable Object> } - public static class RefInItems1 extends JsonSchema implements ListSchemaValidator { + public static class RefInItems1 extends JsonSchema implements ListSchemaValidator<@Nullable Object, RefInItemsList> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static RefInItems1 instance; + private static @Nullable RefInItems1 instance = null; protected RefInItems1() { super(new JsonSchemaInfo() @@ -61,22 +63,29 @@ public static RefInItems1 getInstance() { @Override public RefInItemsList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return new RefInItemsList(newInstanceItems); } @Override - public RefInItemsList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public RefInItemsList validate(List<@Nullable Object> arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -87,11 +96,11 @@ public RefInItemsList validate(List arg, SchemaConfiguration configurati } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof List) { return getNewInstance((List) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInNot.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInNot.java index d3c85a028a9..b0000e068d0 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInNot.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInNot.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class RefInNot { // nest classes so all schemas and input/output classes can be public - public static class RefInNot1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class RefInNot1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static RefInNot1 instance; + private static @Nullable RefInNot1 instance = null; protected RefInNot1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInOneof.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInOneof.java index ea2cca6d2a8..5385f481ff8 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInOneof.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInOneof.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class RefInOneof { // nest classes so all schemas and input/output classes can be public - public static class RefInOneof1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class RefInOneof1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static RefInOneof1 instance; + private static @Nullable RefInOneof1 instance = null; protected RefInOneof1() { super(new JsonSchemaInfo() @@ -136,23 +140,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -163,23 +174,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -192,7 +214,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -207,7 +229,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInProperty.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInProperty.java index 4bb6912bf0b..879c02d7fbc 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInProperty.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RefInProperty.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -32,25 +36,29 @@ public class RefInProperty { // nest classes so all schemas and input/output classes can be public - public static class RefInPropertyMap extends FrozenMap { - protected RefInPropertyMap(FrozenMap m) { + public static class RefInPropertyMap extends FrozenMap<@Nullable Object> { + protected RefInPropertyMap(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of(); public static final Set optionalKeys = Set.of( "a" ); - public static RefInPropertyMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static RefInPropertyMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { return RefInProperty1.getInstance().validate(arg, configuration); } - public Object a() { + public @Nullable Object a() throws UnsetPropertyException { String key = "a"; throwIfKeyNotPresent(key); - return (Object) get(key); + @Nullable Object value = get(key); + if (!(value instanceof Object)) { + throw new InvalidTypeException("Invalid value stored for a"); + } + return (@Nullable Object) value; } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -61,14 +69,14 @@ public static class RefInPropertyMapInput { } - public static class RefInProperty1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { + public static class RefInProperty1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, RefInPropertyMap> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static RefInProperty1 instance; + private static @Nullable RefInProperty1 instance = null; protected RefInProperty1() { super(new JsonSchemaInfo() @@ -166,23 +174,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -194,22 +209,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public RefInPropertyMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new RefInPropertyMap(castProperties); } @Override - public RefInPropertyMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public RefInPropertyMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -222,7 +248,7 @@ public RefInPropertyMap validate(Map arg, SchemaConfiguration co } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -237,7 +263,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RequiredDefaultValidation.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RequiredDefaultValidation.java index 5c2bea749d9..c41cb3c672d 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RequiredDefaultValidation.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RequiredDefaultValidation.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; @@ -36,25 +40,25 @@ public class RequiredDefaultValidation { public static class Foo extends AnyTypeJsonSchema {} - public static class RequiredDefaultValidationMap extends FrozenMap { - protected RequiredDefaultValidationMap(FrozenMap m) { + public static class RequiredDefaultValidationMap extends FrozenMap<@Nullable Object> { + protected RequiredDefaultValidationMap(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of(); public static final Set optionalKeys = Set.of( "foo" ); - public static RequiredDefaultValidationMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static RequiredDefaultValidationMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { return RequiredDefaultValidation1.getInstance().validate(arg, configuration); } - public Object foo() { + public @Nullable Object foo() throws UnsetPropertyException { String key = "foo"; throwIfKeyNotPresent(key); return get(key); } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -65,14 +69,14 @@ public static class RequiredDefaultValidationMapInput { } - public static class RequiredDefaultValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { + public static class RequiredDefaultValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, RequiredDefaultValidationMap> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static RequiredDefaultValidation1 instance; + private static @Nullable RequiredDefaultValidation1 instance = null; protected RequiredDefaultValidation1() { super(new JsonSchemaInfo() @@ -170,23 +174,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -198,22 +209,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public RequiredDefaultValidationMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new RequiredDefaultValidationMap(castProperties); } @Override - public RequiredDefaultValidationMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public RequiredDefaultValidationMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -226,7 +248,7 @@ public RequiredDefaultValidationMap validate(Map arg, SchemaConf } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -241,7 +263,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RequiredValidation.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RequiredValidation.java index f4e310abc42..584bc53d8dd 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RequiredValidation.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RequiredValidation.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; @@ -39,8 +43,8 @@ public static class Foo extends AnyTypeJsonSchema {} public static class Bar extends AnyTypeJsonSchema {} - public static class RequiredValidationMap extends FrozenMap { - protected RequiredValidationMap(FrozenMap m) { + public static class RequiredValidationMap extends FrozenMap<@Nullable Object> { + protected RequiredValidationMap(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of( @@ -49,21 +53,21 @@ protected RequiredValidationMap(FrozenMap m) { public static final Set optionalKeys = Set.of( "bar" ); - public static RequiredValidationMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static RequiredValidationMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { return RequiredValidation1.getInstance().validate(arg, configuration); } - public Object foo() { + public @Nullable Object foo() { return get("foo"); } - public Object bar() { + public @Nullable Object bar() throws UnsetPropertyException { String key = "bar"; throwIfKeyNotPresent(key); return get(key); } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -74,14 +78,14 @@ public static class RequiredValidationMapInput { } - public static class RequiredValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { + public static class RequiredValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, RequiredValidationMap> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static RequiredValidation1 instance; + private static @Nullable RequiredValidation1 instance = null; protected RequiredValidation1() { super(new JsonSchemaInfo() @@ -183,23 +187,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -211,22 +222,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public RequiredValidationMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new RequiredValidationMap(castProperties); } @Override - public RequiredValidationMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public RequiredValidationMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -239,7 +261,7 @@ public RequiredValidationMap validate(Map arg, SchemaConfigurati } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -254,7 +276,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RequiredWithEmptyArray.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RequiredWithEmptyArray.java index bed0df83550..e0bd68bdcec 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RequiredWithEmptyArray.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RequiredWithEmptyArray.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; @@ -36,25 +40,25 @@ public class RequiredWithEmptyArray { public static class Foo extends AnyTypeJsonSchema {} - public static class RequiredWithEmptyArrayMap extends FrozenMap { - protected RequiredWithEmptyArrayMap(FrozenMap m) { + public static class RequiredWithEmptyArrayMap extends FrozenMap<@Nullable Object> { + protected RequiredWithEmptyArrayMap(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of(); public static final Set optionalKeys = Set.of( "foo" ); - public static RequiredWithEmptyArrayMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static RequiredWithEmptyArrayMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { return RequiredWithEmptyArray1.getInstance().validate(arg, configuration); } - public Object foo() { + public @Nullable Object foo() throws UnsetPropertyException { String key = "foo"; throwIfKeyNotPresent(key); return get(key); } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -65,14 +69,14 @@ public static class RequiredWithEmptyArrayMapInput { } - public static class RequiredWithEmptyArray1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { + public static class RequiredWithEmptyArray1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, RequiredWithEmptyArrayMap> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static RequiredWithEmptyArray1 instance; + private static @Nullable RequiredWithEmptyArray1 instance = null; protected RequiredWithEmptyArray1() { super(new JsonSchemaInfo() @@ -170,23 +174,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -198,22 +209,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public RequiredWithEmptyArrayMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new RequiredWithEmptyArrayMap(castProperties); } @Override - public RequiredWithEmptyArrayMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public RequiredWithEmptyArrayMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -226,7 +248,7 @@ public RequiredWithEmptyArrayMap validate(Map arg, SchemaConfigu } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -241,7 +263,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RequiredWithEscapedCharacters.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RequiredWithEscapedCharacters.java index ab9bda16152..3db2129194c 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RequiredWithEscapedCharacters.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/RequiredWithEscapedCharacters.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,8 +35,8 @@ public class RequiredWithEscapedCharacters { // nest classes so all schemas and input/output classes can be public - public static class RequiredWithEscapedCharactersMap extends FrozenMap { - protected RequiredWithEscapedCharactersMap(FrozenMap m) { + public static class RequiredWithEscapedCharactersMap extends FrozenMap<@Nullable Object> { + protected RequiredWithEscapedCharactersMap(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of( @@ -44,11 +48,11 @@ protected RequiredWithEscapedCharactersMap(FrozenMap m) { "foo\\bar" ); public static final Set optionalKeys = Set.of(); - public static RequiredWithEscapedCharactersMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static RequiredWithEscapedCharactersMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { return RequiredWithEscapedCharacters1.getInstance().validate(arg, configuration); } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -59,14 +63,14 @@ public static class RequiredWithEscapedCharactersMapInput { } - public static class RequiredWithEscapedCharacters1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator { + public static class RequiredWithEscapedCharacters1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, RequiredWithEscapedCharactersMap> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static RequiredWithEscapedCharacters1 instance; + private static @Nullable RequiredWithEscapedCharacters1 instance = null; protected RequiredWithEscapedCharacters1() { super(new JsonSchemaInfo() @@ -169,23 +173,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -197,22 +208,33 @@ public FrozenList validate(List arg, SchemaConfiguration configu @Override public RequiredWithEscapedCharactersMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new RequiredWithEscapedCharactersMap(castProperties); } @Override - public RequiredWithEscapedCharactersMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public RequiredWithEscapedCharactersMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -225,7 +247,7 @@ public RequiredWithEscapedCharactersMap validate(Map arg, Schema } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -240,7 +262,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/SimpleEnumValidation.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/SimpleEnumValidation.java index 5472afa0ae5..b6a62e42134 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/SimpleEnumValidation.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/SimpleEnumValidation.java @@ -7,6 +7,8 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; import org.openapijsonschematools.client.exceptions.InvalidTypeException; @@ -29,7 +31,7 @@ public static class SimpleEnumValidation1 extends JsonSchema implements NumberSc Do not edit the class manually. */ - private static SimpleEnumValidation1 instance; + private static @Nullable SimpleEnumValidation1 instance = null; protected SimpleEnumValidation1() { super(new JsonSchemaInfo() @@ -81,11 +83,11 @@ public double validate(double arg, SchemaConfiguration configuration) throws Val } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Number) { return getNewInstance((Number) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing.java index 693324d0e5d..88ca2446c85 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing.java @@ -7,9 +7,13 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.FrozenMap; import org.openapijsonschematools.client.schemas.validation.JsonSchema; @@ -25,7 +29,7 @@ public class TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing { public static class Alpha extends JsonSchema implements NumberSchemaValidator { - private static Alpha instance; + private static @Nullable Alpha instance = null; protected Alpha() { super(new JsonSchemaInfo() @@ -73,33 +77,37 @@ public double validate(double arg, SchemaConfiguration configuration) throws Val } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Number) { return getNewInstance((Number) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMap extends FrozenMap { - protected TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMap(FrozenMap m) { + public static class TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMap extends FrozenMap<@Nullable Object> { + protected TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMap(FrozenMap<@Nullable Object> m) { super(m); } public static final Set requiredKeys = Set.of(); public static final Set optionalKeys = Set.of( "alpha" ); - public static TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + public static TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { return TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing1.getInstance().validate(arg, configuration); } - public Number alpha() { + public Number alpha() throws UnsetPropertyException { String key = "alpha"; throwIfKeyNotPresent(key); - return (Number) get(key); + @Nullable Object value = get(key); + if (!(value instanceof Number)) { + throw new InvalidTypeException("Invalid value stored for alpha"); + } + return (Number) value; } - public Object getAdditionalProperty(String name) { + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { throwIfKeyKnown(name, requiredKeys, optionalKeys); throwIfKeyNotPresent(name); return get(name); @@ -110,14 +118,14 @@ public static class TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMapI } - public static class TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing1 extends JsonSchema implements MapSchemaValidator { + public static class TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing1 extends JsonSchema implements MapSchemaValidator<@Nullable Object, TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMap> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing1 instance; + private static @Nullable TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing1 instance = null; protected TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing1() { super(new JsonSchemaInfo() @@ -136,22 +144,33 @@ public static TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissing1 getInsta } public TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return new TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMap(castProperties); } @Override - public TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -163,11 +182,11 @@ public TheDefaultKeywordDoesNotDoAnythingIfThePropertyIsMissingMap validate(Map< @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/UniqueitemsFalseValidation.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/UniqueitemsFalseValidation.java index 569f94d5fd6..f39eea6a951 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/UniqueitemsFalseValidation.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/UniqueitemsFalseValidation.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class UniqueitemsFalseValidation { // nest classes so all schemas and input/output classes can be public - public static class UniqueitemsFalseValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class UniqueitemsFalseValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static UniqueitemsFalseValidation1 instance; + private static @Nullable UniqueitemsFalseValidation1 instance = null; protected UniqueitemsFalseValidation1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/UniqueitemsValidation.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/UniqueitemsValidation.java index 317cf9c3651..f80ee4705d4 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/UniqueitemsValidation.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/UniqueitemsValidation.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class UniqueitemsValidation { // nest classes so all schemas and input/output classes can be public - public static class UniqueitemsValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class UniqueitemsValidation1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static UniqueitemsValidation1 instance; + private static @Nullable UniqueitemsValidation1 instance = null; protected UniqueitemsValidation1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/UriFormat.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/UriFormat.java index 85c4e34acf7..110daefde83 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/UriFormat.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/UriFormat.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class UriFormat { // nest classes so all schemas and input/output classes can be public - public static class UriFormat1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class UriFormat1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static UriFormat1 instance; + private static @Nullable UriFormat1 instance = null; protected UriFormat1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/UriReferenceFormat.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/UriReferenceFormat.java index c517ee5d9ea..8f86e0bcdf2 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/UriReferenceFormat.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/UriReferenceFormat.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class UriReferenceFormat { // nest classes so all schemas and input/output classes can be public - public static class UriReferenceFormat1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class UriReferenceFormat1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static UriReferenceFormat1 instance; + private static @Nullable UriReferenceFormat1 instance = null; protected UriReferenceFormat1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/UriTemplateFormat.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/UriTemplateFormat.java index dd2ffd0246d..0fd1db648d4 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/UriTemplateFormat.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/components/schemas/UriTemplateFormat.java @@ -10,9 +10,13 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.FrozenList; @@ -31,14 +35,14 @@ public class UriTemplateFormat { // nest classes so all schemas and input/output classes can be public - public static class UriTemplateFormat1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + public static class UriTemplateFormat1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<@Nullable Object, FrozenList<@Nullable Object>>, MapSchemaValidator<@Nullable Object, FrozenMap<@Nullable Object>> { /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator Do not edit the class manually. */ - private static UriTemplateFormat1 instance; + private static @Nullable UriTemplateFormat1 instance = null; protected UriTemplateFormat1() { super(new JsonSchemaInfo() @@ -134,23 +138,30 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = (Object) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((@Nullable Object) itemInstance); i += 1; } - FrozenList newInstanceItems = new FrozenList<>(items); + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); return newInstanceItems; } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -161,23 +172,34 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = (Object) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof Object)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (@Nullable Object) propertyInstance); } - FrozenMap castProperties = new FrozenMap<>(properties); + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); return castProperties; } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -190,7 +212,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -205,7 +227,7 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/exceptions/BaseException.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/exceptions/BaseException.java index 4fd46019607..3d7b352080c 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/exceptions/BaseException.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/exceptions/BaseException.java @@ -1,4 +1,5 @@ package org.openapijsonschematools.client.exceptions; +@SuppressWarnings("serial") public class BaseException extends RuntimeException { } \ No newline at end of file diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/exceptions/InvalidAdditionalPropertyException.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/exceptions/InvalidAdditionalPropertyException.java index 06e1596b99e..7f0805b1be8 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/exceptions/InvalidAdditionalPropertyException.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/exceptions/InvalidAdditionalPropertyException.java @@ -1,5 +1,6 @@ package org.openapijsonschematools.client.exceptions; +@SuppressWarnings("serial") public class InvalidAdditionalPropertyException extends BaseException { public InvalidAdditionalPropertyException(String s) { super(); diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/exceptions/InvalidTypeException.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/exceptions/InvalidTypeException.java index 01d1c5f38e4..73fafa999af 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/exceptions/InvalidTypeException.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/exceptions/InvalidTypeException.java @@ -1,5 +1,6 @@ package org.openapijsonschematools.client.exceptions; +@SuppressWarnings("serial") public class InvalidTypeException extends BaseException { public InvalidTypeException(String s) { super(); diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/exceptions/UnsetPropertyException.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/exceptions/UnsetPropertyException.java index 81d77b763f4..cb743b6a97c 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/exceptions/UnsetPropertyException.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/exceptions/UnsetPropertyException.java @@ -1,5 +1,6 @@ package org.openapijsonschematools.client.exceptions; +@SuppressWarnings("serial") public class UnsetPropertyException extends BaseException { public UnsetPropertyException(String s) { super(); diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/exceptions/ValidationException.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/exceptions/ValidationException.java index c88035bf089..8c217a9f9dc 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/exceptions/ValidationException.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/exceptions/ValidationException.java @@ -1,5 +1,6 @@ package org.openapijsonschematools.client.exceptions; +@SuppressWarnings("serial") public class ValidationException extends BaseException { public ValidationException(String s) { super(); diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/AnyTypeJsonSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/AnyTypeJsonSchema.java index 994aa4d4998..d9f6c0995d2 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/AnyTypeJsonSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/AnyTypeJsonSchema.java @@ -16,6 +16,7 @@ import org.openapijsonschematools.client.schemas.validation.ListSchemaValidator; import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.time.LocalDate; import java.time.ZonedDateTime; @@ -29,8 +30,8 @@ import java.util.Objects; import java.util.UUID; -public class AnyTypeJsonSchema extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static AnyTypeJsonSchema instance; +public class AnyTypeJsonSchema extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + private static @Nullable AnyTypeJsonSchema instance = null; protected AnyTypeJsonSchema() { super(new JsonSchemaInfo()); @@ -124,14 +125,18 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); items.add(castItem); i += 1; } @@ -139,7 +144,7 @@ public FrozenList getNewInstance(List arg, List pathToItem, P } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -152,22 +157,30 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); properties.put(propertyName, castValue); } return new FrozenMap<>(properties); } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -180,7 +193,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -195,6 +208,6 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/BooleanJsonSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/BooleanJsonSchema.java index e550fed4598..b8a7f2dfc75 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/BooleanJsonSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/BooleanJsonSchema.java @@ -11,6 +11,7 @@ import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; import org.openapijsonschematools.client.schemas.validation.TypeValidator; import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.LinkedHashMap; @@ -21,7 +22,7 @@ import java.util.Set; public class BooleanJsonSchema extends JsonSchema implements BooleanSchemaValidator { - private static BooleanJsonSchema instance; + private static @Nullable BooleanJsonSchema instance = null; protected BooleanJsonSchema() { super(new JsonSchemaInfo() @@ -53,11 +54,11 @@ public boolean validate(boolean arg, SchemaConfiguration configuration) throws V } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Boolean) { boolean boolArg = (Boolean) arg; return getNewInstance(boolArg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/DateJsonSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/DateJsonSchema.java index 56882f6403b..c32c7ed9a66 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/DateJsonSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/DateJsonSchema.java @@ -5,13 +5,11 @@ import org.openapijsonschematools.client.exceptions.InvalidTypeException; import org.openapijsonschematools.client.schemas.validation.JsonSchema; import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; -import org.openapijsonschematools.client.schemas.validation.KeywordEntry; import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; import org.openapijsonschematools.client.schemas.validation.StringSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.TypeValidator; -import org.openapijsonschematools.client.schemas.validation.FormatValidator; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.time.LocalDate; import java.util.HashSet; @@ -23,7 +21,7 @@ import java.util.Set; public class DateJsonSchema extends JsonSchema implements StringSchemaValidator { - private static DateJsonSchema instance; + private static @Nullable DateJsonSchema instance = null; protected DateJsonSchema() { super(new JsonSchemaInfo() @@ -60,10 +58,10 @@ public String validate(LocalDate arg, SchemaConfiguration configuration) throws } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof String) { return getNewInstance((String) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/DateTimeJsonSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/DateTimeJsonSchema.java index 8a2dcd8ba4b..40678438e46 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/DateTimeJsonSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/DateTimeJsonSchema.java @@ -12,6 +12,7 @@ import org.openapijsonschematools.client.schemas.validation.FormatValidator; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.time.ZonedDateTime; import java.util.HashSet; @@ -23,7 +24,7 @@ import java.util.Set; public class DateTimeJsonSchema extends JsonSchema implements StringSchemaValidator { - private static DateTimeJsonSchema instance; + private static @Nullable DateTimeJsonSchema instance = null; protected DateTimeJsonSchema() { super(new JsonSchemaInfo() @@ -60,10 +61,10 @@ public String validate(ZonedDateTime arg, SchemaConfiguration configuration) thr } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof String) { return getNewInstance((String) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/DecimalJsonSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/DecimalJsonSchema.java index a67ef350218..c054b61ba89 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/DecimalJsonSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/DecimalJsonSchema.java @@ -12,6 +12,7 @@ import org.openapijsonschematools.client.schemas.validation.TypeValidator; import org.openapijsonschematools.client.schemas.validation.FormatValidator; import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.LinkedHashMap; @@ -22,7 +23,7 @@ import java.util.Set; public class DecimalJsonSchema extends JsonSchema implements StringSchemaValidator { - private static DecimalJsonSchema instance; + private static @Nullable DecimalJsonSchema instance = null; protected DecimalJsonSchema() { super(new JsonSchemaInfo() @@ -55,10 +56,10 @@ public String validate(String arg, SchemaConfiguration configuration) throws Val } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof String) { return getNewInstance((String) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/DoubleJsonSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/DoubleJsonSchema.java index 00afd29ae4c..5e05b11ca82 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/DoubleJsonSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/DoubleJsonSchema.java @@ -12,6 +12,7 @@ import org.openapijsonschematools.client.schemas.validation.TypeValidator; import org.openapijsonschematools.client.schemas.validation.FormatValidator; import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.LinkedHashMap; @@ -22,7 +23,7 @@ import java.util.Set; public class DoubleJsonSchema extends JsonSchema implements NumberSchemaValidator { - private static DoubleJsonSchema instance; + private static @Nullable DoubleJsonSchema instance = null; protected DoubleJsonSchema() { super(new JsonSchemaInfo() @@ -59,10 +60,10 @@ public double validate(double arg, SchemaConfiguration configuration) { } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Number) { return getNewInstance((Number) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/FloatJsonSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/FloatJsonSchema.java index eb7da1dd8de..8892622fe0e 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/FloatJsonSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/FloatJsonSchema.java @@ -12,6 +12,7 @@ import org.openapijsonschematools.client.schemas.validation.TypeValidator; import org.openapijsonschematools.client.schemas.validation.FormatValidator; import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.LinkedHashMap; @@ -22,7 +23,7 @@ import java.util.Set; public class FloatJsonSchema extends JsonSchema implements NumberSchemaValidator { - private static FloatJsonSchema instance; + private static @Nullable FloatJsonSchema instance = null; protected FloatJsonSchema() { super(new JsonSchemaInfo() @@ -59,10 +60,10 @@ public float validate(float arg, SchemaConfiguration configuration) { } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Number) { return getNewInstance((Number) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/Int32JsonSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/Int32JsonSchema.java index f5a2f1872d7..af996f5b34c 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/Int32JsonSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/Int32JsonSchema.java @@ -12,6 +12,7 @@ import org.openapijsonschematools.client.schemas.validation.TypeValidator; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.LinkedHashMap; @@ -22,7 +23,7 @@ import java.util.Map; public class Int32JsonSchema extends JsonSchema implements NumberSchemaValidator { - private static Int32JsonSchema instance; + private static @Nullable Int32JsonSchema instance = null; protected Int32JsonSchema() { super(new JsonSchemaInfo() @@ -66,10 +67,10 @@ public float validate(float arg, SchemaConfiguration configuration) throws Valid } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Number) { return getNewInstance((Number) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/Int64JsonSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/Int64JsonSchema.java index ad2cfee3c81..891b0d078ed 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/Int64JsonSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/Int64JsonSchema.java @@ -12,6 +12,7 @@ import org.openapijsonschematools.client.schemas.validation.TypeValidator; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.LinkedHashMap; @@ -22,7 +23,7 @@ import java.util.Map; public class Int64JsonSchema extends JsonSchema implements NumberSchemaValidator { - private static Int64JsonSchema instance; + private static @Nullable Int64JsonSchema instance = null; protected Int64JsonSchema() { super(new JsonSchemaInfo() @@ -76,10 +77,10 @@ public double validate(double arg, SchemaConfiguration configuration) throws Val } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Number) { return getNewInstance((Number) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/IntJsonSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/IntJsonSchema.java index 70e26275d1c..26b7195ee4c 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/IntJsonSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/IntJsonSchema.java @@ -12,6 +12,7 @@ import org.openapijsonschematools.client.schemas.validation.TypeValidator; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.LinkedHashMap; @@ -22,7 +23,7 @@ import java.util.Map; public class IntJsonSchema extends JsonSchema implements NumberSchemaValidator { - private static IntJsonSchema instance; + private static @Nullable IntJsonSchema instance = null; protected IntJsonSchema() { super(new JsonSchemaInfo() @@ -76,10 +77,10 @@ public double validate(double arg, SchemaConfiguration configuration) throws Val } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Number) { return getNewInstance((Number) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/ListJsonSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/ListJsonSchema.java index 7d171a1937d..b771a88b7b1 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/ListJsonSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/ListJsonSchema.java @@ -10,16 +10,18 @@ import org.openapijsonschematools.client.schemas.validation.ListSchemaValidator; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.ArrayList; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Objects; import java.util.Set; -public class ListJsonSchema extends JsonSchema implements ListSchemaValidator> { - private static ListJsonSchema instance; +public class ListJsonSchema extends JsonSchema implements ListSchemaValidator> { + private static @Nullable ListJsonSchema instance = null; protected ListJsonSchema() { super(new JsonSchemaInfo() @@ -35,14 +37,18 @@ public static ListJsonSchema getInstance() { } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); items.add(castItem); i += 1; } @@ -50,7 +56,7 @@ public FrozenList getNewInstance(List arg, List pathToItem, P } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -63,10 +69,10 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof List) { return getNewInstance((List) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/MapJsonSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/MapJsonSchema.java index 4c3aa27719a..5202b5aa070 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/MapJsonSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/MapJsonSchema.java @@ -10,6 +10,7 @@ import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.ArrayList; import java.util.HashSet; @@ -20,8 +21,8 @@ import java.util.Objects; import java.util.Set; -public class MapJsonSchema extends JsonSchema implements MapSchemaValidator> { - private static MapJsonSchema instance; +public class MapJsonSchema extends JsonSchema implements MapSchemaValidator> { + private static @Nullable MapJsonSchema instance = null; protected MapJsonSchema() { super(new JsonSchemaInfo() @@ -37,22 +38,30 @@ public static MapJsonSchema getInstance() { } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); properties.put(propertyName, castValue); } return new FrozenMap<>(properties); } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -63,10 +72,11 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof FrozenMap) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } + diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/NotAnyTypeJsonSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/NotAnyTypeJsonSchema.java index 81217f8c4cb..7e91749e7ad 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/NotAnyTypeJsonSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/NotAnyTypeJsonSchema.java @@ -16,6 +16,7 @@ import org.openapijsonschematools.client.schemas.validation.ListSchemaValidator; import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.time.LocalDate; import java.time.ZonedDateTime; @@ -29,8 +30,8 @@ import java.util.Objects; import java.util.UUID; -public class NotAnyTypeJsonSchema extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static NotAnyTypeJsonSchema instance; +public class NotAnyTypeJsonSchema extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + private static @Nullable NotAnyTypeJsonSchema instance = null; protected NotAnyTypeJsonSchema() { super(new JsonSchemaInfo() @@ -126,14 +127,18 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); items.add(castItem); i += 1; } @@ -141,7 +146,7 @@ public FrozenList getNewInstance(List arg, List pathToItem, P } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -154,22 +159,30 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); properties.put(propertyName, castValue); } return new FrozenMap<>(properties); } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -182,7 +195,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -197,6 +210,6 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/NullJsonSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/NullJsonSchema.java index 2d6938a4b22..3611bc9e230 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/NullJsonSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/NullJsonSchema.java @@ -11,6 +11,7 @@ import org.openapijsonschematools.client.schemas.validation.NullSchemaValidator; import org.openapijsonschematools.client.schemas.validation.TypeValidator; import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.LinkedHashMap; @@ -21,7 +22,7 @@ import java.util.Set; public class NullJsonSchema extends JsonSchema implements NullSchemaValidator { - private static NullJsonSchema instance; + private static @Nullable NullJsonSchema instance = null; protected NullJsonSchema() { super(new JsonSchemaInfo() @@ -53,11 +54,11 @@ public Void validate(Void arg, SchemaConfiguration configuration) throws Validat } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/NumberJsonSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/NumberJsonSchema.java index 87a15430a93..734c9ea6148 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/NumberJsonSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/NumberJsonSchema.java @@ -11,6 +11,7 @@ import org.openapijsonschematools.client.schemas.validation.TypeValidator; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.LinkedHashMap; @@ -21,7 +22,7 @@ import java.util.Map; public class NumberJsonSchema extends JsonSchema implements NumberSchemaValidator { - private static NumberJsonSchema instance; + private static @Nullable NumberJsonSchema instance = null; protected NumberJsonSchema() { super(new JsonSchemaInfo() @@ -74,10 +75,10 @@ public double validate(double arg, SchemaConfiguration configuration) { } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Number) { return getNewInstance((Number) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/StringJsonSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/StringJsonSchema.java index 7c46f5ba114..e8ae4b8027f 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/StringJsonSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/StringJsonSchema.java @@ -11,6 +11,7 @@ import org.openapijsonschematools.client.schemas.validation.TypeValidator; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.time.LocalDate; import java.time.ZonedDateTime; @@ -24,7 +25,7 @@ import java.util.UUID; public class StringJsonSchema extends JsonSchema implements StringSchemaValidator { - private static StringJsonSchema instance; + private static @Nullable StringJsonSchema instance = null; protected StringJsonSchema() { super(new JsonSchemaInfo() @@ -68,10 +69,10 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof String) { return getNewInstance((String) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/UuidJsonSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/UuidJsonSchema.java index 48c8acacb80..d7f55dc445f 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/UuidJsonSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/UuidJsonSchema.java @@ -12,6 +12,7 @@ import org.openapijsonschematools.client.schemas.validation.FormatValidator; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.LinkedHashMap; @@ -23,7 +24,7 @@ import java.util.UUID; public class UuidJsonSchema extends JsonSchema implements StringSchemaValidator { - private static UuidJsonSchema instance; + private static @Nullable UuidJsonSchema instance = null; protected UuidJsonSchema() { super(new JsonSchemaInfo() @@ -60,10 +61,10 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof String) { return getNewInstance((String) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/AdditionalPropertiesValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/AdditionalPropertiesValidator.java index 5eaea40b69e..5a5a6cfc917 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/AdditionalPropertiesValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/AdditionalPropertiesValidator.java @@ -1,7 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.ArrayList; -import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -15,24 +15,23 @@ public AdditionalPropertiesValidator(Class additionalPrope } @Override - public Object getConstraint() { - return additionalProperties; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { - if (!(arg instanceof Map)) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { + if (!(arg instanceof Map mapArg)) { return null; } - Map castArg = (Map) arg; - Set presentAdditionalProperties = new LinkedHashSet<>(castArg.keySet()); + Set presentAdditionalProperties = new LinkedHashSet<>(); + for (Object key: mapArg.keySet()) { + if (key instanceof String) { + presentAdditionalProperties.add((String) key); + } + } if (schema.properties != null) { presentAdditionalProperties.removeAll(schema.properties.keySet()); } PathToSchemasMap pathToSchemas = new PathToSchemasMap(); // todo add handling for validatedPatternProperties for(String addPropName: presentAdditionalProperties) { - Object propValue = castArg.get(addPropName); + @Nullable Object propValue = mapArg.get(addPropName); List propPathToItem = new ArrayList<>(validationMetadata.pathToItem()); propPathToItem.add(addPropName); ValidationMetadata propValidationMetadata = new ValidationMetadata( diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/AllOfValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/AllOfValidator.java index 067021deaf6..7b392d027a7 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/AllOfValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/AllOfValidator.java @@ -1,4 +1,5 @@ package org.openapijsonschematools.client.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.List; @@ -10,12 +11,7 @@ public AllOfValidator(List> allOf) { } @Override - public Object getConstraint() { - return allOf; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { PathToSchemasMap pathToSchemas = new PathToSchemasMap(); for(Class allOfClass: allOf) { JsonSchema allOfSchema = JsonSchemaFactory.getInstance(allOfClass); diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/AnyOfValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/AnyOfValidator.java index c3a911631a1..53d61e878b2 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/AnyOfValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/AnyOfValidator.java @@ -1,6 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.ArrayList; import java.util.List; @@ -13,12 +14,7 @@ public AnyOfValidator(List> anyOf) { } @Override - public Object getConstraint() { - return anyOf; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { PathToSchemasMap pathToSchemas = new PathToSchemasMap(); List> validatedAnyOfClasses = new ArrayList<>(); for(Class anyOfClass: anyOf) { diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/EnumValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/EnumValidator.java index c614dbac13f..18bf8d346e8 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/EnumValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/EnumValidator.java @@ -1,28 +1,29 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.math.BigDecimal; import java.util.Set; public class EnumValidator implements KeywordValidator { - public final Set enumValues; + public final Set<@Nullable Object> enumValues; - public EnumValidator(Set enumValues) { + public EnumValidator(Set<@Nullable Object> enumValues) { this.enumValues = enumValues; } - @Override - public Object getConstraint() { - return enumValues; + @SuppressWarnings("nullness") + private boolean enumContainsArg(@Nullable Object arg){ + return enumValues.contains(arg); } @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (enumValues.isEmpty()) { throw new ValidationException("No value can match enum because enum is empty"); } - if (enumValues.contains(arg)) { + if (enumContainsArg(arg)) { return null; } if (arg instanceof Float) { diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/ExclusiveMaximumValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/ExclusiveMaximumValidator.java index 92036b3bcea..6f07d0c46e7 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/ExclusiveMaximumValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/ExclusiveMaximumValidator.java @@ -1,6 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; public class ExclusiveMaximumValidator implements KeywordValidator { @@ -11,12 +12,7 @@ public ExclusiveMaximumValidator(Number exclusiveMaximum) { } @Override - public Object getConstraint() { - return exclusiveMaximum; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof Number)) { return null; } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/ExclusiveMinimumValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/ExclusiveMinimumValidator.java index e8b46f9dbe9..0bb892d6e54 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/ExclusiveMinimumValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/ExclusiveMinimumValidator.java @@ -1,6 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; public class ExclusiveMinimumValidator implements KeywordValidator { @@ -11,12 +12,7 @@ public ExclusiveMinimumValidator(Number exclusiveMinimum) { } @Override - public Object getConstraint() { - return exclusiveMinimum; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof Number)) { return null; } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/FakeValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/FakeValidator.java index c8c0ad1d93d..db5ec573081 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/FakeValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/FakeValidator.java @@ -1,13 +1,10 @@ package org.openapijsonschematools.client.schemas.validation; -public class FakeValidator implements KeywordValidator { - @Override - public Object getConstraint() { - return null; - } +import org.checkerframework.checker.nullness.qual.Nullable; +public class FakeValidator implements KeywordValidator { @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { return null; } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/FormatValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/FormatValidator.java index 9e6d941ebef..2f165da7db2 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/FormatValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/FormatValidator.java @@ -1,6 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.math.BigDecimal; import java.math.BigInteger; @@ -14,11 +15,6 @@ public FormatValidator(String format) { this.format = format; } - @Override - public Object getConstraint() { - return format; - } - private final static BigInteger int32InclusiveMinimum = BigInteger.valueOf(-2147483648L); private final static BigInteger int32InclusiveMaximum = BigInteger.valueOf(2147483647L); private final static BigInteger int64InclusiveMinimum = BigInteger.valueOf(-9223372036854775808L); @@ -147,7 +143,7 @@ private Void validateStringFormat(String arg, ValidationMetadata validationMetad } @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (arg instanceof Number) { validateNumericFormat( (Number) arg, diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/FrozenMap.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/FrozenMap.java index 4851ae62d7a..a24276efe3c 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/FrozenMap.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/FrozenMap.java @@ -1,5 +1,6 @@ package org.openapijsonschematools.client.schemas.validation; +import org.checkerframework.checker.nullness.qual.KeyFor; import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; @@ -7,6 +8,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; public class FrozenMap extends AbstractMap { /* @@ -27,15 +29,17 @@ protected void throwIfKeyNotPresent(String key) throws UnsetPropertyException { } } - protected void throwIfKeyKnown(String key, Set requiredKeys, Set optionalKeys) throws IllegalArgumentException { + protected void throwIfKeyKnown(String key, Set requiredKeys, Set optionalKeys) throws InvalidAdditionalPropertyException { if (requiredKeys.contains(key) || optionalKeys.contains(key)) { throw new InvalidAdditionalPropertyException ("The known key " + key + " may not be passed in when getting an additional property"); } } @Override - public Set> entrySet() { - return map.entrySet(); + public Set> entrySet() { + return map.entrySet().stream() + .map(x -> new AbstractMap.SimpleEntry<>(x.getKey(), x.getValue())) + .collect(Collectors.toSet()); } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/ItemsValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/ItemsValidator.java index f20c26efdf5..5d3bea9fc56 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/ItemsValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/ItemsValidator.java @@ -1,5 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; + import java.util.ArrayList; import java.util.List; @@ -11,20 +13,14 @@ public ItemsValidator(Class items) { } @Override - public Object getConstraint() { - return items; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof List)) { return null; } - List castArg = (List) arg; PathToSchemasMap pathToSchemas = new PathToSchemasMap(); // todo add handling for prefixItems int i = 0; - for(Object itemValue: castArg) { + for(Object itemValue: (List) arg) { List itemPathToItem = new ArrayList<>(validationMetadata.pathToItem()); itemPathToItem.add(i); ValidationMetadata itemValidationMetadata = new ValidationMetadata( diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchema.java index 87ee535a81b..7b2dd027212 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchema.java @@ -2,6 +2,7 @@ import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.math.BigDecimal; import java.time.LocalDate; @@ -16,30 +17,30 @@ import java.util.regex.Pattern; public abstract class JsonSchema { - public final Set> type; - public final String format; - public final Class items; - public final Map> properties; - public final Set required; - public final Number exclusiveMaximum; - public final Number exclusiveMinimum; - public final Integer maxItems; - public final Integer minItems; - public final Integer maxLength; - public final Integer minLength; - public final Integer maxProperties; - public final Integer minProperties; - public final Number maximum; - public final Number minimum; - public final BigDecimal multipleOf; - public final Class additionalProperties; - public final List> allOf; - public final List> anyOf; - public final List> oneOf; - public final Class not; - public final Boolean uniqueItems; - public final Set enumValues; - public final Pattern pattern; + public final @Nullable Set> type; + public final @Nullable String format; + public final @Nullable Class items; + public final @Nullable Map> properties; + public final @Nullable Set required; + public final @Nullable Number exclusiveMaximum; + public final @Nullable Number exclusiveMinimum; + public final @Nullable Integer maxItems; + public final @Nullable Integer minItems; + public final @Nullable Integer maxLength; + public final @Nullable Integer minLength; + public final @Nullable Integer maxProperties; + public final @Nullable Integer minProperties; + public final @Nullable Number maximum; + public final @Nullable Number minimum; + public final @Nullable BigDecimal multipleOf; + public final @Nullable Class additionalProperties; + public final @Nullable List> allOf; + public final @Nullable List> anyOf; + public final @Nullable List> oneOf; + public final @Nullable Class not; + public final @Nullable Boolean uniqueItems; + public final @Nullable Set<@Nullable Object> enumValues; + public final @Nullable Pattern pattern; private final LinkedHashMap keywordToValidator; protected JsonSchema(JsonSchemaInfo jsonSchemaInfo) { @@ -215,40 +216,40 @@ protected JsonSchema(JsonSchemaInfo jsonSchemaInfo) { this.keywordToValidator = keywordToValidator; } - public abstract Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas); + public abstract @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas); public static PathToSchemasMap validate( JsonSchema jsonSchema, - Object arg, + @Nullable Object arg, ValidationMetadata validationMetadata ) throws ValidationException { LinkedHashSet disabledKeywords = validationMetadata.configuration().disabledKeywordFlags().getKeywords(); PathToSchemasMap pathToSchemas = new PathToSchemasMap(); LinkedHashMap thisKeywordToValidator = jsonSchema.keywordToValidator; - if (thisKeywordToValidator != null) { - for (Map.Entry entry: thisKeywordToValidator.entrySet()) { - String jsonKeyword = entry.getKey(); - if (disabledKeywords.contains(jsonKeyword)) { - continue; - } - KeywordValidator validator = entry.getValue(); - PathToSchemasMap otherPathToSchemas = validator.validate( - jsonSchema, - arg, - validationMetadata - ); - if (otherPathToSchemas == null) { - continue; - } - pathToSchemas.update(otherPathToSchemas); + for (Map.Entry entry: thisKeywordToValidator.entrySet()) { + String jsonKeyword = entry.getKey(); + if (disabledKeywords.contains(jsonKeyword)) { + continue; } + KeywordValidator validator = entry.getValue(); + @Nullable PathToSchemasMap otherPathToSchemas = validator.validate( + jsonSchema, + arg, + validationMetadata + ); + if (otherPathToSchemas == null) { + continue; + } + pathToSchemas.update(otherPathToSchemas); } List pathToItem = validationMetadata.pathToItem(); if (!pathToSchemas.containsKey(pathToItem)) { pathToSchemas.put(validationMetadata.pathToItem(), new LinkedHashMap<>()); } - pathToSchemas.get(pathToItem).put(jsonSchema, null); - + @Nullable LinkedHashMap schemas = pathToSchemas.get(pathToItem); + if (schemas != null) { + schemas.put(jsonSchema, null); + } return pathToSchemas; } @@ -274,7 +275,7 @@ protected Void castToAllowedTypes(Void arg, List pathToItem, Set castToAllowedTypes(List arg, List pathToItem, Set> pathSet) { pathSet.add(pathToItem); - List argFixed = new ArrayList<>(); + List<@Nullable Object> argFixed = new ArrayList<>(); int i =0; for (Object item: arg) { List newPathToItem = new ArrayList<>(pathToItem); @@ -288,10 +289,14 @@ protected List castToAllowedTypes(List arg, List pathToItem, Set castToAllowedTypes(Map arg, List pathToItem, Set> pathSet) { pathSet.add(pathToItem); - LinkedHashMap argFixed = new LinkedHashMap<>(); + LinkedHashMap argFixed = new LinkedHashMap<>(); for (Map.Entry entry: arg.entrySet()) { - String key = (String) entry.getKey(); - Object val = arg.get(entry.getKey()); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String key = (String) entryKey; + Object val = entry.getValue(); List newPathToItem = new ArrayList<>(pathToItem); newPathToItem.add(key); Object fixedVal = castToAllowedObjectTypes(val, newPathToItem, pathSet); @@ -300,7 +305,7 @@ protected List castToAllowedTypes(List arg, List pathToItem, Set pathToItem, Set> pathSet) { + private @Nullable Object castToAllowedObjectTypes(@Nullable Object arg, List pathToItem, Set> pathSet) { if (arg == null) { return castToAllowedTypes((Void) null, pathToItem, pathSet); } else if (arg instanceof String) { @@ -348,7 +353,7 @@ public String getNewInstance(String arg, List pathToItem, PathToSchemasM return arg; } - protected static PathToSchemasMap getPathToSchemas(JsonSchema jsonSchema, Object arg, ValidationMetadata validationMetadata, Set> pathSet) { + protected static PathToSchemasMap getPathToSchemas(JsonSchema jsonSchema, @Nullable Object arg, ValidationMetadata validationMetadata, Set> pathSet) { PathToSchemasMap pathToSchemasMap = new PathToSchemasMap(); // todo add check of validationMetadata.validationRanEarlier(this) PathToSchemasMap otherPathToSchemas = validate(jsonSchema, arg, validationMetadata); @@ -369,35 +374,12 @@ protected static PathToSchemasMap getPathToSchemas(JsonSchema jsonSchema, Objec return pathToSchemasMap; } - /* - protected FrozenMap getProperties(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); - for(Map.Entry entry: arg.entrySet()) { - String propertyName = entry.getKey(); - List propertyPathToItem = new ArrayList<>(pathToItem); - propertyPathToItem.add(propertyName); - MapValueCastType value = entry.getValue(); - JsonSchema propertySchema = (JsonSchema) pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - MapValueOutType castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); - } - return new FrozenMap<>(properties); - } - - private FrozenList getItems(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - ArrayList items = new ArrayList<>(); - int i = 0; - for (ListItemCastType item: arg) { - List itemPathToItem = new ArrayList<>(pathToItem); - itemPathToItem.add(i); - JsonSchema itemSchema = (JsonSchema) pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - ListItemOutType castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); - i += 1; - } - return new FrozenList<>(items); - } - */ - - // todo add bytes and FileIO + public static String getClass(@Nullable Object arg) { + if (arg == null) { + return Void.class.getSimpleName(); + } else { + return arg.getClass().getSimpleName(); + } + } + // todo add bytes and FileIO } \ No newline at end of file diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaFactory.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaFactory.java index b3eaf9228eb..7c9b6402f2b 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaFactory.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaFactory.java @@ -1,5 +1,8 @@ package org.openapijsonschematools.client.schemas.validation; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; @@ -10,12 +13,16 @@ public class JsonSchemaFactory { static Map, JsonSchema> classToInstance = new HashMap<>(); public static V getInstance(Class schemaCls) { - if (classToInstance.containsKey(schemaCls)) { - return (V) classToInstance.get(schemaCls); + @Nullable JsonSchema cacheInst = classToInstance.get(schemaCls); + if (cacheInst != null) { + assert schemaCls.isInstance(cacheInst); + return schemaCls.cast(cacheInst); } try { Method method = schemaCls.getMethod("getInstance"); - V inst = (V) method.invoke(null); + @SuppressWarnings("nullness") @NonNull Object obj = method.invoke(null); + assert schemaCls.isInstance(obj); + V inst = schemaCls.cast(obj); classToInstance.put(schemaCls, inst); return inst; } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaInfo.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaInfo.java index 0d069ea2f99..3685df5d0bb 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaInfo.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaInfo.java @@ -1,4 +1,5 @@ package org.openapijsonschematools.client.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import java.math.BigDecimal; import java.util.List; @@ -7,122 +8,122 @@ import java.util.regex.Pattern; public class JsonSchemaInfo { - public Set> type; + public @Nullable Set> type = null; public JsonSchemaInfo type(Set> type) { this.type = type; return this; } - public String format; + public @Nullable String format = null; public JsonSchemaInfo format(String format) { this.format = format; return this; } - public Class items; + public @Nullable Class items = null; public JsonSchemaInfo items(Class items) { this.items = items; return this; } - public Map> properties; + public @Nullable Map> properties = null; public JsonSchemaInfo properties(Map> properties) { this.properties = properties; return this; } - public Set required; + public @Nullable Set required = null; public JsonSchemaInfo required(Set required) { this.required = required; return this; } - public Number exclusiveMaximum; + public @Nullable Number exclusiveMaximum = null; public JsonSchemaInfo exclusiveMaximum(Number exclusiveMaximum) { this.exclusiveMaximum = exclusiveMaximum; return this; } - public Number exclusiveMinimum; + public @Nullable Number exclusiveMinimum = null; public JsonSchemaInfo exclusiveMinimum(Number exclusiveMinimum) { this.exclusiveMinimum = exclusiveMinimum; return this; } - public Integer maxItems; + public @Nullable Integer maxItems = null; public JsonSchemaInfo maxItems(Integer maxItems) { this.maxItems = maxItems; return this; } - public Integer minItems; + public @Nullable Integer minItems = null; public JsonSchemaInfo minItems(Integer minItems) { this.minItems = minItems; return this; } - public Integer maxLength; + public @Nullable Integer maxLength = null; public JsonSchemaInfo maxLength(Integer maxLength) { this.maxLength = maxLength; return this; } - public Integer minLength; + public @Nullable Integer minLength = null; public JsonSchemaInfo minLength(Integer minLength) { this.minLength = minLength; return this; } - public Integer maxProperties; + public @Nullable Integer maxProperties = null; public JsonSchemaInfo maxProperties(Integer maxProperties) { this.maxProperties = maxProperties; return this; } - public Integer minProperties; + public @Nullable Integer minProperties = null; public JsonSchemaInfo minProperties(Integer minProperties) { this.minProperties = minProperties; return this; } - public Number maximum; + public @Nullable Number maximum = null; public JsonSchemaInfo maximum(Number maximum) { this.maximum = maximum; return this; } - public Number minimum; + public @Nullable Number minimum = null; public JsonSchemaInfo minimum(Number minimum) { this.minimum = minimum; return this; } - public BigDecimal multipleOf; + public @Nullable BigDecimal multipleOf = null; public JsonSchemaInfo multipleOf(BigDecimal multipleOf) { this.multipleOf = multipleOf; return this; } - public Class additionalProperties; + public @Nullable Class additionalProperties; public JsonSchemaInfo additionalProperties(Class additionalProperties) { this.additionalProperties = additionalProperties; return this; } - public List> allOf; + public @Nullable List> allOf = null; public JsonSchemaInfo allOf(List> allOf) { this.allOf = allOf; return this; } - public List> anyOf; + public @Nullable List> anyOf = null; public JsonSchemaInfo anyOf(List> anyOf) { this.anyOf = anyOf; return this; } - public List> oneOf; + public @Nullable List> oneOf = null; public JsonSchemaInfo oneOf(List> oneOf) { this.oneOf = oneOf; return this; } - public Class not; + public @Nullable Class not = null; public JsonSchemaInfo not(Class not) { this.not = not; return this; } - public Boolean uniqueItems; + public @Nullable Boolean uniqueItems = null; public JsonSchemaInfo uniqueItems(Boolean uniqueItems) { this.uniqueItems = uniqueItems; return this; } - public Set enumValues; - public JsonSchemaInfo enumValues(Set enumValues) { + public @Nullable Set<@Nullable Object> enumValues = null; + public JsonSchemaInfo enumValues(Set<@Nullable Object> enumValues) { this.enumValues = enumValues; return this; } - public Pattern pattern; + public @Nullable Pattern pattern = null; public JsonSchemaInfo pattern(Pattern pattern) { this.pattern = pattern; return this; diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/KeywordEntry.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/KeywordEntry.java index 647b930ba4c..c00f0c4804e 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/KeywordEntry.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/KeywordEntry.java @@ -2,6 +2,7 @@ import java.util.AbstractMap; +@SuppressWarnings("serial") public class KeywordEntry extends AbstractMap.SimpleEntry { public KeywordEntry(String key, KeywordValidator value) { super(key, value); diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/KeywordValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/KeywordValidator.java index 265e1ec3467..42326a9beb3 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/KeywordValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/KeywordValidator.java @@ -1,12 +1,11 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; public interface KeywordValidator { - PathToSchemasMap validate( + @Nullable PathToSchemasMap validate( JsonSchema schema, - Object arg, + @Nullable Object arg, ValidationMetadata validationMetadata) throws ValidationException; - - Object getConstraint(); } \ No newline at end of file diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MaxItemsValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MaxItemsValidator.java index 1391325b064..bc0e7a66932 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MaxItemsValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MaxItemsValidator.java @@ -1,6 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.List; @@ -12,12 +13,7 @@ public MaxItemsValidator(int maxItems) { } @Override - public Object getConstraint() { - return maxItems; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof List)) { return null; } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MaxLengthValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MaxLengthValidator.java index c6db775615f..98986589f7b 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MaxLengthValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MaxLengthValidator.java @@ -1,6 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; public class MaxLengthValidator extends LengthValidator implements KeywordValidator { public final int maxLength; @@ -10,12 +11,7 @@ public MaxLengthValidator(int maxLength) { } @Override - public Object getConstraint() { - return maxLength; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof String)) { return null; } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MaxPropertiesValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MaxPropertiesValidator.java index 8c9e11bb856..985cc631a12 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MaxPropertiesValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MaxPropertiesValidator.java @@ -1,6 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.Map; @@ -12,12 +13,7 @@ public MaxPropertiesValidator(int maxProperties) { } @Override - public Object getConstraint() { - return maxProperties; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof Map)) { return null; } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MaximumValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MaximumValidator.java index 73b5225e280..77568666978 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MaximumValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MaximumValidator.java @@ -1,6 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; public class MaximumValidator implements KeywordValidator { @@ -11,12 +12,7 @@ public MaximumValidator(Number maximum) { } @Override - public Object getConstraint() { - return maximum; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof Number)) { return null; } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MinItemsValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MinItemsValidator.java index c8356d8b952..a9748b11707 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MinItemsValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MinItemsValidator.java @@ -1,6 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.List; @@ -12,12 +13,7 @@ public MinItemsValidator(int minItems) { } @Override - public Object getConstraint() { - return minItems; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof List)) { return null; } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MinLengthValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MinLengthValidator.java index 62adaa68e69..2d23fc896c0 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MinLengthValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MinLengthValidator.java @@ -1,6 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; public class MinLengthValidator extends LengthValidator implements KeywordValidator { public final int minLength; @@ -10,12 +11,7 @@ public MinLengthValidator(int minLength) { } @Override - public Object getConstraint() { - return minLength; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof String)) { return null; } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MinPropertiesValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MinPropertiesValidator.java index 33dec6abe0d..8801e1872ef 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MinPropertiesValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MinPropertiesValidator.java @@ -1,6 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.Map; @@ -12,12 +13,7 @@ public MinPropertiesValidator(int minProperties) { } @Override - public Object getConstraint() { - return minProperties; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof Map)) { return null; } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MinimumValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MinimumValidator.java index 07017c768f0..f8e9b86e1d3 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MinimumValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MinimumValidator.java @@ -1,6 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; public class MinimumValidator implements KeywordValidator { @@ -11,12 +12,7 @@ public MinimumValidator(Number minimum) { } @Override - public Object getConstraint() { - return minimum; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof Number)) { return null; } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MultipleOfValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MultipleOfValidator.java index cde604e8581..ce3c8887c2d 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MultipleOfValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/MultipleOfValidator.java @@ -1,6 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.math.BigDecimal; @@ -11,11 +12,6 @@ public MultipleOfValidator(BigDecimal multipleOf) { this.multipleOf = multipleOf; } - @Override - public Object getConstraint() { - return multipleOf; - } - private BigDecimal getBigDecimal(Number arg) { if (arg instanceof Integer) { return new BigDecimal((Integer) arg); @@ -31,7 +27,7 @@ private BigDecimal getBigDecimal(Number arg) { } @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof Number)) { return null; } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/NotValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/NotValidator.java index 15b94614ecf..34410cf0de0 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/NotValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/NotValidator.java @@ -1,6 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -17,12 +18,7 @@ public NotValidator(Class not) { } @Override - public Object getConstraint() { - return not; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { PathToSchemasMap pathToSchemas; try { JsonSchema notSchema = JsonSchemaFactory.getInstance(not); diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/OneOfValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/OneOfValidator.java index 4ce38a4e753..5d33ed1fefb 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/OneOfValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/OneOfValidator.java @@ -1,6 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.ArrayList; import java.util.List; @@ -13,12 +14,7 @@ public OneOfValidator(List> oneOf) { } @Override - public Object getConstraint() { - return oneOf; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { PathToSchemasMap pathToSchemas = new PathToSchemasMap(); List> validatedOneOfClasses = new ArrayList<>(); for(Class oneOfClass: oneOf) { diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/PathToSchemasMap.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/PathToSchemasMap.java index 54a326630bc..6e199334d4c 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/PathToSchemasMap.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/PathToSchemasMap.java @@ -4,6 +4,7 @@ import java.util.List; import java.util.Map; +@SuppressWarnings("serial") public class PathToSchemasMap extends LinkedHashMap, LinkedHashMap> { public void update(PathToSchemasMap other) { diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/PatternValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/PatternValidator.java index c4c398147ee..a16b14f73d3 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/PatternValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/PatternValidator.java @@ -1,6 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.regex.Pattern; @@ -12,12 +13,7 @@ public PatternValidator(Pattern pattern) { } @Override - public Object getConstraint() { - return pattern; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof String)) { return null; } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/PropertiesValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/PropertiesValidator.java index dc652f79c9e..ebfe8487bc8 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/PropertiesValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/PropertiesValidator.java @@ -1,5 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; + import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; @@ -14,21 +16,23 @@ public PropertiesValidator(Map> properties) } @Override - public Object getConstraint() { - return properties; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { - if (!(arg instanceof Map)) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { + if (!(arg instanceof Map mapArg)) { return null; } PathToSchemasMap pathToSchemas = new PathToSchemasMap(); - Map castArg = (Map) arg; - Set presentProperties = new LinkedHashSet<>(castArg.keySet()); - presentProperties.retainAll(properties.keySet()); - for(String propName: presentProperties) { - Object propValue = castArg.get(propName); + Set presentProperties = new LinkedHashSet<>(); + for (Object key: mapArg.keySet()) { + if (key instanceof String) { + presentProperties.add((String) key); + } + } + for(Map.Entry> entry: properties.entrySet()) { + String propName = entry.getKey(); + if (!presentProperties.contains(propName)) { + continue; + } + @Nullable Object propValue = ((Map) arg).get(propName); List propPathToItem = new ArrayList<>(validationMetadata.pathToItem()); propPathToItem.add(propName); ValidationMetadata propValidationMetadata = new ValidationMetadata( @@ -37,7 +41,7 @@ public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetada validationMetadata.validatedPathToSchemas(), validationMetadata.seenClasses() ); - Class propClass = properties.get(propName); + Class propClass = entry.getValue(); JsonSchema propSchema = JsonSchemaFactory.getInstance(propClass); if (propValidationMetadata.validationRanEarlier(propSchema)) { // todo add_deeper_validated_schemas diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/PropertyEntry.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/PropertyEntry.java index 2d9d81a0196..aaaede643a0 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/PropertyEntry.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/PropertyEntry.java @@ -2,6 +2,7 @@ import java.util.AbstractMap; +@SuppressWarnings("serial") public class PropertyEntry extends AbstractMap.SimpleEntry> { public PropertyEntry(String key, Class value) { super(key, value); diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/RequiredValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/RequiredValidator.java index c73ae4e30fc..6f625049000 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/RequiredValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/RequiredValidator.java @@ -1,6 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.List; @@ -15,18 +16,16 @@ public RequiredValidator(Set required) { } @Override - public Object getConstraint() { - return required; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof Map)) { return null; } - Map castArg = (Map) arg; Set missingRequiredProperties = new HashSet<>(required); - missingRequiredProperties.removeAll(castArg.keySet()); + for (Object key: ((Map) arg).keySet()) { + if (key instanceof String) { + missingRequiredProperties.remove(key); + } + } if (!missingRequiredProperties.isEmpty()) { List missingReqProps = missingRequiredProperties.stream().sorted().toList(); String pluralChar = ""; diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/TypeValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/TypeValidator.java index 33d23b656fa..a3028cae2db 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/TypeValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/TypeValidator.java @@ -1,6 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.List; import java.util.Map; @@ -14,12 +15,7 @@ public TypeValidator(Set> type) { } @Override - public Object getConstraint() { - return type; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { Class argClass; if (arg == null) { argClass = Void.class; diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/UniqueItemsValidator.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/UniqueItemsValidator.java index 1034539e362..8b443cc9b29 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/UniqueItemsValidator.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/UniqueItemsValidator.java @@ -1,6 +1,7 @@ package org.openapijsonschematools.client.schemas.validation; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.List; @@ -14,20 +15,15 @@ public UniqueItemsValidator(boolean uniqueItems) { } @Override - public Object getConstraint() { - return uniqueItems; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof List)) { return null; } if (!uniqueItems) { return null; } - Set seenItems = new HashSet<>(); - for (Object item: (List) arg) { + Set<@Nullable Object> seenItems = new HashSet<>(); + for (@Nullable Object item: (List) arg) { int startingSeenItemsSize = seenItems.size(); seenItems.add(item); if (seenItems.size() == startingSeenItemsSize) { diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/UnsetAnyTypeJsonSchema.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/UnsetAnyTypeJsonSchema.java index 2622abe53e5..1e3e37546e7 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/UnsetAnyTypeJsonSchema.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/UnsetAnyTypeJsonSchema.java @@ -4,6 +4,7 @@ import org.openapijsonschematools.client.configurations.SchemaConfiguration; import org.openapijsonschematools.client.exceptions.InvalidTypeException; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.time.LocalDate; import java.time.ZonedDateTime; @@ -17,8 +18,8 @@ import java.util.Objects; import java.util.UUID; -public class UnsetAnyTypeJsonSchema extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static UnsetAnyTypeJsonSchema instance; +public class UnsetAnyTypeJsonSchema extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + private static @Nullable UnsetAnyTypeJsonSchema instance = null; protected UnsetAnyTypeJsonSchema() { super(new JsonSchemaInfo()); @@ -112,14 +113,18 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); items.add(castItem); i += 1; } @@ -127,7 +132,7 @@ public FrozenList getNewInstance(List arg, List pathToItem, P } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -140,22 +145,30 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); properties.put(propertyName, castValue); } return new FrozenMap<>(properties); } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -168,7 +181,7 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -183,6 +196,6 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/ValidationMetadata.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/ValidationMetadata.java index 3e8cd38b234..8d83f3b6207 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/ValidationMetadata.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/schemas/validation/ValidationMetadata.java @@ -1,5 +1,6 @@ package org.openapijsonschematools.client.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import org.openapijsonschematools.client.configurations.SchemaConfiguration; import java.util.List; import java.util.Map; @@ -13,7 +14,7 @@ public record ValidationMetadata( ) { public boolean validationRanEarlier(JsonSchema schema) { - Map validatedSchemas = validatedPathToSchemas.getOrDefault(pathToItem, null); + @Nullable Map validatedSchemas = validatedPathToSchemas.get(pathToItem); if (validatedSchemas != null && validatedSchemas.containsKey(schema)) { return true; } diff --git a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/AnyTypeSchemaTest.java b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/AnyTypeSchemaTest.java index 36db994c30d..381e9fb422f 100644 --- a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/AnyTypeSchemaTest.java +++ b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/AnyTypeSchemaTest.java @@ -1,10 +1,10 @@ package org.openapijsonschematools.client.schemas; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; -import org.openapijsonschematools.client.schemas.validation.JsonSchemaFactory; import org.openapijsonschematools.client.schemas.validation.FrozenList; import org.openapijsonschematools.client.schemas.validation.FrozenMap; @@ -19,10 +19,16 @@ public class AnyTypeSchemaTest { static final AnyTypeJsonSchema schema = AnyTypeJsonSchema.getInstance(); static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone()); + @SuppressWarnings("nullness") + private Void assertNull(@Nullable Object object) { + Assert.assertNull(object); + return null; + } + @Test public void testValidateNull() { Void validatedValue = schema.validate((Void) null, configuration); - Assert.assertNull(validatedValue); + assertNull(validatedValue); } @Test diff --git a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/ArrayTypeSchemaTest.java b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/ArrayTypeSchemaTest.java index 749c156a71d..d9776b84ecc 100644 --- a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/ArrayTypeSchemaTest.java +++ b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/ArrayTypeSchemaTest.java @@ -1,5 +1,6 @@ package org.openapijsonschematools.client.schemas; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; @@ -15,6 +16,7 @@ import java.util.ArrayList; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Objects; @@ -44,9 +46,16 @@ public FrozenList getNewInstance(List arg, List pathToItem, P for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - String castItem = (String) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(castItem instanceof String)) { + throw new InvalidTypeException("Instantiated type of item is invalid"); + } + items.add((String) castItem); i += 1; } return new FrozenList<>(items); @@ -64,11 +73,11 @@ public FrozenList validate(List arg, SchemaConfiguration configu } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof List) { return getNewInstance((List) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } @@ -93,14 +102,21 @@ public ArrayWithOutputClsSchema() { @Override public ArrayWithOutputClsSchemaList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - ArrayList items = new ArrayList<>(); + List items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - String castItem = (String) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(castItem instanceof String)) { + throw new InvalidTypeException("Instantiated type of item is invalid"); + } + items.add((String) castItem); i += 1; } FrozenList newInstanceItems = new FrozenList<>(items); @@ -119,11 +135,11 @@ public ArrayWithOutputClsSchemaList validate(List arg, SchemaConfigurati } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof List) { return getNewInstance((List) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } diff --git a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/ListSchemaTest.java b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/ListSchemaTest.java index bc9c978e2e0..8b50d60846a 100644 --- a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/ListSchemaTest.java +++ b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/ListSchemaTest.java @@ -1,11 +1,11 @@ package org.openapijsonschematools.client.schemas; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; import org.openapijsonschematools.client.schemas.validation.JsonSchema; -import org.openapijsonschematools.client.schemas.validation.JsonSchemaFactory; import org.openapijsonschematools.client.schemas.validation.FrozenList; import org.openapijsonschematools.client.exceptions.ValidationException; import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; @@ -38,7 +38,7 @@ public void testExceptionThrownForInvalidType() { public void testValidateList() { List inList = new ArrayList<>(); inList.add("today"); - FrozenList validatedValue = listJsonSchema.validate(inList, configuration); + FrozenList<@Nullable Object> validatedValue = listJsonSchema.validate(inList, configuration); ArrayList outList = new ArrayList<>(); outList.add("today"); Assert.assertEquals(validatedValue, outList); diff --git a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/MapSchemaTest.java b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/MapSchemaTest.java index e2601fa66e3..dfdea79a4be 100644 --- a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/MapSchemaTest.java +++ b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/MapSchemaTest.java @@ -1,5 +1,6 @@ package org.openapijsonschematools.client.schemas; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; @@ -39,7 +40,7 @@ public void testExceptionThrownForInvalidType() { public void testValidateMap() { Map inMap = new LinkedHashMap<>(); inMap.put("today", LocalDate.of(2017, 7, 21)); - FrozenMap validatedValue = mapJsonSchema.validate(inMap, configuration); + FrozenMap<@Nullable Object> validatedValue = mapJsonSchema.validate(inMap, configuration); LinkedHashMap outMap = new LinkedHashMap<>(); outMap.put("today", "2017-07-21"); Assert.assertEquals(validatedValue, outMap); diff --git a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/NullSchemaTest.java b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/NullSchemaTest.java index 88bf8cb1536..706d0a9ddad 100644 --- a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/NullSchemaTest.java +++ b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/NullSchemaTest.java @@ -25,6 +25,7 @@ public class NullSchemaTest { @Test + @SuppressWarnings("nullness") public void testValidateNull() { Void validatedValue = nullJsonSchema.validate(null, configuration); Assert.assertNull(validatedValue); diff --git a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/NumberSchemaTest.java b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/NumberSchemaTest.java index add7500fc2d..48e5ab7d76b 100644 --- a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/NumberSchemaTest.java +++ b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/NumberSchemaTest.java @@ -5,14 +5,22 @@ import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; import org.openapijsonschematools.client.configurations.SchemaConfiguration; import org.openapijsonschematools.client.schemas.validation.JsonSchema; -import org.openapijsonschematools.client.schemas.validation.JsonSchemaFactory; import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; +import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; -import java.math.BigDecimal; +import java.util.LinkedHashSet; +import java.util.List; public class NumberSchemaTest { static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone()); static final NumberJsonSchema numberJsonSchema = NumberJsonSchema.getInstance(); + static final ValidationMetadata validationMetadata = new ValidationMetadata( + List.of("args[0"), + configuration, + new PathToSchemasMap(), + new LinkedHashSet<>() + ); @Test public void testValidateInteger() { @@ -40,8 +48,10 @@ public void testValidateDouble() { @Test public void testExceptionThrownForInvalidType() { - Assert.assertThrows(ValidationException.class, () -> numberJsonSchema.validate( - null, configuration + Assert.assertThrows(ValidationException.class, () -> JsonSchema.validate( + numberJsonSchema, + null, + validationMetadata )); } } diff --git a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/ObjectTypeSchemaTest.java b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/ObjectTypeSchemaTest.java index 76872ad89fc..aafcd102b13 100644 --- a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/ObjectTypeSchemaTest.java +++ b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/ObjectTypeSchemaTest.java @@ -1,5 +1,6 @@ package org.openapijsonschematools.client.schemas; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; @@ -23,7 +24,6 @@ import java.util.Objects; import java.util.Set; - public class ObjectTypeSchemaTest { static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone()); static final ValidationMetadata validationMetadata = new ValidationMetadata( @@ -33,8 +33,8 @@ public class ObjectTypeSchemaTest { new LinkedHashSet<>() ); - public static class ObjectWithPropsSchema extends JsonSchema implements MapSchemaValidator> { - private static ObjectWithPropsSchema instance; + public static class ObjectWithPropsSchema extends JsonSchema implements MapSchemaValidator> { + private static @Nullable ObjectWithPropsSchema instance = null; private ObjectWithPropsSchema() { super(new JsonSchemaInfo() .type(Set.of(Map.class)) @@ -53,22 +53,30 @@ public static ObjectWithPropsSchema getInstance() { } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); properties.put(propertyName, castValue); } return new FrozenMap<>(properties); } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -79,16 +87,16 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } public static class ObjectWithAddpropsSchema extends JsonSchema implements MapSchemaValidator> { - private static ObjectWithAddpropsSchema instance; + private static @Nullable ObjectWithAddpropsSchema instance = null; private ObjectWithAddpropsSchema() { super(new JsonSchemaInfo() .type(Set.of(Map.class)) @@ -107,13 +115,24 @@ public static ObjectWithAddpropsSchema getInstance() { public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - String castValue = (String) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(castValue instanceof String)) { + throw new InvalidTypeException("Invalid type for property value"); + } + properties.put(propertyName, (String) castValue); } return new FrozenMap<>(properties); } @@ -130,17 +149,17 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof FrozenMap) { @SuppressWarnings("unchecked") FrozenMap castArg = (FrozenMap) arg; return getNewInstance(castArg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class ObjectWithPropsAndAddpropsSchema extends JsonSchema implements MapSchemaValidator> { - private static ObjectWithPropsAndAddpropsSchema instance; + public static class ObjectWithPropsAndAddpropsSchema extends JsonSchema implements MapSchemaValidator> { + private static @Nullable ObjectWithPropsAndAddpropsSchema instance = null; private ObjectWithPropsAndAddpropsSchema() { super(new JsonSchemaInfo() .type(Set.of(Map.class)) @@ -159,22 +178,30 @@ public static ObjectWithPropsAndAddpropsSchema getInstance() { } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); properties.put(propertyName, castValue); } return new FrozenMap<>(properties); } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -185,17 +212,17 @@ public FrozenMap validate(Map arg, SchemaConfiguration c } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof FrozenMap) { @SuppressWarnings("unchecked") FrozenMap castArg = (FrozenMap) arg; return getNewInstance(castArg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class ObjectWithOutputTypeSchemaMap extends FrozenMap { - protected ObjectWithOutputTypeSchemaMap(FrozenMap m) { + public static class ObjectWithOutputTypeSchemaMap extends FrozenMap<@Nullable Object> { + protected ObjectWithOutputTypeSchemaMap(FrozenMap<@Nullable Object> m) { super(m); } @@ -206,7 +233,7 @@ public static ObjectWithOutputTypeSchemaMap of(Map arg, SchemaCo public static class ObjectWithOutputTypeSchema extends JsonSchema implements MapSchemaValidator { - private static ObjectWithOutputTypeSchema instance; + private static @Nullable ObjectWithOutputTypeSchema instance = null; public ObjectWithOutputTypeSchema() { super(new JsonSchemaInfo() .type(Set.of(Map.class)) @@ -225,14 +252,22 @@ public static ObjectWithOutputTypeSchema getInstance() { @Override public ObjectWithOutputTypeSchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); properties.put(propertyName, castValue); } return new ObjectWithOutputTypeSchemaMap(new FrozenMap<>(properties)); @@ -250,11 +285,11 @@ public ObjectWithOutputTypeSchemaMap validate(Map arg, SchemaCon } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof FrozenMap) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } @@ -275,7 +310,7 @@ public void testValidateObjectWithPropsSchema() { // map with only property works Map inMap = new LinkedHashMap<>(); inMap.put("someString", "abc"); - FrozenMap validatedValue = schema.validate(inMap, configuration); + FrozenMap<@Nullable Object> validatedValue = schema.validate(inMap, configuration); LinkedHashMap outMap = new LinkedHashMap<>(); outMap.put("someString", "abc"); Assert.assertEquals(validatedValue, outMap); @@ -337,7 +372,7 @@ public void testValidateObjectWithPropsAndAddpropsSchema() { // map with only property works Map inMap = new LinkedHashMap<>(); inMap.put("someString", "abc"); - FrozenMap validatedValue = schema.validate(inMap, configuration); + FrozenMap<@Nullable Object> validatedValue = schema.validate(inMap, configuration); LinkedHashMap outMap = new LinkedHashMap<>(); outMap.put("someString", "abc"); Assert.assertEquals(validatedValue, outMap); diff --git a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/AdditionalPropertiesValidatorTest.java b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/AdditionalPropertiesValidatorTest.java index 01f2d0fcd18..c24c93ce54c 100644 --- a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/AdditionalPropertiesValidatorTest.java +++ b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/AdditionalPropertiesValidatorTest.java @@ -1,5 +1,6 @@ package org.openapijsonschematools.client.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; @@ -17,15 +18,14 @@ import java.util.Set; public class AdditionalPropertiesValidatorTest { - public static class ObjectWithPropsSchema extends JsonSchema { - private static ObjectWithPropsSchema instance; + private static @Nullable ObjectWithPropsSchema instance = null; private ObjectWithPropsSchema() { super(new JsonSchemaInfo() - .type(Set.of(FrozenMap.class)) - .properties(Map.ofEntries( - new PropertyEntry("someString", StringJsonSchema.class) - )) + .type(Set.of(FrozenMap.class)) + .properties(Map.ofEntries( + new PropertyEntry("someString", StringJsonSchema.class) + )) ); } @@ -38,15 +38,21 @@ public static ObjectWithPropsSchema getInstance() { } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof FrozenMap) { @SuppressWarnings("unchecked") FrozenMap castArg = (FrozenMap) arg; return castArg; } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } + @SuppressWarnings("nullness") + private Void assertNull(@Nullable Object object) { + Assert.assertNull(object); + return null; + } + @Test public void testCorrectPropertySucceeds() { List pathToItem = new ArrayList<>(); @@ -67,6 +73,9 @@ public void testCorrectPropertySucceeds() { arg, validationMetadata ); + if (pathToSchemas == null) { + throw new RuntimeException("Invalid null value for pathToSchemas for this test case"); + } List expectedPathToItem = new ArrayList<>(); expectedPathToItem.add("args[0]"); expectedPathToItem.add("someAddProp"); @@ -94,7 +103,7 @@ public void testNotApplicableTypeReturnsNull() { 1, validationMetadata ); - Assert.assertNull(pathToSchemas); + assertNull(pathToSchemas); } @Test diff --git a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/FormatValidatorTest.java b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/FormatValidatorTest.java index 8451ff94b0f..10f07ffc2f9 100644 --- a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/FormatValidatorTest.java +++ b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/FormatValidatorTest.java @@ -1,5 +1,6 @@ package org.openapijsonschematools.client.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; @@ -21,6 +22,12 @@ public class FormatValidatorTest { new LinkedHashSet<>() ); + @SuppressWarnings("nullness") + private Void assertNull(@Nullable Object object) { + Assert.assertNull(object); + return null; + } + @Test public void testIntFormatSucceedsWithFloat() { final FormatValidator validator = new FormatValidator("int"); @@ -29,7 +36,7 @@ public void testIntFormatSucceedsWithFloat() { 1.0f, validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -50,7 +57,7 @@ public void testIntFormatSucceedsWithInt() { 1, validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -71,7 +78,7 @@ public void testInt32InclusiveMinSucceeds() { -2147483648, validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -82,7 +89,7 @@ public void testInt32InclusiveMaxSucceeds() { 2147483647, validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -114,7 +121,7 @@ public void testInt64InclusiveMinSucceeds() { -9223372036854775808L, validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -125,7 +132,7 @@ public void testInt64InclusiveMaxSucceeds() { 9223372036854775807L, validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -157,7 +164,7 @@ public void testFloatInclusiveMinSucceeds() { -3.4028234663852886e+38f, validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -168,7 +175,7 @@ public void testFloatInclusiveMaxSucceeds() { 3.4028234663852886e+38f, validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -199,7 +206,7 @@ public void testDoubleInclusiveMinSucceeds() { -1.7976931348623157E+308d, validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -210,7 +217,7 @@ public void testDoubleInclusiveMaxSucceeds() { 1.7976931348623157E+308d, validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -241,7 +248,7 @@ public void testValidFloatNumberStringSucceeds() { "3.14", validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -252,7 +259,7 @@ public void testValidIntNumberStringSucceeds() { "1", validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -273,7 +280,7 @@ public void testValidDateStringSucceeds() { "2017-01-20", validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -294,6 +301,6 @@ public void testValidDateTimeStringSucceeds() { "2017-07-21T17:32:28Z", validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } } diff --git a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/ItemsValidatorTest.java b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/ItemsValidatorTest.java index fba34f90055..ffb72ed6c64 100644 --- a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/ItemsValidatorTest.java +++ b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/ItemsValidatorTest.java @@ -1,5 +1,6 @@ package org.openapijsonschematools.client.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; @@ -14,6 +15,11 @@ import java.util.List; public class ItemsValidatorTest { + @SuppressWarnings("nullness") + private Void assertNull(@Nullable Object object) { + Assert.assertNull(object); + return null; + } @Test public void testCorrectItemsSucceeds() { @@ -34,6 +40,9 @@ public void testCorrectItemsSucceeds() { arg, validationMetadata ); + if (pathToSchemas == null) { + throw new RuntimeException("Invalid null value in pathToSchemas for this test case"); + } List expectedPathToItem = new ArrayList<>(); expectedPathToItem.add("args[0]"); expectedPathToItem.add(0); @@ -61,7 +70,7 @@ public void testNotApplicableTypeReturnsNull() { 1, validationMetadata ); - Assert.assertNull(pathToSchemas); + assertNull(pathToSchemas); } @Test @@ -85,3 +94,4 @@ public void testIncorrectItemFails() { )); } } + diff --git a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaTest.java b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaTest.java index f66926cf23d..d4538bf4f7f 100644 --- a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaTest.java +++ b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/JsonSchemaTest.java @@ -1,5 +1,6 @@ package org.openapijsonschematools.client.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; @@ -13,7 +14,7 @@ import java.util.Set; class SomeSchema extends JsonSchema { - private static SomeSchema instance; + private static @Nullable SomeSchema instance = null; protected SomeSchema() { super(new JsonSchemaInfo() .type(Set.of(String.class)) @@ -28,11 +29,11 @@ public static SomeSchema getInstance() { } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof String) { return arg; } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } diff --git a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/PropertiesValidatorTest.java b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/PropertiesValidatorTest.java index 137a5445aaf..4d5d445aaa2 100644 --- a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/PropertiesValidatorTest.java +++ b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/PropertiesValidatorTest.java @@ -1,5 +1,6 @@ package org.openapijsonschematools.client.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; @@ -15,6 +16,12 @@ import java.util.Map; public class PropertiesValidatorTest { + @SuppressWarnings("nullness") + private Void assertNull(@Nullable Object object) { + Assert.assertNull(object); + return null; + } + @Test public void testCorrectPropertySucceeds() { Map> properties = new LinkedHashMap<>(); @@ -37,6 +44,9 @@ public void testCorrectPropertySucceeds() { arg, validationMetadata ); + if (pathToSchemas == null) { + throw new RuntimeException("Invalid null value for pathToSchemas for this test case"); + } List expectedPathToItem = new ArrayList<>(); expectedPathToItem.add("args[0]"); expectedPathToItem.add("someString"); @@ -66,7 +76,7 @@ public void testNotApplicableTypeReturnsNull() { 1, validationMetadata ); - Assert.assertNull(pathToSchemas); + assertNull(pathToSchemas); } @Test diff --git a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/RequiredValidatorTest.java b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/RequiredValidatorTest.java index 79daae996ab..37ee2fc6431 100644 --- a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/RequiredValidatorTest.java +++ b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/RequiredValidatorTest.java @@ -1,5 +1,6 @@ package org.openapijsonschematools.client.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; @@ -14,6 +15,11 @@ import java.util.Set; public class RequiredValidatorTest { + @SuppressWarnings("nullness") + private Void assertNull(@Nullable Object object) { + Assert.assertNull(object); + return null; + } @Test public void testCorrectPropertySucceeds() { @@ -37,7 +43,7 @@ public void testCorrectPropertySucceeds() { arg, validationMetadata ); - Assert.assertNull(pathToSchemas); + assertNull(pathToSchemas); } @Test @@ -59,7 +65,7 @@ public void testNotApplicableTypeReturnsNull() { 1, validationMetadata ); - Assert.assertNull(pathToSchemas); + assertNull(pathToSchemas); } @Test diff --git a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/TypeValidatorTest.java b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/TypeValidatorTest.java index 62f1d7f76a5..70c3049982c 100644 --- a/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/TypeValidatorTest.java +++ b/samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/schemas/validation/TypeValidatorTest.java @@ -1,5 +1,6 @@ package org.openapijsonschematools.client.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; @@ -11,6 +12,11 @@ import java.util.LinkedHashSet; public class TypeValidatorTest { + @SuppressWarnings("nullness") + private Void assertNull(@Nullable Object object) { + Assert.assertNull(object); + return null; + } @Test public void testValidateSucceeds() { @@ -23,12 +29,12 @@ public void testValidateSucceeds() { new PathToSchemasMap(), new LinkedHashSet<>() ); - PathToSchemasMap pathToSchemasMap = validator.validate( + @Nullable PathToSchemasMap pathToSchemasMap = validator.validate( StringJsonSchema.getInstance(), "hi", validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java b/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java index c0e7fcc7643..4da076cc91a 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java +++ b/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java @@ -1526,6 +1526,10 @@ public Set getImports(String sourceJsonPath, CodegenSchema schema, Featu if (schema.items != null) { imports.addAll(getDeeperImports(sourceJsonPath, schema.items)); } + if (schema.additionalProperties == null || !schema.additionalProperties.isBooleanSchemaFalse) { + imports.add("import "+packageName + ".exceptions.UnsetPropertyException;"); + imports.add("import "+packageName + ".exceptions.InvalidAdditionalPropertyException;"); + } } } return imports; @@ -1548,6 +1552,7 @@ private void addPropertiesValidator(CodegenSchema schema, Set imports) { imports.add("import "+packageName + ".schemas.validation.PropertyEntry;"); imports.add("import java.util.Map;"); imports.add("import java.util.Set;"); + imports.add("import "+packageName + ".exceptions.UnsetPropertyException;"); } } @@ -1597,6 +1602,8 @@ private void addCustomSchemaImports(Set imports, CodegenSchema schema) { imports.add("import "+packageName + ".schemas.validation.PathToSchemasMap;"); // for getNewInstance imports.add("import "+packageName + ".schemas.validation.ValidationMetadata;"); // for getNewInstance imports.add("import "+packageName + ".configurations.JsonSchemaKeywordFlags;"); // for getNewInstance + imports.add("import org.checkerframework.checker.nullness.qual.NonNull;"); + imports.add("import org.checkerframework.checker.nullness.qual.Nullable;"); } private void addBooleanSchemaImports(Set imports, CodegenSchema schema) { @@ -1625,6 +1632,10 @@ private void addMapSchemaImports(Set imports, CodegenSchema schema) { addAllOfValidator(schema, imports); addAnyOfValidator(schema, imports); addOneOfValidator(schema, imports); + if (schema.additionalProperties == null || !schema.additionalProperties.isBooleanSchemaFalse) { + imports.add("import "+packageName + ".exceptions.UnsetPropertyException;"); + imports.add("import "+packageName + ".exceptions.InvalidAdditionalPropertyException;"); + } } private void addListSchemaImports(Set imports, CodegenSchema schema) { diff --git a/src/main/resources/java/pom.hbs b/src/main/resources/java/pom.hbs index 1e0ba83059c..b44657d7801 100644 --- a/src/main/resources/java/pom.hbs +++ b/src/main/resources/java/pom.hbs @@ -50,13 +50,34 @@ true 128m 512m - - -Xlint:all + -Xlint:all + + -Awarns -J-Xss4m + -J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED + -J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED + + + org.checkerframework + checker + ${checker-version} + + + + + org.checkerframework.checker.nullness.NullnessChecker + + - org.apache.maven.plugins maven-enforcer-plugin @@ -220,13 +241,17 @@ ${swagger-annotations-version} - + - com.google.code.findbugs - jsr305 - 3.0.2 + org.checkerframework + checker-qual + ${checker-version} + + + org.checkerframework + checker + ${checker-version} - @@ -317,6 +342,7 @@ 17 UTF-8 1.6.3 + 3.34.0 1.19.4 2.12.6 2.12.6.1 diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_anytypeOrMultitype.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_anytypeOrMultitype.hbs index d20daedd22d..7ed1c36241c 100644 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_anytypeOrMultitype.hbs +++ b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_anytypeOrMultitype.hbs @@ -1,9 +1,9 @@ {{#if types}} -public static class {{jsonPathPiece.camelCase}} extends JsonSchema implements {{#each types}}{{#eq this "null"}}NullSchemaValidator{{else}}{{#eq this "boolean"}}BooleanSchemaValidator{{else}}{{#or (eq this "number") (eq this "integer")}}NumberSchemaValidator{{else}}{{#eq this "string"}}StringSchemaValidator{{else}}{{#eq this "array"}}ListSchemaValidator<{{#with items}}{{> src/main/java/packagename/components/schemas/types/schema_input_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}, {{#if arrayOutputJsonPathPiece}}{{arrayOutputJsonPathPiece.camelCase}}{{else}}FrozenList{{/if}}>{{else}}{{#eq this "object"}}MapSchemaValidator<{{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_input_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}, {{#if mapOutputJsonPathPiece}}{{mapOutputJsonPathPiece.camelCase}}{{else}}FrozenMap<{{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}>{{/if}}>{{/eq}}{{/eq}}{{/eq}}{{/or}}{{/eq}}{{/eq}}{{#unless @last}}, {{/unless}}{{/each}} { +public static class {{jsonPathPiece.camelCase}} extends JsonSchema implements {{#each types}}{{#eq this "null"}}NullSchemaValidator{{else}}{{#eq this "boolean"}}BooleanSchemaValidator{{else}}{{#or (eq this "number") (eq this "integer")}}NumberSchemaValidator{{else}}{{#eq this "string"}}StringSchemaValidator{{else}}{{#eq this "array"}}ListSchemaValidator<{{#with items}}{{> src/main/java/packagename/components/schemas/types/schema_input_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}, {{#if arrayOutputJsonPathPiece}}{{arrayOutputJsonPathPiece.camelCase}}{{else}}FrozenList<@Nullable Object>{{/if}}>{{else}}{{#eq this "object"}}MapSchemaValidator<{{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_input_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}, {{#if mapOutputJsonPathPiece}}{{mapOutputJsonPathPiece.camelCase}}{{else}}FrozenMap<{{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}>{{/if}}>{{/eq}}{{/eq}}{{/eq}}{{/or}}{{/eq}}{{/eq}}{{#unless @last}}, {{/unless}}{{/each}} { {{else}} -public static class {{jsonPathPiece.camelCase}} extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<{{#with items}}{{> src/main/java/packagename/components/schemas/types/schema_input_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}, {{#if arrayOutputJsonPathPiece}}{{arrayOutputJsonPathPiece.camelCase}}{{else}}FrozenList{{/if}}>, MapSchemaValidator<{{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_input_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}, {{#if mapOutputJsonPathPiece}}{{mapOutputJsonPathPiece.camelCase}}{{else}}FrozenMap<{{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}>{{/if}}> { +public static class {{jsonPathPiece.camelCase}} extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator<{{#with items}}{{> src/main/java/packagename/components/schemas/types/schema_input_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}, {{#if arrayOutputJsonPathPiece}}{{arrayOutputJsonPathPiece.camelCase}}{{else}}FrozenList<@Nullable Object>{{/if}}>, MapSchemaValidator<{{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_input_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}, {{#if mapOutputJsonPathPiece}}{{mapOutputJsonPathPiece.camelCase}}{{else}}FrozenMap<{{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}>{{/if}}> { {{/if}} {{#if componentModule}} /* @@ -17,7 +17,7 @@ public static class {{jsonPathPiece.camelCase}} extends JsonSchema implements Nu {{/if}} */ {{/if}} - private static {{jsonPathPiece.camelCase}} instance; + private static @Nullable {{jsonPathPiece.camelCase}} instance = null; protected {{../jsonPathPiece.camelCase}}() { super(new JsonSchemaInfo() diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_boolean.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_boolean.hbs index 83daa52d9f7..5202418cf36 100644 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_boolean.hbs +++ b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_boolean.hbs @@ -13,7 +13,7 @@ public static class {{jsonPathPiece.camelCase}} extends JsonSchema implements Bo {{/if}} */ {{/if}} - private static {{jsonPathPiece.camelCase}} instance; + private static @Nullable {{jsonPathPiece.camelCase}} instance = null; protected {{../jsonPathPiece.camelCase}}() { super(new JsonSchemaInfo() diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_list.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_list.hbs index 68ecae1d8c7..01cccab64dd 100644 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_list.hbs +++ b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_list.hbs @@ -13,7 +13,7 @@ public static class {{jsonPathPiece.camelCase}} extends JsonSchema implements Li {{/if}} */ {{/if}} - private static {{jsonPathPiece.camelCase}} instance; + private static @Nullable {{jsonPathPiece.camelCase}} instance = null; protected {{../jsonPathPiece.camelCase}}() { super(new JsonSchemaInfo() diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_map.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_map.hbs index 8f5de98e12e..2db5d9e6295 100644 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_map.hbs +++ b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_map.hbs @@ -1,6 +1,6 @@ -public static class {{jsonPathPiece.camelCase}} extends JsonSchema implements MapSchemaValidator<{{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_input_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}, {{#if mapOutputJsonPathPiece}}{{mapOutputJsonPathPiece.camelCase}}{{else}}FrozenMap<{{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}>{{/if}}> { +public static class {{jsonPathPiece.camelCase}} extends JsonSchema implements MapSchemaValidator<{{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_input_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}, {{#if mapOutputJsonPathPiece}}{{mapOutputJsonPathPiece.camelCase}}{{else}}FrozenMap<{{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}>{{/if}}> { {{#if componentModule}} /* NOTE: This class is auto generated by OpenAPI JSON Schema Generator. @@ -13,7 +13,7 @@ public static class {{jsonPathPiece.camelCase}} extends JsonSchema implements Ma {{/if}} */ {{/if}} - private static {{jsonPathPiece.camelCase}} instance; + private static @Nullable {{jsonPathPiece.camelCase}} instance = null; protected {{../jsonPathPiece.camelCase}}() { super(new JsonSchemaInfo() diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_null.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_null.hbs index 099e5ec4c1d..fb3007fe179 100644 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_null.hbs +++ b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_null.hbs @@ -13,7 +13,7 @@ public static class {{jsonPathPiece.camelCase}} extends JsonSchema implements Nu {{/if}} */ {{/if}} - private static {{jsonPathPiece.camelCase}} instance; + private static @Nullable {{jsonPathPiece.camelCase}} instance = null; protected {{../jsonPathPiece.camelCase}}() { super(new JsonSchemaInfo() diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_number.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_number.hbs index 53d54ccac5d..0c4103f1a7c 100644 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_number.hbs +++ b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_number.hbs @@ -13,7 +13,7 @@ public static class {{jsonPathPiece.camelCase}} extends JsonSchema implements Nu {{/if}} */ {{/if}} - private static {{jsonPathPiece.camelCase}} instance; + private static @Nullable {{jsonPathPiece.camelCase}} instance = null; protected {{../jsonPathPiece.camelCase}}() { super(new JsonSchemaInfo() diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_string.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_string.hbs index 7997b69df62..04e6b04d7c5 100644 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_string.hbs +++ b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_Schema_string.hbs @@ -13,7 +13,7 @@ public static class {{jsonPathPiece.camelCase}} extends JsonSchema implements St {{/if}} */ {{/if}} - private static {{jsonPathPiece.camelCase}} instance; + private static @Nullable {{jsonPathPiece.camelCase}} instance = null; protected {{../jsonPathPiece.camelCase}}() { super(new JsonSchemaInfo() diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_getNewInstanceObject_implementor.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_getNewInstanceObject_implementor.hbs index c4a86c51f5c..28ecaca28b6 100644 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_getNewInstanceObject_implementor.hbs +++ b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_getNewInstanceObject_implementor.hbs @@ -1,6 +1,6 @@ @Override -public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { +public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { {{#if types}} {{#each types}} {{#if @first}} @@ -47,5 +47,5 @@ public Object getNewInstance(Object arg, List pathToItem, PathToSchemasM return getNewInstance((Map) arg, pathToItem, pathToSchemas); } {{/if}} - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_validate_implementor.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_validate_implementor.hbs index 03274cc4492..a10167c3cb4 100644 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_validate_implementor.hbs +++ b/src/main/resources/java/src/main/java/packagename/components/schemas/SchemaClass/_validate_implementor.hbs @@ -15,18 +15,29 @@ public Void validate(Void arg, SchemaConfiguration configuration) throws Validat {{/eq}} {{#eq this "object"}} -public {{#if ../mapOutputJsonPathPiece}}{{../mapOutputJsonPathPiece.camelCase}}{{else}}FrozenMap{{/if}} getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}> properties = new LinkedHashMap<>(); +public {{#if ../mapOutputJsonPathPiece}}{{../mapOutputJsonPathPiece.camelCase}}{{else}}FrozenMap<@Nullable Object>{{/if}} getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}> properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - {{#with ../mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}} castValue = ({{#with ../mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof {{#with ../mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true noAnnotations=true }}{{else}}Object{{/with}})) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, ({{#with ../mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}) propertyInstance); } - FrozenMap<{{#with ../mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}> castProperties = new FrozenMap<>(properties); + FrozenMap<{{#with ../mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}> castProperties = new FrozenMap<>(properties); {{#if ../mapOutputJsonPathPiece}} return new {{mapOutputJsonPathPiece.camelCase}}(castProperties); {{else}} @@ -35,7 +46,7 @@ public {{#if ../mapOutputJsonPathPiece}}{{../mapOutputJsonPathPiece.camelCase}}{ } @Override -public {{#if ../mapOutputJsonPathPiece}}{{../mapOutputJsonPathPiece.camelCase}}{{else}}FrozenMap{{/if}} validate(Map src/main/java/packagename/components/schemas/types/schema_input_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}> arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { +public {{#if ../mapOutputJsonPathPiece}}{{../mapOutputJsonPathPiece.camelCase}}{{else}}FrozenMap<@Nullable Object>{{/if}} validate(Map src/main/java/packagename/components/schemas/types/schema_input_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}> arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -49,18 +60,25 @@ public {{#if ../mapOutputJsonPathPiece}}{{../mapOutputJsonPathPiece.camelCase}}{ {{#eq this "array"}} @Override -public {{#if ../arrayOutputJsonPathPiece}}{{../arrayOutputJsonPathPiece.camelCase}}{{else}}FrozenList<{{#with ../items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}>{{/if}} getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List<{{#with ../items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}> items = new ArrayList<>(); +public {{#if ../arrayOutputJsonPathPiece}}{{../arrayOutputJsonPathPiece.camelCase}}{{else}}FrozenList<{{#with ../items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}>{{/if}} getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<{{#with ../items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - {{#with items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}} castItem = ({{#with items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof {{#with ../items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true noAnnotations=true }}{{else}}Object{{/with}})) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add(({{#with ../items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}) itemInstance); i += 1; } - FrozenList<{{#with items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}> newInstanceItems = new FrozenList<>(items); + FrozenList<{{#with items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}> newInstanceItems = new FrozenList<>(items); {{#if ../arrayOutputJsonPathPiece}} return new {{../arrayOutputJsonPathPiece.camelCase}}(newInstanceItems); {{else}} @@ -69,7 +87,7 @@ public {{#if ../arrayOutputJsonPathPiece}}{{../arrayOutputJsonPathPiece.camelCas } @Override -public {{#if ../arrayOutputJsonPathPiece}}{{../arrayOutputJsonPathPiece.camelCase}}{{else}}FrozenList<{{#with ../items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}>{{/if}} validate(List<{{#with ../items}}{{> src/main/java/packagename/components/schemas/types/schema_input_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}> arg, SchemaConfiguration configuration) throws ValidationException { +public {{#if ../arrayOutputJsonPathPiece}}{{../arrayOutputJsonPathPiece.camelCase}}{{else}}FrozenList<{{#with ../items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}>{{/if}} validate(List<{{#with ../items}}{{> src/main/java/packagename/components/schemas/types/schema_input_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}> arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -295,18 +313,25 @@ public String validate(UUID arg, SchemaConfiguration configuration) throws Valid } @Override -public {{#if arrayOutputJsonPathPiece}}{{arrayOutputJsonPathPiece.camelCase}}{{else}}FrozenList<{{#with items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}>{{/if}} getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List<{{#with items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}> items = new ArrayList<>(); +public {{#if arrayOutputJsonPathPiece}}{{arrayOutputJsonPathPiece.camelCase}}{{else}}FrozenList<{{#with items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}>{{/if}} getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<{{#with items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - {{#with items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}} castItem = ({{#with items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof {{#with items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true noAnnotations=true }}{{else}}Object{{/with}})) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add(({{#with items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}) itemInstance); i += 1; } - FrozenList<{{#with items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}> newInstanceItems = new FrozenList<>(items); + FrozenList<{{#with items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}> newInstanceItems = new FrozenList<>(items); {{#if arrayOutputJsonPathPiece}} return new {{arrayOutputJsonPathPiece.camelCase}}(newInstanceItems); {{else}} @@ -315,7 +340,7 @@ public {{#if arrayOutputJsonPathPiece}}{{arrayOutputJsonPathPiece.camelCase}}{{e } @Override -public {{#if arrayOutputJsonPathPiece}}{{arrayOutputJsonPathPiece.camelCase}}{{else}}FrozenList<{{#with ../items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}>{{/if}} validate(List<{{#with items}}{{> src/main/java/packagename/components/schemas/types/schema_input_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}> arg, SchemaConfiguration configuration) throws ValidationException { +public {{#if arrayOutputJsonPathPiece}}{{arrayOutputJsonPathPiece.camelCase}}{{else}}FrozenList<{{#with ../items}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}>{{/if}} validate(List<{{#with items}}{{> src/main/java/packagename/components/schemas/types/schema_input_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}> arg, SchemaConfiguration configuration) throws ValidationException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); List castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -326,18 +351,29 @@ public {{#if arrayOutputJsonPathPiece}}{{arrayOutputJsonPathPiece.camelCase}}{{e } @Override -public {{#if mapOutputJsonPathPiece}}{{mapOutputJsonPathPiece.camelCase}}{{else}}FrozenMap<{{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}>{{/if}} getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}> properties = new LinkedHashMap<>(); +public {{#if mapOutputJsonPathPiece}}{{mapOutputJsonPathPiece.camelCase}}{{else}}FrozenMap<{{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}>{{/if}} getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}> properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - {{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}} castValue = ({{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof {{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true noAnnotations=true }}{{else}}Object{{/with}})) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, ({{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}) propertyInstance); } - FrozenMap<{{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}> castProperties = new FrozenMap<>(properties); + FrozenMap<{{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}> castProperties = new FrozenMap<>(properties); {{#if ../mapOutputJsonPathPiece}} return new {{mapOutputJsonPathPiece.camelCase}}(castProperties); {{else}} @@ -346,7 +382,7 @@ public {{#if mapOutputJsonPathPiece}}{{mapOutputJsonPathPiece.camelCase}}{{else} } @Override -public {{#if mapOutputJsonPathPiece}}{{mapOutputJsonPathPiece.camelCase}}{{else}}FrozenMap<{{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}>{{/if}} validate(Map src/main/java/packagename/components/schemas/types/schema_input_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}> arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { +public {{#if mapOutputJsonPathPiece}}{{mapOutputJsonPathPiece.camelCase}}{{else}}FrozenMap<{{#with mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}@Nullable Object{{/with}}>{{/if}} validate(Map src/main/java/packagename/components/schemas/types/schema_input_type sourceJsonPath=../jsonPath forceNull=true }}{{else}}Object{{/with}}> arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/_objectOutputGetProperty.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/_objectOutputGetProperty.hbs index cc9b9325e85..673a71666ff 100644 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/_objectOutputGetProperty.hbs +++ b/src/main/resources/java/src/main/java/packagename/components/schemas/_objectOutputGetProperty.hbs @@ -2,7 +2,11 @@ {{#and ../../mapValueSchema (eq typeSchema ../../mapValueSchema.typeSchema) }} return get("{{{@key.original}}}"); {{else}} -return ({{> src/main/java/packagename/components/schemas/types/schema_output_type fullRefModule="" }}) get("{{{@key.original}}}"); +{{#with ../../mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type fullRefModule="" }}{{/with}} value = get("{{{@key.original}}}"); +if (!(value instanceof {{> src/main/java/packagename/components/schemas/types/schema_output_type fullRefModule="" forceNull=true noAnnotations=true }})) { + throw new InvalidTypeException("Invalid value stored for {{{@key.original}}}"); +} +return ({{> src/main/java/packagename/components/schemas/types/schema_output_type fullRefModule="" }}) value; {{/and}} {{else}} String key = "{{{@key.original}}}"; @@ -10,6 +14,10 @@ throwIfKeyNotPresent(key); {{#and ../../mapValueSchema (eq typeSchema ../../mapValueSchema.typeSchema) }} return get(key); {{else}} -return ({{> src/main/java/packagename/components/schemas/types/schema_output_type fullRefModule="" }}) get(key); +{{#with ../../mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type fullRefModule="" }}{{/with}} value = get(key); +if (!(value instanceof {{> src/main/java/packagename/components/schemas/types/schema_output_type fullRefModule="" forceNull=true noAnnotations=true }})) { + throw new InvalidTypeException("Invalid value stored for {{{@key.original}}}"); +} +return ({{> src/main/java/packagename/components/schemas/types/schema_output_type fullRefModule="" }}) value; {{/and}} {{/if}} diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/_objectOutputProperties.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/_objectOutputProperties.hbs index 0127b817d96..20012498d88 100644 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/_objectOutputProperties.hbs +++ b/src/main/resources/java/src/main/java/packagename/components/schemas/_objectOutputProperties.hbs @@ -16,7 +16,7 @@ public {{> src/main/java/packagename/components/schemas/types/schema_output_type {{else}} {{! for when additionalProperties is unset, use OUTPUT_BASE_TYPES }} -public Object {{@key.original}}() { +public @Nullable Object {{@key.original}}() { {{> src/main/java/packagename/components/schemas/_objectOutputGetProperty required=true }} } {{/if}} @@ -30,7 +30,7 @@ public Object {{@key.original}}() { {{#if @key.isValid}} {{#with this}} -public {{> src/main/java/packagename/components/schemas/types/schema_output_type }} {{{@key.original}}}() { +public {{> src/main/java/packagename/components/schemas/types/schema_output_type }} {{{@key.original}}}() throws UnsetPropertyException { {{> src/main/java/packagename/components/schemas/_objectOutputGetProperty required=false }} } {{/with}} @@ -39,7 +39,7 @@ public {{> src/main/java/packagename/components/schemas/types/schema_output_type {{/if}} {{#eq additionalProperties null }} -public Object getAdditionalProperty(String name) { +public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { {{#or optionalProperties requiredProperties}} throwIfKeyKnown(name, requiredKeys, optionalKeys); {{/or}} @@ -50,15 +50,27 @@ public Object getAdditionalProperty(String name) { {{#with additionalProperties}} {{#unless isBooleanSchemaFalse}} -public {{> src/main/java/packagename/components/schemas/types/schema_output_type }} getAdditionalProperty(String name) { - {{#or optionalProperties requiredProperties}} +public {{> src/main/java/packagename/components/schemas/types/schema_output_type }} getAdditionalProperty(String name) throws UnsetPropertyException{{#or ../optionalProperties ../requiredProperties}}, InvalidAdditionalPropertyException{{/or}} { + {{#or ../optionalProperties ../requiredProperties}} throwIfKeyKnown(name, requiredKeys, optionalKeys); {{/or}} throwIfKeyNotPresent(name); {{#and ../../mapValueSchema (eq typeSchema ../../mapValueSchema.typeSchema) }} + {{#and (neq types null) (eq types.size 1) (contains types "boolean") }} + Boolean value = get(name); + if (value == null) { + throw new InvalidTypeException("Value may not be null"); + } + return (boolean) value; + {{else}} return get(name); + {{/and}} {{else}} - return ({{> src/main/java/packagename/components/schemas/types/schema_output_type fullRefModule="" }}) get(name); + {{#with ../../mapValueSchema}}{{> src/main/java/packagename/components/schemas/types/schema_output_type fullRefModule="" }}{{/with}} value = get(name); + if (!(value instanceof {{> src/main/java/packagename/components/schemas/types/schema_output_type fullRefModule="" forceNull=true noAnnotations=true }})) { + throw new InvalidTypeException("Invalid value stored for " + name); + } + return ({{> src/main/java/packagename/components/schemas/types/schema_output_type fullRefModule="" }}) value; {{/and}} } {{/unless}} diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/types/_schema_cast_type_base.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/types/_schema_cast_type_base.hbs deleted file mode 100644 index 97d38dcc03d..00000000000 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/types/_schema_cast_type_base.hbs +++ /dev/null @@ -1,206 +0,0 @@ -{{#if types}} - {{#eq types.size 1}} - {{#each types}} - {{#eq this "boolean"}} - {{#if forceNull}} -Boolean - {{~else}} -boolean - {{~/if}} - {{else}} - {{#eq this "null"}} -Void - {{~else}} - {{#eq this "string"}} - {{#eq format "binary"}} -{{! todo binary types}} -String - {{~else}} -String - {{~/eq}} - {{else}} - {{#eq this "number"}} - {{#eq ../format null}} -Number - {{~else}} - {{#eq ../format "float"}} - {{#if forceNull}} -Float - {{~else}} -float - {{~/if}} - {{else}} - {{#eq ../format "double"}} - {{#if forceNull}} -Double - {{~else}} -double - {{~/if}} - {{else}} - {{#eq ../format "int32"}} - {{#if forceNull}} -Integer - {{~else}} -int - {{~/if}} - {{else}} - {{#eq ../format "int64"}} - {{#if forceNull}} -Long - {{~else}} -long - {{~/if}} - {{/eq}} - {{/eq}} - {{/eq}} - {{/eq}} - {{/eq}} - {{else}} - {{#eq this "integer"}} - {{#eq ../format null}} - {{#if forceNull}} -Long - {{~else}} -long - {{~/if}} - {{else}} - {{#eq ../format "int32"}} - {{#if forceNull}} -Integer - {{~else}} -int - {{~/if}} - {{else}} - {{#eq ../format "int64"}} - {{#if forceNull}} -Long - {{~else}} -long - {{~/if}} - {{/eq}} - {{/eq}} - {{/eq}} - {{else}} - {{#eq this "array"}} - {{#eq ../jsonPath sourceJsonPath}} -FrozenList - {{~else}} - {{#with items}} -FrozenList<{{> src/main/java/packagename/components/schemas/types/schema_cast_type }}> - {{~else}} -FrozenList - {{~/with}} - {{/eq}} - {{else}} - {{#eq this "object"}} - {{#eq ../jsonPath sourceJsonPath}} -FrozenMap - {{~else}} - {{#with mapValueSchema}} -FrozenMap<{{> src/main/java/packagename/components/schemas/types/schema_cast_type }}> - {{~else}} -FrozenMap - {{~/with}} - {{/eq}} - {{/eq}} - {{/eq}} - {{/eq}} - {{/eq}} - {{/eq}} - {{/eq}} - {{/eq}} - {{/each}} - {{else}} - {{#eq types.size 2}} - {{#contains types "null"}} - {{! nullable types }} - {{#each types}} - {{#eq this "boolean"}} -Boolean - {{~else}} - {{#eq this "string"}} - {{#eq format "binary"}} -{{! todo binary types}} -String - {{~else}} -String - {{~/eq}} - {{else}} - {{#eq this "number"}} - {{#eq ../format null}} -Number - {{~else}} - {{#eq ../format "float"}} -Float - {{~else}} - {{#eq ../format "double"}} -Double - {{~else}} - {{#eq ../format "int32"}} -Integer - {{~else}} - {{#eq ../format "int64"}} -Long - {{~/eq}} - {{/eq}} - {{/eq}} - {{/eq}} - {{/eq}} - {{else}} - {{#eq this "integer"}} - {{#eq ../format null}} -Long - {{~else}} - {{#eq ../format "int32"}} -Integer - {{~else}} - {{#eq ../format "int64"}} -Long - {{~/eq}} - {{/eq}} - {{/eq}} - {{else}} - {{#eq this "array"}} - {{#eq ../jsonPath sourceJsonPath}} -FrozenList - {{~else}} - {{#with items}} -FrozenList<{{> src/main/java/packagename/components/schemas/types/schema_cast_type }}> - {{~else}} -FrozenList - {{~/with}} - {{/eq}} - {{else}} - {{#eq this "object"}} - {{#eq ../jsonPath sourceJsonPath}} -FrozenMap - {{~else}} - {{#with mapValueSchema}} -FrozenMap<{{> src/main/java/packagename/components/schemas/types/schema_cast_type }}> - {{~else}} -FrozenMap - {{~/with}} - {{/eq}} - {{/eq}} - {{/eq}} - {{/eq}} - {{/eq}} - {{/eq}} - {{/eq}} - {{/each}} - {{else}} - {{! none of the types are null }} - {{#and (contains types "integer") (contains types "number") }} -Number - {{~else}} -Object - {{~/and}} - {{/contains}} - {{else}} - {{! 3 or more types }} -Object - {{~/eq}} - {{/eq}} -{{else}} -Object -{{~/if}} \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/types/_schema_input_type_base.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/types/_schema_input_type_base.hbs index f4a1d05d1bc..5128c83d4d4 100644 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/types/_schema_input_type_base.hbs +++ b/src/main/resources/java/src/main/java/packagename/components/schemas/types/_schema_input_type_base.hbs @@ -88,7 +88,7 @@ List {{#with items}} List<{{> src/main/java/packagename/components/schemas/types/schema_input_type }}> {{~else}} -List +List<@Nullable Object> {{~/with}} {{/eq}} {{else}} @@ -99,7 +99,7 @@ Map {{#with mapValueSchema}} Map src/main/java/packagename/components/schemas/types/schema_input_type }}> {{~else}} -Map +Map {{~/with}} {{/eq}} {{/eq}} @@ -116,31 +116,31 @@ Map {{! nullable types }} {{#each types}} {{#eq this "boolean"}} -Boolean +@Nullable Boolean {{~else}} {{#eq this "string"}} {{#eq format "binary"}} {{! todo binary types}} -String +@Nullable String {{~else}} -String +@Nullable String {{~/eq}} {{else}} {{#eq this "number"}} {{#eq ../format null}} -Number +@Nullable Number {{~else}} {{#eq ../format "float"}} -Float +@Nullable Float {{~else}} {{#eq ../format "double"}} -Double +@Nullable Double {{~else}} {{#eq ../format "int32"}} -Integer +@Nullable Integer {{~else}} {{#eq ../format "int64"}} -Long +@Nullable Long {{~/eq}} {{/eq}} {{/eq}} @@ -149,36 +149,36 @@ Long {{else}} {{#eq this "integer"}} {{#eq ../format null}} -Long +@Nullable Long {{~else}} {{#eq ../format "int32"}} -Integer +@Nullable Integer {{~else}} {{#eq ../format "int64"}} -Long +@Nullable Long {{~/eq}} {{/eq}} {{/eq}} {{else}} {{#eq this "array"}} {{#eq ../jsonPath sourceJsonPath}} -List +@Nullable List {{~else}} {{#with items}} -List<{{> src/main/java/packagename/components/schemas/types/schema_input_type }}> +@Nullable List<{{> src/main/java/packagename/components/schemas/types/schema_input_type }}> {{~else}} -List +@Nullable List<@Nullable Object> {{~/with}} {{/eq}} {{else}} {{#eq this "object"}} {{#eq ../jsonPath sourceJsonPath}} -Map +@Nullable Map {{~else}} {{#with mapValueSchema}} -Map src/main/java/packagename/components/schemas/types/schema_input_type }}> +@Nullable Map src/main/java/packagename/components/schemas/types/schema_input_type }}> {{~else}} -Map +@Nullable Map {{~/with}} {{/eq}} {{/eq}} @@ -198,9 +198,13 @@ Object {{/contains}} {{else}} {{! 3 or more types }} + {{#contains types "null"}} +@Nullable Object + {{else}} Object + {{/contains}} {{~/eq}} {{/eq}} {{else}} -Object +@Nullable Object {{~/if}} \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/types/_schema_output_type_base.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/types/_schema_output_type_base.hbs index 59d096cddab..fdc8ceb5ded 100644 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/types/_schema_output_type_base.hbs +++ b/src/main/resources/java/src/main/java/packagename/components/schemas/types/_schema_output_type_base.hbs @@ -97,7 +97,7 @@ long {{~/if}} {{/if}} {{else}} -FrozenList +FrozenList<@Nullable Object> {{~/if}} {{else}} {{#eq this "object"}} @@ -116,7 +116,7 @@ FrozenList {{~/if}} {{/if}} {{~else}} -FrozenMap +FrozenMap<@Nullable Object> {{~/if}} {{/eq}} {{/eq}} @@ -132,31 +132,31 @@ FrozenMap {{! nullable types }} {{#each types}} {{#eq this "boolean"}} -Boolean +{{#unless noAnnotations}}@Nullable {{/unless}}Boolean {{~else}} {{#eq this "string"}} {{#eq format "binary"}} {{! todo binary types}} -String +{{#unless noAnnotations}}@Nullable {{/unless}}String {{~else}} -String +{{#unless noAnnotations}}@Nullable {{/unless}}String {{~/eq}} {{else}} {{#eq this "number"}} {{#eq ../format null}} -Number +{{#unless noAnnotations}}@Nullable {{/unless}}Number {{~else}} {{#eq ../format "float"}} -Float +{{#unless noAnnotations}}@Nullable {{/unless}}Float {{~else}} {{#eq ../format "double"}} -Double +{{#unless noAnnotations}}@Nullable {{/unless}}Double {{~else}} {{#eq ../format "int32"}} -Integer +{{#unless noAnnotations}}@Nullable {{/unless}}Integer {{~else}} {{#eq ../format "int64"}} -Long +{{#unless noAnnotations}}@Nullable {{/unless}}Long {{~/eq}} {{/eq}} {{/eq}} @@ -165,13 +165,13 @@ Long {{else}} {{#eq this "integer"}} {{#eq ../format null}} -Long +{{#unless noAnnotations}}@Nullable {{/unless}}Long {{~else}} {{#eq ../format "int32"}} -Integer +{{#unless noAnnotations}}@Nullable {{/unless}}Integer {{~else}} {{#eq ../format "int64"}} -Long +{{#unless noAnnotations}}@Nullable {{/unless}}Long {{~/eq}} {{/eq}} {{/eq}} @@ -186,13 +186,13 @@ Long {{~/if}} {{else}} {{#if fullRefModule}} -{{fullRefModule}}.{{arrayOutputJsonPathPiece.camelCase}} +{{#unless noAnnotations}}@Nullable {{/unless}}{{fullRefModule}}.{{arrayOutputJsonPathPiece.camelCase}} {{~else}} -{{arrayOutputJsonPathPiece.camelCase}} +{{#unless noAnnotations}}@Nullable {{/unless}}{{arrayOutputJsonPathPiece.camelCase}} {{~/if}} {{/if}} {{else}} -FrozenList +{{#unless noAnnotations}}@Nullable {{/unless}}FrozenList<@Nullable Object> {{~/if}} {{else}} {{#eq this "object"}} @@ -205,13 +205,13 @@ FrozenList {{~/if}} {{else}} {{#if fullRefModule}} -{{fullRefModule}}.{{mapOutputJsonPathPiece.camelCase}} +{{#unless noAnnotations}}@Nullable {{/unless}}{{fullRefModule}}.{{mapOutputJsonPathPiece.camelCase}} {{~else}} -{{mapOutputJsonPathPiece.camelCase}} +{{#unless noAnnotations}}@Nullable {{/unless}}{{mapOutputJsonPathPiece.camelCase}} {{~/if}} {{/if}} {{~else}} -FrozenMap +{{#unless noAnnotations}}@Nullable {{/unless}}FrozenMap<@Nullable Object> {{~/if}} {{/eq}} {{/eq}} @@ -230,9 +230,13 @@ Object {{/contains}} {{else}} {{! 3 or more types }} + {{#contains types "null"}} +{{#unless noAnnotations}}@Nullable {{/unless}}Object + {{else}} Object + {{/contains}} {{~/eq}} {{/eq}} {{else}} -Object +{{#unless noAnnotations}}@Nullable {{/unless}}Object {{~/if}} diff --git a/src/main/resources/java/src/main/java/packagename/components/schemas/types/schema_cast_type.hbs b/src/main/resources/java/src/main/java/packagename/components/schemas/types/schema_cast_type.hbs deleted file mode 100644 index 7b949d47d08..00000000000 --- a/src/main/resources/java/src/main/java/packagename/components/schemas/types/schema_cast_type.hbs +++ /dev/null @@ -1,13 +0,0 @@ -{{#if refInfo}} - {{#if refInfo.refModule}} - {{#with getDeepestRef}} -{{> src/main/java/packagename/components/schemas/types/_schema_cast_type_base }} - {{/with}} - {{else}} - {{#with refInfo.ref}} -{{> src/main/java/packagename/components/schemas/types/_schema_cast_type_base }} - {{/with}} - {{/if}} -{{else}} -{{> src/main/java/packagename/components/schemas/types/_schema_cast_type_base }} -{{/if}} \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/exceptions/BaseException.hbs b/src/main/resources/java/src/main/java/packagename/exceptions/BaseException.hbs index 44b738dbf67..5c79af60205 100644 --- a/src/main/resources/java/src/main/java/packagename/exceptions/BaseException.hbs +++ b/src/main/resources/java/src/main/java/packagename/exceptions/BaseException.hbs @@ -1,4 +1,5 @@ package {{{packageName}}}.exceptions; +@SuppressWarnings("serial") public class BaseException extends RuntimeException { } \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/exceptions/InvalidAdditionalPropertyException.hbs b/src/main/resources/java/src/main/java/packagename/exceptions/InvalidAdditionalPropertyException.hbs index 8a17f15c6f4..b7948c64791 100644 --- a/src/main/resources/java/src/main/java/packagename/exceptions/InvalidAdditionalPropertyException.hbs +++ b/src/main/resources/java/src/main/java/packagename/exceptions/InvalidAdditionalPropertyException.hbs @@ -1,5 +1,6 @@ package {{{packageName}}}.exceptions; +@SuppressWarnings("serial") public class InvalidAdditionalPropertyException extends BaseException { public InvalidAdditionalPropertyException(String s) { super(); diff --git a/src/main/resources/java/src/main/java/packagename/exceptions/InvalidTypeException.hbs b/src/main/resources/java/src/main/java/packagename/exceptions/InvalidTypeException.hbs index 585132ddcc1..e5e1455ffc0 100644 --- a/src/main/resources/java/src/main/java/packagename/exceptions/InvalidTypeException.hbs +++ b/src/main/resources/java/src/main/java/packagename/exceptions/InvalidTypeException.hbs @@ -1,5 +1,6 @@ package {{{packageName}}}.exceptions; +@SuppressWarnings("serial") public class InvalidTypeException extends BaseException { public InvalidTypeException(String s) { super(); diff --git a/src/main/resources/java/src/main/java/packagename/exceptions/UnsetPropertyException.hbs b/src/main/resources/java/src/main/java/packagename/exceptions/UnsetPropertyException.hbs index 63a5699df08..fd37bec8d71 100644 --- a/src/main/resources/java/src/main/java/packagename/exceptions/UnsetPropertyException.hbs +++ b/src/main/resources/java/src/main/java/packagename/exceptions/UnsetPropertyException.hbs @@ -1,5 +1,6 @@ package {{{packageName}}}.exceptions; +@SuppressWarnings("serial") public class UnsetPropertyException extends BaseException { public UnsetPropertyException(String s) { super(); diff --git a/src/main/resources/java/src/main/java/packagename/exceptions/ValidationException.hbs b/src/main/resources/java/src/main/java/packagename/exceptions/ValidationException.hbs index 2b7bacfb80a..7a0e0a43af2 100644 --- a/src/main/resources/java/src/main/java/packagename/exceptions/ValidationException.hbs +++ b/src/main/resources/java/src/main/java/packagename/exceptions/ValidationException.hbs @@ -1,5 +1,6 @@ package {{{packageName}}}.exceptions; +@SuppressWarnings("serial") public class ValidationException extends BaseException { public ValidationException(String s) { super(); diff --git a/src/main/resources/java/src/main/java/packagename/schemas/AnyTypeJsonSchema.hbs b/src/main/resources/java/src/main/java/packagename/schemas/AnyTypeJsonSchema.hbs index 4b1006a2d4a..bb83e0a9f44 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/AnyTypeJsonSchema.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/AnyTypeJsonSchema.hbs @@ -16,6 +16,7 @@ import {{{packageName}}}.schemas.validation.StringSchemaValidator; import {{{packageName}}}.schemas.validation.ListSchemaValidator; import {{{packageName}}}.schemas.validation.MapSchemaValidator; import {{{packageName}}}.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.time.LocalDate; import java.time.ZonedDateTime; @@ -29,8 +30,8 @@ import java.util.Map; import java.util.Objects; import java.util.UUID; -public class AnyTypeJsonSchema extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static AnyTypeJsonSchema instance; +public class AnyTypeJsonSchema extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + private static @Nullable AnyTypeJsonSchema instance = null; protected AnyTypeJsonSchema() { super(new JsonSchemaInfo()); @@ -124,14 +125,18 @@ public class AnyTypeJsonSchema extends JsonSchema implements NullSchemaValidator } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); items.add(castItem); i += 1; } @@ -139,7 +144,7 @@ public class AnyTypeJsonSchema extends JsonSchema implements NullSchemaValidator } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -152,22 +157,30 @@ public class AnyTypeJsonSchema extends JsonSchema implements NullSchemaValidator } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); properties.put(propertyName, castValue); } return new FrozenMap<>(properties); } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -180,7 +193,7 @@ public class AnyTypeJsonSchema extends JsonSchema implements NullSchemaValidator } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -195,6 +208,6 @@ public class AnyTypeJsonSchema extends JsonSchema implements NullSchemaValidator } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/schemas/BooleanJsonSchema.hbs b/src/main/resources/java/src/main/java/packagename/schemas/BooleanJsonSchema.hbs index abe93b769bf..44d1995e865 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/BooleanJsonSchema.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/BooleanJsonSchema.hbs @@ -11,6 +11,7 @@ import {{{packageName}}}.schemas.validation.PathToSchemasMap; import {{{packageName}}}.schemas.validation.BooleanSchemaValidator; import {{{packageName}}}.schemas.validation.TypeValidator; import {{{packageName}}}.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.LinkedHashMap; @@ -21,7 +22,7 @@ import java.util.Objects; import java.util.Set; public class BooleanJsonSchema extends JsonSchema implements BooleanSchemaValidator { - private static BooleanJsonSchema instance; + private static @Nullable BooleanJsonSchema instance = null; protected BooleanJsonSchema() { super(new JsonSchemaInfo() @@ -53,11 +54,11 @@ public class BooleanJsonSchema extends JsonSchema implements BooleanSchemaValida } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Boolean) { boolean boolArg = (Boolean) arg; return getNewInstance(boolArg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/schemas/DateJsonSchema.hbs b/src/main/resources/java/src/main/java/packagename/schemas/DateJsonSchema.hbs index 22f9b5910f3..eccfd1ff3b4 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/DateJsonSchema.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/DateJsonSchema.hbs @@ -5,13 +5,11 @@ import {{{packageName}}}.configurations.SchemaConfiguration; import {{{packageName}}}.exceptions.InvalidTypeException; import {{{packageName}}}.schemas.validation.JsonSchema; import {{{packageName}}}.schemas.validation.JsonSchemaInfo; -import {{{packageName}}}.schemas.validation.KeywordEntry; import {{{packageName}}}.schemas.validation.PathToSchemasMap; import {{{packageName}}}.schemas.validation.StringSchemaValidator; -import {{{packageName}}}.schemas.validation.TypeValidator; -import {{{packageName}}}.schemas.validation.FormatValidator; import {{{packageName}}}.exceptions.ValidationException; import {{{packageName}}}.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.time.LocalDate; import java.util.HashSet; @@ -23,7 +21,7 @@ import java.util.Objects; import java.util.Set; public class DateJsonSchema extends JsonSchema implements StringSchemaValidator { - private static DateJsonSchema instance; + private static @Nullable DateJsonSchema instance = null; protected DateJsonSchema() { super(new JsonSchemaInfo() @@ -60,10 +58,10 @@ public class DateJsonSchema extends JsonSchema implements StringSchemaValidator } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof String) { return getNewInstance((String) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } diff --git a/src/main/resources/java/src/main/java/packagename/schemas/DateTimeJsonSchema.hbs b/src/main/resources/java/src/main/java/packagename/schemas/DateTimeJsonSchema.hbs index f28d2851f70..5aba00371d8 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/DateTimeJsonSchema.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/DateTimeJsonSchema.hbs @@ -12,6 +12,7 @@ import {{{packageName}}}.schemas.validation.TypeValidator; import {{{packageName}}}.schemas.validation.FormatValidator; import {{{packageName}}}.exceptions.ValidationException; import {{{packageName}}}.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.time.ZonedDateTime; import java.util.HashSet; @@ -23,7 +24,7 @@ import java.util.Objects; import java.util.Set; public class DateTimeJsonSchema extends JsonSchema implements StringSchemaValidator { - private static DateTimeJsonSchema instance; + private static @Nullable DateTimeJsonSchema instance = null; protected DateTimeJsonSchema() { super(new JsonSchemaInfo() @@ -60,10 +61,10 @@ public class DateTimeJsonSchema extends JsonSchema implements StringSchemaValida } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof String) { return getNewInstance((String) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/schemas/DecimalJsonSchema.hbs b/src/main/resources/java/src/main/java/packagename/schemas/DecimalJsonSchema.hbs index 607327f7cfe..cca823bb998 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/DecimalJsonSchema.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/DecimalJsonSchema.hbs @@ -12,6 +12,7 @@ import {{{packageName}}}.schemas.validation.StringSchemaValidator; import {{{packageName}}}.schemas.validation.TypeValidator; import {{{packageName}}}.schemas.validation.FormatValidator; import {{{packageName}}}.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.LinkedHashMap; @@ -22,7 +23,7 @@ import java.util.Objects; import java.util.Set; public class DecimalJsonSchema extends JsonSchema implements StringSchemaValidator { - private static DecimalJsonSchema instance; + private static @Nullable DecimalJsonSchema instance = null; protected DecimalJsonSchema() { super(new JsonSchemaInfo() @@ -55,10 +56,10 @@ public class DecimalJsonSchema extends JsonSchema implements StringSchemaValidat } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof String) { return getNewInstance((String) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/schemas/DoubleJsonSchema.hbs b/src/main/resources/java/src/main/java/packagename/schemas/DoubleJsonSchema.hbs index 7fd14b0883d..4d873a97bc6 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/DoubleJsonSchema.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/DoubleJsonSchema.hbs @@ -12,6 +12,7 @@ import {{{packageName}}}.schemas.validation.NumberSchemaValidator; import {{{packageName}}}.schemas.validation.TypeValidator; import {{{packageName}}}.schemas.validation.FormatValidator; import {{{packageName}}}.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.LinkedHashMap; @@ -22,7 +23,7 @@ import java.util.Objects; import java.util.Set; public class DoubleJsonSchema extends JsonSchema implements NumberSchemaValidator { - private static DoubleJsonSchema instance; + private static @Nullable DoubleJsonSchema instance = null; protected DoubleJsonSchema() { super(new JsonSchemaInfo() @@ -59,10 +60,10 @@ public class DoubleJsonSchema extends JsonSchema implements NumberSchemaValidato } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Number) { return getNewInstance((Number) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/schemas/FloatJsonSchema.hbs b/src/main/resources/java/src/main/java/packagename/schemas/FloatJsonSchema.hbs index 2f8f9435958..6f3109500dc 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/FloatJsonSchema.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/FloatJsonSchema.hbs @@ -12,6 +12,7 @@ import {{{packageName}}}.schemas.validation.NumberSchemaValidator; import {{{packageName}}}.schemas.validation.TypeValidator; import {{{packageName}}}.schemas.validation.FormatValidator; import {{{packageName}}}.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.LinkedHashMap; @@ -22,7 +23,7 @@ import java.util.Objects; import java.util.Set; public class FloatJsonSchema extends JsonSchema implements NumberSchemaValidator { - private static FloatJsonSchema instance; + private static @Nullable FloatJsonSchema instance = null; protected FloatJsonSchema() { super(new JsonSchemaInfo() @@ -59,10 +60,10 @@ public class FloatJsonSchema extends JsonSchema implements NumberSchemaValidator } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Number) { return getNewInstance((Number) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } diff --git a/src/main/resources/java/src/main/java/packagename/schemas/Int32JsonSchema.hbs b/src/main/resources/java/src/main/java/packagename/schemas/Int32JsonSchema.hbs index d77737e3c60..7b8fce601f0 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/Int32JsonSchema.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/Int32JsonSchema.hbs @@ -12,6 +12,7 @@ import {{{packageName}}}.schemas.validation.NumberSchemaValidator; import {{{packageName}}}.schemas.validation.TypeValidator; import {{{packageName}}}.exceptions.ValidationException; import {{{packageName}}}.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.LinkedHashMap; @@ -22,7 +23,7 @@ import java.util.Set; import java.util.Map; public class Int32JsonSchema extends JsonSchema implements NumberSchemaValidator { - private static Int32JsonSchema instance; + private static @Nullable Int32JsonSchema instance = null; protected Int32JsonSchema() { super(new JsonSchemaInfo() @@ -66,10 +67,10 @@ public class Int32JsonSchema extends JsonSchema implements NumberSchemaValidator } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Number) { return getNewInstance((Number) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/schemas/Int64JsonSchema.hbs b/src/main/resources/java/src/main/java/packagename/schemas/Int64JsonSchema.hbs index b47428525f5..516755a0bfc 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/Int64JsonSchema.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/Int64JsonSchema.hbs @@ -12,6 +12,7 @@ import {{{packageName}}}.schemas.validation.NumberSchemaValidator; import {{{packageName}}}.schemas.validation.TypeValidator; import {{{packageName}}}.exceptions.ValidationException; import {{{packageName}}}.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.LinkedHashMap; @@ -22,7 +23,7 @@ import java.util.Set; import java.util.Map; public class Int64JsonSchema extends JsonSchema implements NumberSchemaValidator { - private static Int64JsonSchema instance; + private static @Nullable Int64JsonSchema instance = null; protected Int64JsonSchema() { super(new JsonSchemaInfo() @@ -76,10 +77,10 @@ public class Int64JsonSchema extends JsonSchema implements NumberSchemaValidator } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Number) { return getNewInstance((Number) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/schemas/IntJsonSchema.hbs b/src/main/resources/java/src/main/java/packagename/schemas/IntJsonSchema.hbs index c250c0a48a6..6579e8661a9 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/IntJsonSchema.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/IntJsonSchema.hbs @@ -12,6 +12,7 @@ import {{{packageName}}}.schemas.validation.NumberSchemaValidator; import {{{packageName}}}.schemas.validation.TypeValidator; import {{{packageName}}}.exceptions.ValidationException; import {{{packageName}}}.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.LinkedHashMap; @@ -22,7 +23,7 @@ import java.util.Set; import java.util.Map; public class IntJsonSchema extends JsonSchema implements NumberSchemaValidator { - private static IntJsonSchema instance; + private static @Nullable IntJsonSchema instance = null; protected IntJsonSchema() { super(new JsonSchemaInfo() @@ -76,10 +77,10 @@ public class IntJsonSchema extends JsonSchema implements NumberSchemaValidator { } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Number) { return getNewInstance((Number) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/schemas/ListJsonSchema.hbs b/src/main/resources/java/src/main/java/packagename/schemas/ListJsonSchema.hbs index c8d5b45c8a4..f63f92dfa76 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/ListJsonSchema.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/ListJsonSchema.hbs @@ -10,16 +10,18 @@ import {{{packageName}}}.schemas.validation.PathToSchemasMap; import {{{packageName}}}.schemas.validation.ListSchemaValidator; import {{{packageName}}}.exceptions.ValidationException; import {{{packageName}}}.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.ArrayList; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Objects; import java.util.Set; -public class ListJsonSchema extends JsonSchema implements ListSchemaValidator> { - private static ListJsonSchema instance; +public class ListJsonSchema extends JsonSchema implements ListSchemaValidator> { + private static @Nullable ListJsonSchema instance = null; protected ListJsonSchema() { super(new JsonSchemaInfo() @@ -35,14 +37,18 @@ public class ListJsonSchema extends JsonSchema implements ListSchemaValidator getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); items.add(castItem); i += 1; } @@ -50,7 +56,7 @@ public class ListJsonSchema extends JsonSchema implements ListSchemaValidator validate(List arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -63,10 +69,10 @@ public class ListJsonSchema extends JsonSchema implements ListSchemaValidator pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof List) { return getNewInstance((List) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } diff --git a/src/main/resources/java/src/main/java/packagename/schemas/MapJsonSchema.hbs b/src/main/resources/java/src/main/java/packagename/schemas/MapJsonSchema.hbs index 2927ad674dd..a225b22c810 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/MapJsonSchema.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/MapJsonSchema.hbs @@ -10,6 +10,7 @@ import {{{packageName}}}.schemas.validation.PathToSchemasMap; import {{{packageName}}}.schemas.validation.MapSchemaValidator; import {{{packageName}}}.exceptions.ValidationException; import {{{packageName}}}.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.ArrayList; import java.util.HashSet; @@ -20,8 +21,8 @@ import java.util.Map; import java.util.Objects; import java.util.Set; -public class MapJsonSchema extends JsonSchema implements MapSchemaValidator> { - private static MapJsonSchema instance; +public class MapJsonSchema extends JsonSchema implements MapSchemaValidator> { + private static @Nullable MapJsonSchema instance = null; protected MapJsonSchema() { super(new JsonSchemaInfo() @@ -37,22 +38,30 @@ public class MapJsonSchema extends JsonSchema implements MapSchemaValidator getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); properties.put(propertyName, castValue); } return new FrozenMap<>(properties); } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -63,10 +72,11 @@ public class MapJsonSchema extends JsonSchema implements MapSchemaValidator pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof FrozenMap) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } + diff --git a/src/main/resources/java/src/main/java/packagename/schemas/NotAnyTypeJsonSchema.hbs b/src/main/resources/java/src/main/java/packagename/schemas/NotAnyTypeJsonSchema.hbs index 114b7fbb9bb..a9c40f211bf 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/NotAnyTypeJsonSchema.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/NotAnyTypeJsonSchema.hbs @@ -16,6 +16,7 @@ import {{{packageName}}}.schemas.validation.StringSchemaValidator; import {{{packageName}}}.schemas.validation.ListSchemaValidator; import {{{packageName}}}.schemas.validation.MapSchemaValidator; import {{{packageName}}}.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.time.LocalDate; import java.time.ZonedDateTime; @@ -29,8 +30,8 @@ import java.util.Map; import java.util.Objects; import java.util.UUID; -public class NotAnyTypeJsonSchema extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static NotAnyTypeJsonSchema instance; +public class NotAnyTypeJsonSchema extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + private static @Nullable NotAnyTypeJsonSchema instance = null; protected NotAnyTypeJsonSchema() { super(new JsonSchemaInfo() @@ -126,14 +127,18 @@ public class NotAnyTypeJsonSchema extends JsonSchema implements NullSchemaValida } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); items.add(castItem); i += 1; } @@ -141,7 +146,7 @@ public class NotAnyTypeJsonSchema extends JsonSchema implements NullSchemaValida } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -154,22 +159,30 @@ public class NotAnyTypeJsonSchema extends JsonSchema implements NullSchemaValida } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); properties.put(propertyName, castValue); } return new FrozenMap<>(properties); } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -182,7 +195,7 @@ public class NotAnyTypeJsonSchema extends JsonSchema implements NullSchemaValida } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -197,6 +210,6 @@ public class NotAnyTypeJsonSchema extends JsonSchema implements NullSchemaValida } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/schemas/NullJsonSchema.hbs b/src/main/resources/java/src/main/java/packagename/schemas/NullJsonSchema.hbs index ab33635a16d..3d30e412600 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/NullJsonSchema.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/NullJsonSchema.hbs @@ -11,6 +11,7 @@ import {{{packageName}}}.schemas.validation.PathToSchemasMap; import {{{packageName}}}.schemas.validation.NullSchemaValidator; import {{{packageName}}}.schemas.validation.TypeValidator; import {{{packageName}}}.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.LinkedHashMap; @@ -21,7 +22,7 @@ import java.util.Objects; import java.util.Set; public class NullJsonSchema extends JsonSchema implements NullSchemaValidator { - private static NullJsonSchema instance; + private static @Nullable NullJsonSchema instance = null; protected NullJsonSchema() { super(new JsonSchemaInfo() @@ -53,11 +54,11 @@ public class NullJsonSchema extends JsonSchema implements NullSchemaValidator { } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } diff --git a/src/main/resources/java/src/main/java/packagename/schemas/NumberJsonSchema.hbs b/src/main/resources/java/src/main/java/packagename/schemas/NumberJsonSchema.hbs index 09b5883bf89..ac63343aacd 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/NumberJsonSchema.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/NumberJsonSchema.hbs @@ -11,6 +11,7 @@ import {{{packageName}}}.schemas.validation.NumberSchemaValidator; import {{{packageName}}}.schemas.validation.TypeValidator; import {{{packageName}}}.exceptions.ValidationException; import {{{packageName}}}.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.LinkedHashMap; @@ -21,7 +22,7 @@ import java.util.Set; import java.util.Map; public class NumberJsonSchema extends JsonSchema implements NumberSchemaValidator { - private static NumberJsonSchema instance; + private static @Nullable NumberJsonSchema instance = null; protected NumberJsonSchema() { super(new JsonSchemaInfo() @@ -74,10 +75,10 @@ public class NumberJsonSchema extends JsonSchema implements NumberSchemaValidato } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Number) { return getNewInstance((Number) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/schemas/StringJsonSchema.hbs b/src/main/resources/java/src/main/java/packagename/schemas/StringJsonSchema.hbs index f2ef9176b77..739f443b5ba 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/StringJsonSchema.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/StringJsonSchema.hbs @@ -11,6 +11,7 @@ import {{{packageName}}}.schemas.validation.StringSchemaValidator; import {{{packageName}}}.schemas.validation.TypeValidator; import {{{packageName}}}.exceptions.ValidationException; import {{{packageName}}}.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.time.LocalDate; import java.time.ZonedDateTime; @@ -24,7 +25,7 @@ import java.util.Map; import java.util.UUID; public class StringJsonSchema extends JsonSchema implements StringSchemaValidator { - private static StringJsonSchema instance; + private static @Nullable StringJsonSchema instance = null; protected StringJsonSchema() { super(new JsonSchemaInfo() @@ -68,10 +69,10 @@ public class StringJsonSchema extends JsonSchema implements StringSchemaValidato } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof String) { return getNewInstance((String) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/schemas/UuidJsonSchema.hbs b/src/main/resources/java/src/main/java/packagename/schemas/UuidJsonSchema.hbs index 462b953cb06..1f8ce915139 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/UuidJsonSchema.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/UuidJsonSchema.hbs @@ -12,6 +12,7 @@ import {{{packageName}}}.schemas.validation.TypeValidator; import {{{packageName}}}.schemas.validation.FormatValidator; import {{{packageName}}}.exceptions.ValidationException; import {{{packageName}}}.schemas.validation.ValidationMetadata; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.LinkedHashMap; @@ -23,7 +24,7 @@ import java.util.Set; import java.util.UUID; public class UuidJsonSchema extends JsonSchema implements StringSchemaValidator { - private static UuidJsonSchema instance; + private static @Nullable UuidJsonSchema instance = null; protected UuidJsonSchema() { super(new JsonSchemaInfo() @@ -60,10 +61,10 @@ public class UuidJsonSchema extends JsonSchema implements StringSchemaValidator } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof String) { return getNewInstance((String) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/AdditionalPropertiesValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/AdditionalPropertiesValidator.hbs index 683c58a2883..fb251d07c73 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/AdditionalPropertiesValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/AdditionalPropertiesValidator.hbs @@ -1,7 +1,7 @@ package {{{packageName}}}.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.ArrayList; -import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -15,24 +15,23 @@ public class AdditionalPropertiesValidator implements KeywordValidator { } @Override - public Object getConstraint() { - return additionalProperties; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { - if (!(arg instanceof Map)) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { + if (!(arg instanceof Map mapArg)) { return null; } - Map castArg = (Map) arg; - Set presentAdditionalProperties = new LinkedHashSet<>(castArg.keySet()); + Set presentAdditionalProperties = new LinkedHashSet<>(); + for (Object key: mapArg.keySet()) { + if (key instanceof String) { + presentAdditionalProperties.add((String) key); + } + } if (schema.properties != null) { presentAdditionalProperties.removeAll(schema.properties.keySet()); } PathToSchemasMap pathToSchemas = new PathToSchemasMap(); // todo add handling for validatedPatternProperties for(String addPropName: presentAdditionalProperties) { - Object propValue = castArg.get(addPropName); + @Nullable Object propValue = mapArg.get(addPropName); List propPathToItem = new ArrayList<>(validationMetadata.pathToItem()); propPathToItem.add(addPropName); ValidationMetadata propValidationMetadata = new ValidationMetadata( diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/AllOfValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/AllOfValidator.hbs index 0044ac84705..1265988112d 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/AllOfValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/AllOfValidator.hbs @@ -1,4 +1,5 @@ package {{{packageName}}}.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.List; @@ -10,12 +11,7 @@ public class AllOfValidator implements KeywordValidator { } @Override - public Object getConstraint() { - return allOf; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { PathToSchemasMap pathToSchemas = new PathToSchemasMap(); for(Class allOfClass: allOf) { JsonSchema allOfSchema = JsonSchemaFactory.getInstance(allOfClass); diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/AnyOfValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/AnyOfValidator.hbs index e063abe37cc..34cfb02095e 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/AnyOfValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/AnyOfValidator.hbs @@ -1,6 +1,7 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.ArrayList; import java.util.List; @@ -13,12 +14,7 @@ public class AnyOfValidator implements KeywordValidator { } @Override - public Object getConstraint() { - return anyOf; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { PathToSchemasMap pathToSchemas = new PathToSchemasMap(); List> validatedAnyOfClasses = new ArrayList<>(); for(Class anyOfClass: anyOf) { diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/EnumValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/EnumValidator.hbs index 805b9bc1ebe..66f935c79ab 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/EnumValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/EnumValidator.hbs @@ -1,28 +1,29 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.math.BigDecimal; import java.util.Set; public class EnumValidator implements KeywordValidator { - public final Set enumValues; + public final Set<@Nullable Object> enumValues; - public EnumValidator(Set enumValues) { + public EnumValidator(Set<@Nullable Object> enumValues) { this.enumValues = enumValues; } - @Override - public Object getConstraint() { - return enumValues; + @SuppressWarnings("nullness") + private boolean enumContainsArg(@Nullable Object arg){ + return enumValues.contains(arg); } @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (enumValues.isEmpty()) { throw new ValidationException("No value can match enum because enum is empty"); } - if (enumValues.contains(arg)) { + if (enumContainsArg(arg)) { return null; } if (arg instanceof Float) { diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/ExclusiveMaximumValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/ExclusiveMaximumValidator.hbs index b98de4cbf39..3e22c7284a6 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/ExclusiveMaximumValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/ExclusiveMaximumValidator.hbs @@ -1,6 +1,7 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; public class ExclusiveMaximumValidator implements KeywordValidator { @@ -11,12 +12,7 @@ public class ExclusiveMaximumValidator implements KeywordValidator { } @Override - public Object getConstraint() { - return exclusiveMaximum; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof Number)) { return null; } diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/ExclusiveMinimumValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/ExclusiveMinimumValidator.hbs index 07421f931a0..aed9d726981 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/ExclusiveMinimumValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/ExclusiveMinimumValidator.hbs @@ -1,6 +1,7 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; public class ExclusiveMinimumValidator implements KeywordValidator { @@ -11,12 +12,7 @@ public class ExclusiveMinimumValidator implements KeywordValidator { } @Override - public Object getConstraint() { - return exclusiveMinimum; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof Number)) { return null; } diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/FakeValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/FakeValidator.hbs index 0ae586d3258..2418e8a3552 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/FakeValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/FakeValidator.hbs @@ -1,13 +1,10 @@ package {{{packageName}}}.schemas.validation; -public class FakeValidator implements KeywordValidator { - @Override - public Object getConstraint() { - return null; - } +import org.checkerframework.checker.nullness.qual.Nullable; +public class FakeValidator implements KeywordValidator { @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { return null; } } diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/FormatValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/FormatValidator.hbs index e8d2a36f299..315235efcc1 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/FormatValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/FormatValidator.hbs @@ -1,6 +1,7 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.math.BigDecimal; import java.math.BigInteger; @@ -14,11 +15,6 @@ public class FormatValidator implements KeywordValidator { this.format = format; } - @Override - public Object getConstraint() { - return format; - } - private final static BigInteger int32InclusiveMinimum = BigInteger.valueOf(-2147483648L); private final static BigInteger int32InclusiveMaximum = BigInteger.valueOf(2147483647L); private final static BigInteger int64InclusiveMinimum = BigInteger.valueOf(-9223372036854775808L); @@ -147,7 +143,7 @@ public class FormatValidator implements KeywordValidator { } @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (arg instanceof Number) { validateNumericFormat( (Number) arg, diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/FrozenMap.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/FrozenMap.hbs index 8d86dcc0801..38f76b622b7 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/FrozenMap.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/FrozenMap.hbs @@ -1,5 +1,6 @@ package {{{packageName}}}.schemas.validation; +import org.checkerframework.checker.nullness.qual.KeyFor; import {{{packageName}}}.exceptions.UnsetPropertyException; import {{{packageName}}}.exceptions.InvalidAdditionalPropertyException; @@ -7,6 +8,7 @@ import java.util.AbstractMap; import java.util.HashMap; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; public class FrozenMap extends AbstractMap { /* @@ -27,15 +29,17 @@ public class FrozenMap extends AbstractMap { } } - protected void throwIfKeyKnown(String key, Set requiredKeys, Set optionalKeys) throws IllegalArgumentException { + protected void throwIfKeyKnown(String key, Set requiredKeys, Set optionalKeys) throws InvalidAdditionalPropertyException { if (requiredKeys.contains(key) || optionalKeys.contains(key)) { throw new InvalidAdditionalPropertyException ("The known key " + key + " may not be passed in when getting an additional property"); } } @Override - public Set> entrySet() { - return map.entrySet(); + public Set> entrySet() { + return map.entrySet().stream() + .map(x -> new AbstractMap.SimpleEntry<>(x.getKey(), x.getValue())) + .collect(Collectors.toSet()); } } diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/ItemsValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/ItemsValidator.hbs index 938d194048a..8ca2f6b7834 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/ItemsValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/ItemsValidator.hbs @@ -1,5 +1,7 @@ package {{{packageName}}}.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; + import java.util.ArrayList; import java.util.List; @@ -11,20 +13,14 @@ public class ItemsValidator implements KeywordValidator { } @Override - public Object getConstraint() { - return items; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof List)) { return null; } - List castArg = (List) arg; PathToSchemasMap pathToSchemas = new PathToSchemasMap(); // todo add handling for prefixItems int i = 0; - for(Object itemValue: castArg) { + for(Object itemValue: (List) arg) { List itemPathToItem = new ArrayList<>(validationMetadata.pathToItem()); itemPathToItem.add(i); ValidationMetadata itemValidationMetadata = new ValidationMetadata( diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchema.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchema.hbs index cfd25b75bab..d6c63cee579 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchema.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchema.hbs @@ -2,6 +2,7 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; import {{{packageName}}}.exceptions.InvalidTypeException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.math.BigDecimal; import java.time.LocalDate; @@ -16,30 +17,30 @@ import java.util.UUID; import java.util.regex.Pattern; public abstract class JsonSchema { - public final Set> type; - public final String format; - public final Class items; - public final Map> properties; - public final Set required; - public final Number exclusiveMaximum; - public final Number exclusiveMinimum; - public final Integer maxItems; - public final Integer minItems; - public final Integer maxLength; - public final Integer minLength; - public final Integer maxProperties; - public final Integer minProperties; - public final Number maximum; - public final Number minimum; - public final BigDecimal multipleOf; - public final Class additionalProperties; - public final List> allOf; - public final List> anyOf; - public final List> oneOf; - public final Class not; - public final Boolean uniqueItems; - public final Set enumValues; - public final Pattern pattern; + public final @Nullable Set> type; + public final @Nullable String format; + public final @Nullable Class items; + public final @Nullable Map> properties; + public final @Nullable Set required; + public final @Nullable Number exclusiveMaximum; + public final @Nullable Number exclusiveMinimum; + public final @Nullable Integer maxItems; + public final @Nullable Integer minItems; + public final @Nullable Integer maxLength; + public final @Nullable Integer minLength; + public final @Nullable Integer maxProperties; + public final @Nullable Integer minProperties; + public final @Nullable Number maximum; + public final @Nullable Number minimum; + public final @Nullable BigDecimal multipleOf; + public final @Nullable Class additionalProperties; + public final @Nullable List> allOf; + public final @Nullable List> anyOf; + public final @Nullable List> oneOf; + public final @Nullable Class not; + public final @Nullable Boolean uniqueItems; + public final @Nullable Set<@Nullable Object> enumValues; + public final @Nullable Pattern pattern; private final LinkedHashMap keywordToValidator; protected JsonSchema(JsonSchemaInfo jsonSchemaInfo) { @@ -215,40 +216,40 @@ public abstract class JsonSchema { this.keywordToValidator = keywordToValidator; } - public abstract Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas); + public abstract @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas); public static PathToSchemasMap validate( JsonSchema jsonSchema, - Object arg, + @Nullable Object arg, ValidationMetadata validationMetadata ) throws ValidationException { LinkedHashSet disabledKeywords = validationMetadata.configuration().disabledKeywordFlags().getKeywords(); PathToSchemasMap pathToSchemas = new PathToSchemasMap(); LinkedHashMap thisKeywordToValidator = jsonSchema.keywordToValidator; - if (thisKeywordToValidator != null) { - for (Map.Entry entry: thisKeywordToValidator.entrySet()) { - String jsonKeyword = entry.getKey(); - if (disabledKeywords.contains(jsonKeyword)) { - continue; - } - KeywordValidator validator = entry.getValue(); - PathToSchemasMap otherPathToSchemas = validator.validate( - jsonSchema, - arg, - validationMetadata - ); - if (otherPathToSchemas == null) { - continue; - } - pathToSchemas.update(otherPathToSchemas); + for (Map.Entry entry: thisKeywordToValidator.entrySet()) { + String jsonKeyword = entry.getKey(); + if (disabledKeywords.contains(jsonKeyword)) { + continue; } + KeywordValidator validator = entry.getValue(); + @Nullable PathToSchemasMap otherPathToSchemas = validator.validate( + jsonSchema, + arg, + validationMetadata + ); + if (otherPathToSchemas == null) { + continue; + } + pathToSchemas.update(otherPathToSchemas); } List pathToItem = validationMetadata.pathToItem(); if (!pathToSchemas.containsKey(pathToItem)) { pathToSchemas.put(validationMetadata.pathToItem(), new LinkedHashMap<>()); } - pathToSchemas.get(pathToItem).put(jsonSchema, null); - + @Nullable LinkedHashMap schemas = pathToSchemas.get(pathToItem); + if (schemas != null) { + schemas.put(jsonSchema, null); + } return pathToSchemas; } @@ -274,7 +275,7 @@ public abstract class JsonSchema { protected List castToAllowedTypes(List arg, List pathToItem, Set> pathSet) { pathSet.add(pathToItem); - List argFixed = new ArrayList<>(); + List<@Nullable Object> argFixed = new ArrayList<>(); int i =0; for (Object item: arg) { List newPathToItem = new ArrayList<>(pathToItem); @@ -288,10 +289,14 @@ public abstract class JsonSchema { protected Map castToAllowedTypes(Map arg, List pathToItem, Set> pathSet) { pathSet.add(pathToItem); - LinkedHashMap argFixed = new LinkedHashMap<>(); + LinkedHashMap argFixed = new LinkedHashMap<>(); for (Map.Entry entry: arg.entrySet()) { - String key = (String) entry.getKey(); - Object val = arg.get(entry.getKey()); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String key = (String) entryKey; + Object val = entry.getValue(); List newPathToItem = new ArrayList<>(pathToItem); newPathToItem.add(key); Object fixedVal = castToAllowedObjectTypes(val, newPathToItem, pathSet); @@ -300,7 +305,7 @@ public abstract class JsonSchema { return argFixed; } - protected Object castToAllowedObjectTypes(Object arg, List pathToItem, Set> pathSet) { + private @Nullable Object castToAllowedObjectTypes(@Nullable Object arg, List pathToItem, Set> pathSet) { if (arg == null) { return castToAllowedTypes((Void) null, pathToItem, pathSet); } else if (arg instanceof String) { @@ -348,7 +353,7 @@ public abstract class JsonSchema { return arg; } - protected static PathToSchemasMap getPathToSchemas(JsonSchema jsonSchema, Object arg, ValidationMetadata validationMetadata, Set> pathSet) { + protected static PathToSchemasMap getPathToSchemas(JsonSchema jsonSchema, @Nullable Object arg, ValidationMetadata validationMetadata, Set> pathSet) { PathToSchemasMap pathToSchemasMap = new PathToSchemasMap(); // todo add check of validationMetadata.validationRanEarlier(this) PathToSchemasMap otherPathToSchemas = validate(jsonSchema, arg, validationMetadata); @@ -369,35 +374,12 @@ public abstract class JsonSchema { return pathToSchemasMap; } - /* - protected FrozenMap getProperties(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); - for(Map.Entry entry: arg.entrySet()) { - String propertyName = entry.getKey(); - List propertyPathToItem = new ArrayList<>(pathToItem); - propertyPathToItem.add(propertyName); - MapValueCastType value = entry.getValue(); - JsonSchema propertySchema = (JsonSchema) pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - MapValueOutType castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); - } - return new FrozenMap<>(properties); - } - - private FrozenList getItems(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - ArrayList items = new ArrayList<>(); - int i = 0; - for (ListItemCastType item: arg) { - List itemPathToItem = new ArrayList<>(pathToItem); - itemPathToItem.add(i); - JsonSchema itemSchema = (JsonSchema) pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - ListItemOutType castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); - i += 1; - } - return new FrozenList<>(items); - } - */ - - // todo add bytes and FileIO + public static String getClass(@Nullable Object arg) { + if (arg == null) { + return Void.class.getSimpleName(); + } else { + return arg.getClass().getSimpleName(); + } + } + // todo add bytes and FileIO } \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchemaFactory.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchemaFactory.hbs index 0402a8408a5..2ea8c56b4fe 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchemaFactory.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchemaFactory.hbs @@ -1,5 +1,8 @@ package {{{packageName}}}.schemas.validation; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; @@ -10,12 +13,16 @@ public class JsonSchemaFactory { static Map, JsonSchema> classToInstance = new HashMap<>(); public static V getInstance(Class schemaCls) { - if (classToInstance.containsKey(schemaCls)) { - return (V) classToInstance.get(schemaCls); + @Nullable JsonSchema cacheInst = classToInstance.get(schemaCls); + if (cacheInst != null) { + assert schemaCls.isInstance(cacheInst); + return schemaCls.cast(cacheInst); } try { Method method = schemaCls.getMethod("getInstance"); - V inst = (V) method.invoke(null); + @SuppressWarnings("nullness") @NonNull Object obj = method.invoke(null); + assert schemaCls.isInstance(obj); + V inst = schemaCls.cast(obj); classToInstance.put(schemaCls, inst); return inst; } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchemaInfo.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchemaInfo.hbs index 673f607b31b..ccd26fb5bf3 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchemaInfo.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/JsonSchemaInfo.hbs @@ -1,4 +1,5 @@ package {{{packageName}}}.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import java.math.BigDecimal; import java.util.List; @@ -7,122 +8,122 @@ import java.util.Set; import java.util.regex.Pattern; public class JsonSchemaInfo { - public Set> type; + public @Nullable Set> type = null; public JsonSchemaInfo type(Set> type) { this.type = type; return this; } - public String format; + public @Nullable String format = null; public JsonSchemaInfo format(String format) { this.format = format; return this; } - public Class items; + public @Nullable Class items = null; public JsonSchemaInfo items(Class items) { this.items = items; return this; } - public Map> properties; + public @Nullable Map> properties = null; public JsonSchemaInfo properties(Map> properties) { this.properties = properties; return this; } - public Set required; + public @Nullable Set required = null; public JsonSchemaInfo required(Set required) { this.required = required; return this; } - public Number exclusiveMaximum; + public @Nullable Number exclusiveMaximum = null; public JsonSchemaInfo exclusiveMaximum(Number exclusiveMaximum) { this.exclusiveMaximum = exclusiveMaximum; return this; } - public Number exclusiveMinimum; + public @Nullable Number exclusiveMinimum = null; public JsonSchemaInfo exclusiveMinimum(Number exclusiveMinimum) { this.exclusiveMinimum = exclusiveMinimum; return this; } - public Integer maxItems; + public @Nullable Integer maxItems = null; public JsonSchemaInfo maxItems(Integer maxItems) { this.maxItems = maxItems; return this; } - public Integer minItems; + public @Nullable Integer minItems = null; public JsonSchemaInfo minItems(Integer minItems) { this.minItems = minItems; return this; } - public Integer maxLength; + public @Nullable Integer maxLength = null; public JsonSchemaInfo maxLength(Integer maxLength) { this.maxLength = maxLength; return this; } - public Integer minLength; + public @Nullable Integer minLength = null; public JsonSchemaInfo minLength(Integer minLength) { this.minLength = minLength; return this; } - public Integer maxProperties; + public @Nullable Integer maxProperties = null; public JsonSchemaInfo maxProperties(Integer maxProperties) { this.maxProperties = maxProperties; return this; } - public Integer minProperties; + public @Nullable Integer minProperties = null; public JsonSchemaInfo minProperties(Integer minProperties) { this.minProperties = minProperties; return this; } - public Number maximum; + public @Nullable Number maximum = null; public JsonSchemaInfo maximum(Number maximum) { this.maximum = maximum; return this; } - public Number minimum; + public @Nullable Number minimum = null; public JsonSchemaInfo minimum(Number minimum) { this.minimum = minimum; return this; } - public BigDecimal multipleOf; + public @Nullable BigDecimal multipleOf = null; public JsonSchemaInfo multipleOf(BigDecimal multipleOf) { this.multipleOf = multipleOf; return this; } - public Class additionalProperties; + public @Nullable Class additionalProperties; public JsonSchemaInfo additionalProperties(Class additionalProperties) { this.additionalProperties = additionalProperties; return this; } - public List> allOf; + public @Nullable List> allOf = null; public JsonSchemaInfo allOf(List> allOf) { this.allOf = allOf; return this; } - public List> anyOf; + public @Nullable List> anyOf = null; public JsonSchemaInfo anyOf(List> anyOf) { this.anyOf = anyOf; return this; } - public List> oneOf; + public @Nullable List> oneOf = null; public JsonSchemaInfo oneOf(List> oneOf) { this.oneOf = oneOf; return this; } - public Class not; + public @Nullable Class not = null; public JsonSchemaInfo not(Class not) { this.not = not; return this; } - public Boolean uniqueItems; + public @Nullable Boolean uniqueItems = null; public JsonSchemaInfo uniqueItems(Boolean uniqueItems) { this.uniqueItems = uniqueItems; return this; } - public Set enumValues; - public JsonSchemaInfo enumValues(Set enumValues) { + public @Nullable Set<@Nullable Object> enumValues = null; + public JsonSchemaInfo enumValues(Set<@Nullable Object> enumValues) { this.enumValues = enumValues; return this; } - public Pattern pattern; + public @Nullable Pattern pattern = null; public JsonSchemaInfo pattern(Pattern pattern) { this.pattern = pattern; return this; diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/KeywordEntry.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/KeywordEntry.hbs index 8002aaf682d..7f5a8c70c02 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/KeywordEntry.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/KeywordEntry.hbs @@ -2,6 +2,7 @@ package {{{packageName}}}.schemas.validation; import java.util.AbstractMap; +@SuppressWarnings("serial") public class KeywordEntry extends AbstractMap.SimpleEntry { public KeywordEntry(String key, KeywordValidator value) { super(key, value); diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/KeywordValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/KeywordValidator.hbs index c73259fa67f..4c7352bc3ab 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/KeywordValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/KeywordValidator.hbs @@ -1,12 +1,11 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; public interface KeywordValidator { - PathToSchemasMap validate( + @Nullable PathToSchemasMap validate( JsonSchema schema, - Object arg, + @Nullable Object arg, ValidationMetadata validationMetadata) throws ValidationException; - - Object getConstraint(); } \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/MaxItemsValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/MaxItemsValidator.hbs index 04c6beeee98..ed08a4e64f7 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/MaxItemsValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/MaxItemsValidator.hbs @@ -1,6 +1,7 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.List; @@ -12,12 +13,7 @@ public class MaxItemsValidator implements KeywordValidator { } @Override - public Object getConstraint() { - return maxItems; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof List)) { return null; } diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/MaxLengthValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/MaxLengthValidator.hbs index 0a73606ef6b..22d2ad9dc0e 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/MaxLengthValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/MaxLengthValidator.hbs @@ -1,6 +1,7 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; public class MaxLengthValidator extends LengthValidator implements KeywordValidator { public final int maxLength; @@ -10,12 +11,7 @@ public class MaxLengthValidator extends LengthValidator implements KeywordValida } @Override - public Object getConstraint() { - return maxLength; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof String)) { return null; } diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/MaxPropertiesValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/MaxPropertiesValidator.hbs index 520f1cd8b7d..2ada73c7893 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/MaxPropertiesValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/MaxPropertiesValidator.hbs @@ -1,6 +1,7 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.Map; @@ -12,12 +13,7 @@ public class MaxPropertiesValidator implements KeywordValidator { } @Override - public Object getConstraint() { - return maxProperties; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof Map)) { return null; } diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/MaximumValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/MaximumValidator.hbs index 0359aa1a5c6..7730c282c3e 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/MaximumValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/MaximumValidator.hbs @@ -1,6 +1,7 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; public class MaximumValidator implements KeywordValidator { @@ -11,12 +12,7 @@ public class MaximumValidator implements KeywordValidator { } @Override - public Object getConstraint() { - return maximum; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof Number)) { return null; } diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/MinItemsValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/MinItemsValidator.hbs index 370ae58c1e0..90e247bd943 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/MinItemsValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/MinItemsValidator.hbs @@ -1,6 +1,7 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.List; @@ -12,12 +13,7 @@ public class MinItemsValidator implements KeywordValidator { } @Override - public Object getConstraint() { - return minItems; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof List)) { return null; } diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/MinLengthValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/MinLengthValidator.hbs index d93b0b741e5..8197813a52a 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/MinLengthValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/MinLengthValidator.hbs @@ -1,6 +1,7 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; public class MinLengthValidator extends LengthValidator implements KeywordValidator { public final int minLength; @@ -10,12 +11,7 @@ public class MinLengthValidator extends LengthValidator implements KeywordValida } @Override - public Object getConstraint() { - return minLength; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof String)) { return null; } diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/MinPropertiesValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/MinPropertiesValidator.hbs index 1f7e6ae7521..f8d63210f8d 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/MinPropertiesValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/MinPropertiesValidator.hbs @@ -1,6 +1,7 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.Map; @@ -12,12 +13,7 @@ public class MinPropertiesValidator implements KeywordValidator { } @Override - public Object getConstraint() { - return minProperties; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof Map)) { return null; } diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/MinimumValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/MinimumValidator.hbs index 23acd8c7913..0153a3b9cff 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/MinimumValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/MinimumValidator.hbs @@ -1,6 +1,7 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; public class MinimumValidator implements KeywordValidator { @@ -11,12 +12,7 @@ public class MinimumValidator implements KeywordValidator { } @Override - public Object getConstraint() { - return minimum; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof Number)) { return null; } diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/MultipleOfValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/MultipleOfValidator.hbs index 046d4079b99..9142d9ebc34 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/MultipleOfValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/MultipleOfValidator.hbs @@ -1,6 +1,7 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.math.BigDecimal; @@ -11,11 +12,6 @@ public class MultipleOfValidator implements KeywordValidator { this.multipleOf = multipleOf; } - @Override - public Object getConstraint() { - return multipleOf; - } - private BigDecimal getBigDecimal(Number arg) { if (arg instanceof Integer) { return new BigDecimal((Integer) arg); @@ -31,7 +27,7 @@ public class MultipleOfValidator implements KeywordValidator { } @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof Number)) { return null; } diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/NotValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/NotValidator.hbs index b8252d4ef1b..5eb143982a7 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/NotValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/NotValidator.hbs @@ -1,6 +1,7 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -17,12 +18,7 @@ public class NotValidator implements KeywordValidator { } @Override - public Object getConstraint() { - return not; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { PathToSchemasMap pathToSchemas; try { JsonSchema notSchema = JsonSchemaFactory.getInstance(not); diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/OneOfValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/OneOfValidator.hbs index 0850b86dc08..f2d71311cb9 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/OneOfValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/OneOfValidator.hbs @@ -1,6 +1,7 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.ArrayList; import java.util.List; @@ -13,12 +14,7 @@ public class OneOfValidator implements KeywordValidator { } @Override - public Object getConstraint() { - return oneOf; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { PathToSchemasMap pathToSchemas = new PathToSchemasMap(); List> validatedOneOfClasses = new ArrayList<>(); for(Class oneOfClass: oneOf) { diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/PathToSchemasMap.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/PathToSchemasMap.hbs index b9c218e13ac..83ddc2233e4 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/PathToSchemasMap.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/PathToSchemasMap.hbs @@ -4,6 +4,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +@SuppressWarnings("serial") public class PathToSchemasMap extends LinkedHashMap, LinkedHashMap> { public void update(PathToSchemasMap other) { diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/PatternValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/PatternValidator.hbs index 0e537dd27fd..6f4bd5053c1 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/PatternValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/PatternValidator.hbs @@ -1,6 +1,7 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.regex.Pattern; @@ -12,12 +13,7 @@ public class PatternValidator implements KeywordValidator { } @Override - public Object getConstraint() { - return pattern; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof String)) { return null; } diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/PropertiesValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/PropertiesValidator.hbs index 812ff962376..065c3c30eb1 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/PropertiesValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/PropertiesValidator.hbs @@ -1,5 +1,7 @@ package {{{packageName}}}.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; + import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; @@ -14,21 +16,23 @@ public class PropertiesValidator implements KeywordValidator { } @Override - public Object getConstraint() { - return properties; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { - if (!(arg instanceof Map)) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { + if (!(arg instanceof Map mapArg)) { return null; } PathToSchemasMap pathToSchemas = new PathToSchemasMap(); - Map castArg = (Map) arg; - Set presentProperties = new LinkedHashSet<>(castArg.keySet()); - presentProperties.retainAll(properties.keySet()); - for(String propName: presentProperties) { - Object propValue = castArg.get(propName); + Set presentProperties = new LinkedHashSet<>(); + for (Object key: mapArg.keySet()) { + if (key instanceof String) { + presentProperties.add((String) key); + } + } + for(Map.Entry> entry: properties.entrySet()) { + String propName = entry.getKey(); + if (!presentProperties.contains(propName)) { + continue; + } + @Nullable Object propValue = ((Map) arg).get(propName); List propPathToItem = new ArrayList<>(validationMetadata.pathToItem()); propPathToItem.add(propName); ValidationMetadata propValidationMetadata = new ValidationMetadata( @@ -37,7 +41,7 @@ public class PropertiesValidator implements KeywordValidator { validationMetadata.validatedPathToSchemas(), validationMetadata.seenClasses() ); - Class propClass = properties.get(propName); + Class propClass = entry.getValue(); JsonSchema propSchema = JsonSchemaFactory.getInstance(propClass); if (propValidationMetadata.validationRanEarlier(propSchema)) { // todo add_deeper_validated_schemas diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/PropertyEntry.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/PropertyEntry.hbs index 619b756d7a3..8d3d3840474 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/PropertyEntry.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/PropertyEntry.hbs @@ -2,6 +2,7 @@ package {{{packageName}}}.schemas.validation; import java.util.AbstractMap; +@SuppressWarnings("serial") public class PropertyEntry extends AbstractMap.SimpleEntry> { public PropertyEntry(String key, Class value) { super(key, value); diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/RequiredValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/RequiredValidator.hbs index c6ced3be056..7c9a0b72b16 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/RequiredValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/RequiredValidator.hbs @@ -1,6 +1,7 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.List; @@ -15,18 +16,16 @@ public class RequiredValidator implements KeywordValidator { } @Override - public Object getConstraint() { - return required; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof Map)) { return null; } - Map castArg = (Map) arg; Set missingRequiredProperties = new HashSet<>(required); - missingRequiredProperties.removeAll(castArg.keySet()); + for (Object key: ((Map) arg).keySet()) { + if (key instanceof String) { + missingRequiredProperties.remove(key); + } + } if (!missingRequiredProperties.isEmpty()) { List missingReqProps = missingRequiredProperties.stream().sorted().toList(); String pluralChar = ""; diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/TypeValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/TypeValidator.hbs index aa492962925..3fc3dd6717b 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/TypeValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/TypeValidator.hbs @@ -1,6 +1,7 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.List; import java.util.Map; @@ -14,12 +15,7 @@ public class TypeValidator implements KeywordValidator { } @Override - public Object getConstraint() { - return type; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { Class argClass; if (arg == null) { argClass = Void.class; diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/UniqueItemsValidator.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/UniqueItemsValidator.hbs index b6ca39a0a05..e556a59f816 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/UniqueItemsValidator.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/UniqueItemsValidator.hbs @@ -1,6 +1,7 @@ package {{{packageName}}}.schemas.validation; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.HashSet; import java.util.List; @@ -14,20 +15,15 @@ public class UniqueItemsValidator implements KeywordValidator { } @Override - public Object getConstraint() { - return uniqueItems; - } - - @Override - public PathToSchemasMap validate(JsonSchema schema, Object arg, ValidationMetadata validationMetadata) { + public @Nullable PathToSchemasMap validate(JsonSchema schema, @Nullable Object arg, ValidationMetadata validationMetadata) { if (!(arg instanceof List)) { return null; } if (!uniqueItems) { return null; } - Set seenItems = new HashSet<>(); - for (Object item: (List) arg) { + Set<@Nullable Object> seenItems = new HashSet<>(); + for (@Nullable Object item: (List) arg) { int startingSeenItemsSize = seenItems.size(); seenItems.add(item); if (seenItems.size() == startingSeenItemsSize) { diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/UnsetAnyTypeJsonSchema.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/UnsetAnyTypeJsonSchema.hbs index 121cff1d68d..ce1312f884e 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/UnsetAnyTypeJsonSchema.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/UnsetAnyTypeJsonSchema.hbs @@ -4,6 +4,7 @@ import {{{packageName}}}.configurations.JsonSchemaKeywordFlags; import {{{packageName}}}.configurations.SchemaConfiguration; import {{{packageName}}}.exceptions.InvalidTypeException; import {{{packageName}}}.exceptions.ValidationException; +import org.checkerframework.checker.nullness.qual.Nullable; import java.time.LocalDate; import java.time.ZonedDateTime; @@ -17,8 +18,8 @@ import java.util.Map; import java.util.Objects; import java.util.UUID; -public class UnsetAnyTypeJsonSchema extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static UnsetAnyTypeJsonSchema instance; +public class UnsetAnyTypeJsonSchema extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + private static @Nullable UnsetAnyTypeJsonSchema instance = null; protected UnsetAnyTypeJsonSchema() { super(new JsonSchemaInfo()); @@ -112,14 +113,18 @@ public class UnsetAnyTypeJsonSchema extends JsonSchema implements NullSchemaVali } @Override - public FrozenList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - Object castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); items.add(castItem); i += 1; } @@ -127,7 +132,7 @@ public class UnsetAnyTypeJsonSchema extends JsonSchema implements NullSchemaVali } @Override - public FrozenList validate(List arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -140,22 +145,30 @@ public class UnsetAnyTypeJsonSchema extends JsonSchema implements NullSchemaVali } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); properties.put(propertyName, castValue); } return new FrozenMap<>(properties); } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = new ArrayList<>(); pathToItem.add("args[0]"); @@ -168,7 +181,7 @@ public class UnsetAnyTypeJsonSchema extends JsonSchema implements NullSchemaVali } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg == null) { return getNewInstance((Void) null, pathToItem, pathToSchemas); } else if (arg instanceof Boolean) { @@ -183,6 +196,6 @@ public class UnsetAnyTypeJsonSchema extends JsonSchema implements NullSchemaVali } else if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/schemas/validation/ValidationMetadata.hbs b/src/main/resources/java/src/main/java/packagename/schemas/validation/ValidationMetadata.hbs index 56175bb8976..a868e40d0b2 100644 --- a/src/main/resources/java/src/main/java/packagename/schemas/validation/ValidationMetadata.hbs +++ b/src/main/resources/java/src/main/java/packagename/schemas/validation/ValidationMetadata.hbs @@ -1,5 +1,6 @@ package {{{packageName}}}.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import {{{packageName}}}.configurations.SchemaConfiguration; import java.util.List; import java.util.Map; @@ -13,7 +14,7 @@ public record ValidationMetadata( ) { public boolean validationRanEarlier(JsonSchema schema) { - Map validatedSchemas = validatedPathToSchemas.getOrDefault(pathToItem, null); + @Nullable Map validatedSchemas = validatedPathToSchemas.get(pathToItem); if (validatedSchemas != null && validatedSchemas.containsKey(schema)) { return true; } diff --git a/src/main/resources/java/src/test/java/packagename/schemas/AnyTypeSchemaTest.hbs b/src/main/resources/java/src/test/java/packagename/schemas/AnyTypeSchemaTest.hbs index 25467823fb3..ac605239396 100644 --- a/src/main/resources/java/src/test/java/packagename/schemas/AnyTypeSchemaTest.hbs +++ b/src/main/resources/java/src/test/java/packagename/schemas/AnyTypeSchemaTest.hbs @@ -1,10 +1,10 @@ package {{{packageName}}}.schemas; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import {{{packageName}}}.configurations.JsonSchemaKeywordFlags; import {{{packageName}}}.configurations.SchemaConfiguration; -import {{{packageName}}}.schemas.validation.JsonSchemaFactory; import {{{packageName}}}.schemas.validation.FrozenList; import {{{packageName}}}.schemas.validation.FrozenMap; @@ -19,10 +19,16 @@ public class AnyTypeSchemaTest { static final AnyTypeJsonSchema schema = AnyTypeJsonSchema.getInstance(); static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone()); + @SuppressWarnings("nullness") + private Void assertNull(@Nullable Object object) { + Assert.assertNull(object); + return null; + } + @Test public void testValidateNull() { Void validatedValue = schema.validate((Void) null, configuration); - Assert.assertNull(validatedValue); + assertNull(validatedValue); } @Test diff --git a/src/main/resources/java/src/test/java/packagename/schemas/ArrayTypeSchemaTest.hbs b/src/main/resources/java/src/test/java/packagename/schemas/ArrayTypeSchemaTest.hbs index e2f2058b688..01c63b79c67 100644 --- a/src/main/resources/java/src/test/java/packagename/schemas/ArrayTypeSchemaTest.hbs +++ b/src/main/resources/java/src/test/java/packagename/schemas/ArrayTypeSchemaTest.hbs @@ -1,5 +1,6 @@ package {{{packageName}}}.schemas; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import {{{packageName}}}.configurations.JsonSchemaKeywordFlags; @@ -15,6 +16,7 @@ import {{{packageName}}}.schemas.validation.ValidationMetadata; import java.util.ArrayList; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Objects; @@ -44,9 +46,16 @@ public class ArrayTypeSchemaTest { for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - String castItem = (String) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(castItem instanceof String)) { + throw new InvalidTypeException("Instantiated type of item is invalid"); + } + items.add((String) castItem); i += 1; } return new FrozenList<>(items); @@ -64,11 +73,11 @@ public class ArrayTypeSchemaTest { } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof List) { return getNewInstance((List) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } @@ -93,14 +102,21 @@ public class ArrayTypeSchemaTest { @Override public ArrayWithOutputClsSchemaList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - ArrayList items = new ArrayList<>(); + List items = new ArrayList<>(); int i = 0; for (Object item: arg) { List itemPathToItem = new ArrayList<>(pathToItem); itemPathToItem.add(i); - JsonSchema itemSchema = pathToSchemas.get(itemPathToItem).entrySet().iterator().next().getKey(); - String castItem = (String) itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(castItem); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castItem = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(castItem instanceof String)) { + throw new InvalidTypeException("Instantiated type of item is invalid"); + } + items.add((String) castItem); i += 1; } FrozenList newInstanceItems = new FrozenList<>(items); @@ -119,11 +135,11 @@ public class ArrayTypeSchemaTest { } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof List) { return getNewInstance((List) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } diff --git a/src/main/resources/java/src/test/java/packagename/schemas/ListSchemaTest.hbs b/src/main/resources/java/src/test/java/packagename/schemas/ListSchemaTest.hbs index 7bd571064c7..03a6086b4e4 100644 --- a/src/main/resources/java/src/test/java/packagename/schemas/ListSchemaTest.hbs +++ b/src/main/resources/java/src/test/java/packagename/schemas/ListSchemaTest.hbs @@ -1,11 +1,11 @@ package {{{packageName}}}.schemas; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import {{{packageName}}}.configurations.JsonSchemaKeywordFlags; import {{{packageName}}}.configurations.SchemaConfiguration; import {{{packageName}}}.schemas.validation.JsonSchema; -import {{{packageName}}}.schemas.validation.JsonSchemaFactory; import {{{packageName}}}.schemas.validation.FrozenList; import {{{packageName}}}.exceptions.ValidationException; import {{{packageName}}}.schemas.validation.PathToSchemasMap; @@ -38,7 +38,7 @@ public class ListSchemaTest { public void testValidateList() { List inList = new ArrayList<>(); inList.add("today"); - FrozenList validatedValue = listJsonSchema.validate(inList, configuration); + FrozenList<@Nullable Object> validatedValue = listJsonSchema.validate(inList, configuration); ArrayList outList = new ArrayList<>(); outList.add("today"); Assert.assertEquals(validatedValue, outList); diff --git a/src/main/resources/java/src/test/java/packagename/schemas/MapSchemaTest.hbs b/src/main/resources/java/src/test/java/packagename/schemas/MapSchemaTest.hbs index 8fdb1b57722..85331314d4e 100644 --- a/src/main/resources/java/src/test/java/packagename/schemas/MapSchemaTest.hbs +++ b/src/main/resources/java/src/test/java/packagename/schemas/MapSchemaTest.hbs @@ -1,5 +1,6 @@ package {{{packageName}}}.schemas; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import {{{packageName}}}.configurations.JsonSchemaKeywordFlags; @@ -39,7 +40,7 @@ public class MapSchemaTest { public void testValidateMap() { Map inMap = new LinkedHashMap<>(); inMap.put("today", LocalDate.of(2017, 7, 21)); - FrozenMap validatedValue = mapJsonSchema.validate(inMap, configuration); + FrozenMap<@Nullable Object> validatedValue = mapJsonSchema.validate(inMap, configuration); LinkedHashMap outMap = new LinkedHashMap<>(); outMap.put("today", "2017-07-21"); Assert.assertEquals(validatedValue, outMap); diff --git a/src/main/resources/java/src/test/java/packagename/schemas/NullSchemaTest.hbs b/src/main/resources/java/src/test/java/packagename/schemas/NullSchemaTest.hbs index bb7a2e55a10..2acce4245ed 100644 --- a/src/main/resources/java/src/test/java/packagename/schemas/NullSchemaTest.hbs +++ b/src/main/resources/java/src/test/java/packagename/schemas/NullSchemaTest.hbs @@ -25,6 +25,7 @@ public class NullSchemaTest { @Test + @SuppressWarnings("nullness") public void testValidateNull() { Void validatedValue = nullJsonSchema.validate(null, configuration); Assert.assertNull(validatedValue); diff --git a/src/main/resources/java/src/test/java/packagename/schemas/NumberSchemaTest.hbs b/src/main/resources/java/src/test/java/packagename/schemas/NumberSchemaTest.hbs index 464fc6ec5b7..e712e6c0572 100644 --- a/src/main/resources/java/src/test/java/packagename/schemas/NumberSchemaTest.hbs +++ b/src/main/resources/java/src/test/java/packagename/schemas/NumberSchemaTest.hbs @@ -5,14 +5,22 @@ import org.junit.Test; import {{{packageName}}}.configurations.JsonSchemaKeywordFlags; import {{{packageName}}}.configurations.SchemaConfiguration; import {{{packageName}}}.schemas.validation.JsonSchema; -import {{{packageName}}}.schemas.validation.JsonSchemaFactory; import {{{packageName}}}.exceptions.ValidationException; +import {{{packageName}}}.schemas.validation.PathToSchemasMap; +import {{{packageName}}}.schemas.validation.ValidationMetadata; -import java.math.BigDecimal; +import java.util.LinkedHashSet; +import java.util.List; public class NumberSchemaTest { static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone()); static final NumberJsonSchema numberJsonSchema = NumberJsonSchema.getInstance(); + static final ValidationMetadata validationMetadata = new ValidationMetadata( + List.of("args[0"), + configuration, + new PathToSchemasMap(), + new LinkedHashSet<>() + ); @Test public void testValidateInteger() { @@ -40,8 +48,10 @@ public class NumberSchemaTest { @Test public void testExceptionThrownForInvalidType() { - Assert.assertThrows(ValidationException.class, () -> numberJsonSchema.validate( - null, configuration + Assert.assertThrows(ValidationException.class, () -> JsonSchema.validate( + numberJsonSchema, + null, + validationMetadata )); } } diff --git a/src/main/resources/java/src/test/java/packagename/schemas/ObjectTypeSchemaTest.hbs b/src/main/resources/java/src/test/java/packagename/schemas/ObjectTypeSchemaTest.hbs index 33106ba9406..70c093a4963 100644 --- a/src/main/resources/java/src/test/java/packagename/schemas/ObjectTypeSchemaTest.hbs +++ b/src/main/resources/java/src/test/java/packagename/schemas/ObjectTypeSchemaTest.hbs @@ -1,5 +1,6 @@ package {{{packageName}}}.schemas; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import {{{packageName}}}.configurations.JsonSchemaKeywordFlags; @@ -23,7 +24,6 @@ import java.util.Map; import java.util.Objects; import java.util.Set; - public class ObjectTypeSchemaTest { static final SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone()); static final ValidationMetadata validationMetadata = new ValidationMetadata( @@ -33,8 +33,8 @@ public class ObjectTypeSchemaTest { new LinkedHashSet<>() ); - public static class ObjectWithPropsSchema extends JsonSchema implements MapSchemaValidator> { - private static ObjectWithPropsSchema instance; + public static class ObjectWithPropsSchema extends JsonSchema implements MapSchemaValidator> { + private static @Nullable ObjectWithPropsSchema instance = null; private ObjectWithPropsSchema() { super(new JsonSchemaInfo() .type(Set.of(Map.class)) @@ -53,22 +53,30 @@ public class ObjectTypeSchemaTest { } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); properties.put(propertyName, castValue); } return new FrozenMap<>(properties); } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -79,16 +87,16 @@ public class ObjectTypeSchemaTest { } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof Map) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } public static class ObjectWithAddpropsSchema extends JsonSchema implements MapSchemaValidator> { - private static ObjectWithAddpropsSchema instance; + private static @Nullable ObjectWithAddpropsSchema instance = null; private ObjectWithAddpropsSchema() { super(new JsonSchemaInfo() .type(Set.of(Map.class)) @@ -107,13 +115,24 @@ public class ObjectTypeSchemaTest { public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - String castValue = (String) propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, castValue); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(castValue instanceof String)) { + throw new InvalidTypeException("Invalid type for property value"); + } + properties.put(propertyName, (String) castValue); } return new FrozenMap<>(properties); } @@ -130,17 +149,17 @@ public class ObjectTypeSchemaTest { } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof FrozenMap) { @SuppressWarnings("unchecked") FrozenMap castArg = (FrozenMap) arg; return getNewInstance(castArg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class ObjectWithPropsAndAddpropsSchema extends JsonSchema implements MapSchemaValidator> { - private static ObjectWithPropsAndAddpropsSchema instance; + public static class ObjectWithPropsAndAddpropsSchema extends JsonSchema implements MapSchemaValidator> { + private static @Nullable ObjectWithPropsAndAddpropsSchema instance = null; private ObjectWithPropsAndAddpropsSchema() { super(new JsonSchemaInfo() .type(Set.of(Map.class)) @@ -159,22 +178,30 @@ public class ObjectTypeSchemaTest { } @Override - public FrozenMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); properties.put(propertyName, castValue); } return new FrozenMap<>(properties); } @Override - public FrozenMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { Set> pathSet = new HashSet<>(); List pathToItem = List.of("args[0"); Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); @@ -185,17 +212,17 @@ public class ObjectTypeSchemaTest { } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof FrozenMap) { @SuppressWarnings("unchecked") FrozenMap castArg = (FrozenMap) arg; return getNewInstance(castArg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } - public static class ObjectWithOutputTypeSchemaMap extends FrozenMap { - protected ObjectWithOutputTypeSchemaMap(FrozenMap m) { + public static class ObjectWithOutputTypeSchemaMap extends FrozenMap<@Nullable Object> { + protected ObjectWithOutputTypeSchemaMap(FrozenMap<@Nullable Object> m) { super(m); } @@ -206,7 +233,7 @@ public class ObjectTypeSchemaTest { public static class ObjectWithOutputTypeSchema extends JsonSchema implements MapSchemaValidator { - private static ObjectWithOutputTypeSchema instance; + private static @Nullable ObjectWithOutputTypeSchema instance = null; public ObjectWithOutputTypeSchema() { super(new JsonSchemaInfo() .type(Set.of(Map.class)) @@ -225,14 +252,22 @@ public class ObjectTypeSchemaTest { @Override public ObjectWithOutputTypeSchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); + LinkedHashMap properties = new LinkedHashMap<>(); for(Map.Entry entry: arg.entrySet()) { - String propertyName = (String) entry.getKey(); + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; List propertyPathToItem = new ArrayList<>(pathToItem); propertyPathToItem.add(propertyName); Object value = entry.getValue(); - JsonSchema propertySchema = pathToSchemas.get(propertyPathToItem).entrySet().iterator().next().getKey(); - Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object castValue = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); properties.put(propertyName, castValue); } return new ObjectWithOutputTypeSchemaMap(new FrozenMap<>(properties)); @@ -250,11 +285,11 @@ public class ObjectTypeSchemaTest { } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof FrozenMap) { return getNewInstance((Map) arg, pathToItem, pathToSchemas); } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } @@ -275,7 +310,7 @@ public class ObjectTypeSchemaTest { // map with only property works Map inMap = new LinkedHashMap<>(); inMap.put("someString", "abc"); - FrozenMap validatedValue = schema.validate(inMap, configuration); + FrozenMap<@Nullable Object> validatedValue = schema.validate(inMap, configuration); LinkedHashMap outMap = new LinkedHashMap<>(); outMap.put("someString", "abc"); Assert.assertEquals(validatedValue, outMap); @@ -337,7 +372,7 @@ public class ObjectTypeSchemaTest { // map with only property works Map inMap = new LinkedHashMap<>(); inMap.put("someString", "abc"); - FrozenMap validatedValue = schema.validate(inMap, configuration); + FrozenMap<@Nullable Object> validatedValue = schema.validate(inMap, configuration); LinkedHashMap outMap = new LinkedHashMap<>(); outMap.put("someString", "abc"); Assert.assertEquals(validatedValue, outMap); diff --git a/src/main/resources/java/src/test/java/packagename/schemas/validation/AdditionalPropertiesValidatorTest.hbs b/src/main/resources/java/src/test/java/packagename/schemas/validation/AdditionalPropertiesValidatorTest.hbs index 31f81bfca82..2c787064d5a 100644 --- a/src/main/resources/java/src/test/java/packagename/schemas/validation/AdditionalPropertiesValidatorTest.hbs +++ b/src/main/resources/java/src/test/java/packagename/schemas/validation/AdditionalPropertiesValidatorTest.hbs @@ -1,5 +1,6 @@ package {{{packageName}}}.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import {{{packageName}}}.configurations.JsonSchemaKeywordFlags; @@ -17,15 +18,14 @@ import java.util.Map; import java.util.Set; public class AdditionalPropertiesValidatorTest { - public static class ObjectWithPropsSchema extends JsonSchema { - private static ObjectWithPropsSchema instance; + private static @Nullable ObjectWithPropsSchema instance = null; private ObjectWithPropsSchema() { super(new JsonSchemaInfo() - .type(Set.of(FrozenMap.class)) - .properties(Map.ofEntries( - new PropertyEntry("someString", StringJsonSchema.class) - )) + .type(Set.of(FrozenMap.class)) + .properties(Map.ofEntries( + new PropertyEntry("someString", StringJsonSchema.class) + )) ); } @@ -38,15 +38,21 @@ public class AdditionalPropertiesValidatorTest { } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof FrozenMap) { @SuppressWarnings("unchecked") FrozenMap castArg = (FrozenMap) arg; return castArg; } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } + @SuppressWarnings("nullness") + private Void assertNull(@Nullable Object object) { + Assert.assertNull(object); + return null; + } + @Test public void testCorrectPropertySucceeds() { List pathToItem = new ArrayList<>(); @@ -67,6 +73,9 @@ public class AdditionalPropertiesValidatorTest { arg, validationMetadata ); + if (pathToSchemas == null) { + throw new RuntimeException("Invalid null value for pathToSchemas for this test case"); + } List expectedPathToItem = new ArrayList<>(); expectedPathToItem.add("args[0]"); expectedPathToItem.add("someAddProp"); @@ -94,7 +103,7 @@ public class AdditionalPropertiesValidatorTest { 1, validationMetadata ); - Assert.assertNull(pathToSchemas); + assertNull(pathToSchemas); } @Test diff --git a/src/main/resources/java/src/test/java/packagename/schemas/validation/FormatValidatorTest.hbs b/src/main/resources/java/src/test/java/packagename/schemas/validation/FormatValidatorTest.hbs index ee059035bdd..1a1c3c217d2 100644 --- a/src/main/resources/java/src/test/java/packagename/schemas/validation/FormatValidatorTest.hbs +++ b/src/main/resources/java/src/test/java/packagename/schemas/validation/FormatValidatorTest.hbs @@ -1,5 +1,6 @@ package {{{packageName}}}.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import {{{packageName}}}.configurations.JsonSchemaKeywordFlags; @@ -21,6 +22,12 @@ public class FormatValidatorTest { new LinkedHashSet<>() ); + @SuppressWarnings("nullness") + private Void assertNull(@Nullable Object object) { + Assert.assertNull(object); + return null; + } + @Test public void testIntFormatSucceedsWithFloat() { final FormatValidator validator = new FormatValidator("int"); @@ -29,7 +36,7 @@ public class FormatValidatorTest { 1.0f, validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -50,7 +57,7 @@ public class FormatValidatorTest { 1, validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -71,7 +78,7 @@ public class FormatValidatorTest { -2147483648, validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -82,7 +89,7 @@ public class FormatValidatorTest { 2147483647, validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -114,7 +121,7 @@ public class FormatValidatorTest { -9223372036854775808L, validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -125,7 +132,7 @@ public class FormatValidatorTest { 9223372036854775807L, validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -157,7 +164,7 @@ public class FormatValidatorTest { -3.4028234663852886e+38f, validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -168,7 +175,7 @@ public class FormatValidatorTest { 3.4028234663852886e+38f, validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -199,7 +206,7 @@ public class FormatValidatorTest { -1.7976931348623157E+308d, validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -210,7 +217,7 @@ public class FormatValidatorTest { 1.7976931348623157E+308d, validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -241,7 +248,7 @@ public class FormatValidatorTest { "3.14", validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -252,7 +259,7 @@ public class FormatValidatorTest { "1", validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -273,7 +280,7 @@ public class FormatValidatorTest { "2017-01-20", validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test @@ -294,6 +301,6 @@ public class FormatValidatorTest { "2017-07-21T17:32:28Z", validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } } diff --git a/src/main/resources/java/src/test/java/packagename/schemas/validation/ItemsValidatorTest.hbs b/src/main/resources/java/src/test/java/packagename/schemas/validation/ItemsValidatorTest.hbs index db5fbd04973..082711a3fbc 100644 --- a/src/main/resources/java/src/test/java/packagename/schemas/validation/ItemsValidatorTest.hbs +++ b/src/main/resources/java/src/test/java/packagename/schemas/validation/ItemsValidatorTest.hbs @@ -1,5 +1,6 @@ package {{{packageName}}}.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import {{{packageName}}}.configurations.JsonSchemaKeywordFlags; @@ -14,6 +15,11 @@ import java.util.LinkedHashSet; import java.util.List; public class ItemsValidatorTest { + @SuppressWarnings("nullness") + private Void assertNull(@Nullable Object object) { + Assert.assertNull(object); + return null; + } @Test public void testCorrectItemsSucceeds() { @@ -34,6 +40,9 @@ public class ItemsValidatorTest { arg, validationMetadata ); + if (pathToSchemas == null) { + throw new RuntimeException("Invalid null value in pathToSchemas for this test case"); + } List expectedPathToItem = new ArrayList<>(); expectedPathToItem.add("args[0]"); expectedPathToItem.add(0); @@ -61,7 +70,7 @@ public class ItemsValidatorTest { 1, validationMetadata ); - Assert.assertNull(pathToSchemas); + assertNull(pathToSchemas); } @Test @@ -85,3 +94,4 @@ public class ItemsValidatorTest { )); } } + diff --git a/src/main/resources/java/src/test/java/packagename/schemas/validation/JsonSchemaTest.hbs b/src/main/resources/java/src/test/java/packagename/schemas/validation/JsonSchemaTest.hbs index c70d858a0b0..61cb3074d4d 100644 --- a/src/main/resources/java/src/test/java/packagename/schemas/validation/JsonSchemaTest.hbs +++ b/src/main/resources/java/src/test/java/packagename/schemas/validation/JsonSchemaTest.hbs @@ -1,5 +1,6 @@ package {{{packageName}}}.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import {{{packageName}}}.configurations.JsonSchemaKeywordFlags; @@ -13,7 +14,7 @@ import java.util.List; import java.util.Set; class SomeSchema extends JsonSchema { - private static SomeSchema instance; + private static @Nullable SomeSchema instance = null; protected SomeSchema() { super(new JsonSchemaInfo() .type(Set.of(String.class)) @@ -28,11 +29,11 @@ class SomeSchema extends JsonSchema { } @Override - public Object getNewInstance(Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { + public Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) { if (arg instanceof String) { return arg; } - throw new InvalidTypeException("Invalid input type="+arg.getClass()+". It can't be instantiated by this schema"); + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); } } diff --git a/src/main/resources/java/src/test/java/packagename/schemas/validation/PropertiesValidatorTest.hbs b/src/main/resources/java/src/test/java/packagename/schemas/validation/PropertiesValidatorTest.hbs index dfd783a9e62..5ae3dfc6c67 100644 --- a/src/main/resources/java/src/test/java/packagename/schemas/validation/PropertiesValidatorTest.hbs +++ b/src/main/resources/java/src/test/java/packagename/schemas/validation/PropertiesValidatorTest.hbs @@ -1,5 +1,6 @@ package {{{packageName}}}.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import {{{packageName}}}.configurations.JsonSchemaKeywordFlags; @@ -15,6 +16,12 @@ import java.util.List; import java.util.Map; public class PropertiesValidatorTest { + @SuppressWarnings("nullness") + private Void assertNull(@Nullable Object object) { + Assert.assertNull(object); + return null; + } + @Test public void testCorrectPropertySucceeds() { Map> properties = new LinkedHashMap<>(); @@ -37,6 +44,9 @@ public class PropertiesValidatorTest { arg, validationMetadata ); + if (pathToSchemas == null) { + throw new RuntimeException("Invalid null value for pathToSchemas for this test case"); + } List expectedPathToItem = new ArrayList<>(); expectedPathToItem.add("args[0]"); expectedPathToItem.add("someString"); @@ -66,7 +76,7 @@ public class PropertiesValidatorTest { 1, validationMetadata ); - Assert.assertNull(pathToSchemas); + assertNull(pathToSchemas); } @Test diff --git a/src/main/resources/java/src/test/java/packagename/schemas/validation/RequiredValidatorTest.hbs b/src/main/resources/java/src/test/java/packagename/schemas/validation/RequiredValidatorTest.hbs index f5a5e025864..156e5a118dd 100644 --- a/src/main/resources/java/src/test/java/packagename/schemas/validation/RequiredValidatorTest.hbs +++ b/src/main/resources/java/src/test/java/packagename/schemas/validation/RequiredValidatorTest.hbs @@ -1,5 +1,6 @@ package {{{packageName}}}.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import {{{packageName}}}.configurations.JsonSchemaKeywordFlags; @@ -14,6 +15,11 @@ import java.util.List; import java.util.Set; public class RequiredValidatorTest { + @SuppressWarnings("nullness") + private Void assertNull(@Nullable Object object) { + Assert.assertNull(object); + return null; + } @Test public void testCorrectPropertySucceeds() { @@ -37,7 +43,7 @@ public class RequiredValidatorTest { arg, validationMetadata ); - Assert.assertNull(pathToSchemas); + assertNull(pathToSchemas); } @Test @@ -59,7 +65,7 @@ public class RequiredValidatorTest { 1, validationMetadata ); - Assert.assertNull(pathToSchemas); + assertNull(pathToSchemas); } @Test diff --git a/src/main/resources/java/src/test/java/packagename/schemas/validation/TypeValidatorTest.hbs b/src/main/resources/java/src/test/java/packagename/schemas/validation/TypeValidatorTest.hbs index fb3c13c71c1..99c9bc73d1e 100644 --- a/src/main/resources/java/src/test/java/packagename/schemas/validation/TypeValidatorTest.hbs +++ b/src/main/resources/java/src/test/java/packagename/schemas/validation/TypeValidatorTest.hbs @@ -1,5 +1,6 @@ package {{{packageName}}}.schemas.validation; +import org.checkerframework.checker.nullness.qual.Nullable; import org.junit.Assert; import org.junit.Test; import {{{packageName}}}.configurations.JsonSchemaKeywordFlags; @@ -11,6 +12,11 @@ import java.util.ArrayList; import java.util.LinkedHashSet; public class TypeValidatorTest { + @SuppressWarnings("nullness") + private Void assertNull(@Nullable Object object) { + Assert.assertNull(object); + return null; + } @Test public void testValidateSucceeds() { @@ -23,12 +29,12 @@ public class TypeValidatorTest { new PathToSchemasMap(), new LinkedHashSet<>() ); - PathToSchemasMap pathToSchemasMap = validator.validate( + @Nullable PathToSchemasMap pathToSchemasMap = validator.validate( StringJsonSchema.getInstance(), "hi", validationMetadata ); - Assert.assertNull(pathToSchemasMap); + assertNull(pathToSchemasMap); } @Test