|
| 1 | +# Migration from v2 to v3 |
| 2 | + |
| 3 | +This document contains all the breaking changes and migrations guidelines for adapting your code to the new version. |
| 4 | + |
| 5 | +## allowInheritance set to true will enable inheritance |
| 6 | + |
| 7 | +This feature introduces a new option called `allowInheritance` in the interpreter options, which controls whether the generated models should inherit when the schema includes an `allOf`. By default, this option is set to false, which means that you'll not be affected if this property is not set. In the `MetaModel` and the `ConstrainedMetaModel` options, there is now an `extend` property (a list of models) and an `isExtended` property (boolean). |
| 8 | + |
| 9 | +Here is an example of how to use the new feature and the `allowInheritance` option in your code: |
| 10 | + |
| 11 | +```ts |
| 12 | +const generator = new JavaFileGenerator({ |
| 13 | + processorOptions: { |
| 14 | + interpreter: { |
| 15 | + allowInheritance: true |
| 16 | + } |
| 17 | + } |
| 18 | +}); |
| 19 | +``` |
| 20 | + |
| 21 | +## TypeScript |
| 22 | + |
| 23 | +### JS reserved keywords are no longer applied by default |
| 24 | +By default up until now, JS reserved keywords have been checked for TS as well. Which means that something like: |
| 25 | + |
| 26 | +``` |
| 27 | +{ |
| 28 | + $schema: 'http://json-schema.org/draft-07/schema#', |
| 29 | + type: 'object', |
| 30 | + additionalProperties: false, |
| 31 | + properties: { |
| 32 | + location: { |
| 33 | + type: 'string' |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +Would be default be rendered as: |
| 40 | +```ts |
| 41 | +class Root { |
| 42 | + private _reservedLocation?: string; |
| 43 | + |
| 44 | + constructor(input: { |
| 45 | + reservedLocation?: string, |
| 46 | + }) { |
| 47 | + this._reservedLocation = input.reservedLocation; |
| 48 | + } |
| 49 | + |
| 50 | + get reservedLocation(): string | undefined { return this._reservedLocation; } |
| 51 | + set reservedLocation(reservedLocation: string | undefined) { this._reservedLocation = reservedLocation; } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +However, without setting `useJavascriptReservedKeywords: true` by default the following will be generated: |
| 56 | + |
| 57 | +```ts |
| 58 | +class Root { |
| 59 | + private _location?: string; |
| 60 | + |
| 61 | + constructor(input: { |
| 62 | + location?: string, |
| 63 | + }) { |
| 64 | + this._location = input.location; |
| 65 | + } |
| 66 | + |
| 67 | + get location(): string | undefined { return this._location; } |
| 68 | + set location(location: string | undefined) { this._location = location; } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +## JavaScript |
| 73 | + |
| 74 | +Is not affected by this change. |
| 75 | + |
| 76 | +## C# |
| 77 | + |
| 78 | +### System.TimeSpan is used when format is time |
| 79 | + |
| 80 | +This example used to generate a `string`, but is now instead using `System.TimeSpan`. |
| 81 | + |
| 82 | +```yaml |
| 83 | +type: object |
| 84 | +properties: |
| 85 | + duration: |
| 86 | + type: string |
| 87 | + format: time |
| 88 | +``` |
| 89 | +
|
| 90 | +will generate |
| 91 | +
|
| 92 | +```csharp |
| 93 | +public class TestClass { |
| 94 | + private System.TimeSpan duration; |
| 95 | + ... |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +### System.DateTime is used when format is date-time |
| 100 | + |
| 101 | +This example used to generate a `string`, but is now instead using `System.DateTime`. |
| 102 | + |
| 103 | +```yaml |
| 104 | +type: object |
| 105 | +properties: |
| 106 | + dob: |
| 107 | + type: string |
| 108 | + format: date-time |
| 109 | +``` |
| 110 | +
|
| 111 | +will generate |
| 112 | +
|
| 113 | +```csharp |
| 114 | +public class TestClass { |
| 115 | + private System.DateTime dob; |
| 116 | + ... |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +### System.Guid is used when format is uuid |
| 121 | + |
| 122 | +This example used to generate a `string`, but is now instead using `System.Guid`. |
| 123 | + |
| 124 | +```yaml |
| 125 | +type: object |
| 126 | +properties: |
| 127 | + uniqueId: |
| 128 | + type: string |
| 129 | + format: uuid |
| 130 | +``` |
| 131 | +
|
| 132 | +will generate |
| 133 | +
|
| 134 | +```csharp |
| 135 | +public class TestClass { |
| 136 | + private System.Guid uniqueId; |
| 137 | + ... |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +## Java |
| 142 | + |
| 143 | +### java.time.Duration is used when format is duration |
| 144 | + |
| 145 | +This example used to generate a `String`, but is now instead using `java.time.Duration`. |
| 146 | + |
| 147 | +```yaml |
| 148 | +type: object |
| 149 | +properties: |
| 150 | + duration: |
| 151 | + type: string |
| 152 | + format: duration |
| 153 | +``` |
| 154 | +
|
| 155 | +will generate |
| 156 | +
|
| 157 | +```java |
| 158 | +public class TestClass { |
| 159 | + private java.time.Duration duration; |
| 160 | + ... |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +### inheritance will generate interfaces |
| 165 | + |
| 166 | +Please read the section about [allowInheritance](#allowinheritance-set-to-true-will-enable-inheritance) first. When `allowInheritance` is enabled, interfaces will be generated for schemas that uses `allOf`: |
| 167 | + |
| 168 | +```yaml |
| 169 | +components: |
| 170 | + messages: |
| 171 | + Vehicle: |
| 172 | + payload: |
| 173 | + oneOf: |
| 174 | + - $ref: '#/components/schemas/Car' |
| 175 | + - $ref: '#/components/schemas/Truck' |
| 176 | + schemas: |
| 177 | + Vehicle: |
| 178 | + title: Vehicle |
| 179 | + type: object |
| 180 | + discriminator: vehicleType |
| 181 | + properties: |
| 182 | + vehicleType: |
| 183 | + title: VehicleType |
| 184 | + type: string |
| 185 | + length: |
| 186 | + type: number |
| 187 | + format: float |
| 188 | + required: |
| 189 | + - vehicleType |
| 190 | + Car: |
| 191 | + allOf: |
| 192 | + - '#/components/schemas/Vehicle' |
| 193 | + - type: object |
| 194 | + properties: |
| 195 | + vehicleType: |
| 196 | + const: Car |
| 197 | + Truck: |
| 198 | + allOf: |
| 199 | + - '#/components/schemas/Vehicle' |
| 200 | + - type: object |
| 201 | + properties: |
| 202 | + vehicleType: |
| 203 | + const: Truck |
| 204 | +``` |
| 205 | +
|
| 206 | +will generate |
| 207 | +
|
| 208 | +```java |
| 209 | +public interface NewVehicle { |
| 210 | + VehicleType getVehicleType(); |
| 211 | +} |
| 212 | + |
| 213 | +public class Car implements NewVehicle, Vehicle { |
| 214 | + private final VehicleType vehicleType = VehicleType.CAR; |
| 215 | + private Float length; |
| 216 | + private Map<String, Object> additionalProperties; |
| 217 | + |
| 218 | + public VehicleType getVehicleType() { return this.vehicleType; } |
| 219 | + |
| 220 | + @Override |
| 221 | + public Float getLength() { return this.length; } |
| 222 | + @Override |
| 223 | + public void setLength(Float length) { this.length = length; } |
| 224 | +} |
| 225 | + |
| 226 | +public enum VehicleType { |
| 227 | + CAR((String)\\"Car\\"), TRUCK((String)\\"Truck\\"); |
| 228 | + |
| 229 | + private String value; |
| 230 | + |
| 231 | + VehicleType(String value) { |
| 232 | + this.value = value; |
| 233 | + } |
| 234 | + |
| 235 | + public String getValue() { |
| 236 | + return value; |
| 237 | + } |
| 238 | + |
| 239 | + public static VehicleType fromValue(String value) { |
| 240 | + for (VehicleType e : VehicleType.values()) { |
| 241 | + if (e.value.equals(value)) { |
| 242 | + return e; |
| 243 | + } |
| 244 | + } |
| 245 | + throw new IllegalArgumentException(\\"Unexpected value '\\" + value + \\"'\\"); |
| 246 | + } |
| 247 | + |
| 248 | + @Override |
| 249 | + public String toString() { |
| 250 | + return String.valueOf(value); |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | +public interface Vehicle { |
| 255 | + public Float getLength(); |
| 256 | + public void setLength(Float length); |
| 257 | +} |
| 258 | + |
| 259 | +public class Truck implements NewVehicle, Vehicle { |
| 260 | + private final VehicleType vehicleType = VehicleType.TRUCK; |
| 261 | + private Float length; |
| 262 | + private Map<String, Object> additionalProperties; |
| 263 | + |
| 264 | + public VehicleType getVehicleType() { return this.vehicleType; } |
| 265 | + |
| 266 | + @Override |
| 267 | + public Float getLength() { return this.length; } |
| 268 | + @Override |
| 269 | + public void setLength(Float length) { this.length = length; } |
| 270 | +} |
| 271 | +``` |
| 272 | + |
| 273 | +## Kotlin |
| 274 | + |
| 275 | +Is not affected by this change. |
| 276 | + |
| 277 | +## Rust |
| 278 | + |
| 279 | +Is not affected by this change. |
| 280 | + |
| 281 | +## Python |
| 282 | + |
| 283 | +### Union type for the Pydantic preset supports Python pre 3.10 |
| 284 | + |
| 285 | +Modelina used to use the newer way of representing unions in Python by using the `|` operator. In the Pydantic preset, this is now adjusted to support Python pre 3.10 by using `Union[Model1, Model2]` instead: |
| 286 | + |
| 287 | +```yaml |
| 288 | +title: UnionTest |
| 289 | +type: object |
| 290 | + properties: |
| 291 | + unionTest: |
| 292 | + oneOf: |
| 293 | + - title: Union1 |
| 294 | + type: object |
| 295 | + properties: |
| 296 | + testProp1: |
| 297 | + type: string |
| 298 | + - title: Union2 |
| 299 | + type: object |
| 300 | + properties: |
| 301 | + testProp2: |
| 302 | + type: string |
| 303 | +``` |
| 304 | +
|
| 305 | +will generate |
| 306 | +
|
| 307 | +```python |
| 308 | +class UnionTest(BaseModel): |
| 309 | + unionTest: Optional[Union[Union1, Union2]] = Field() |
| 310 | + additionalProperties: Optional[dict[Any, Any]] = Field() |
| 311 | + |
| 312 | +class Union1(BaseModel): |
| 313 | + testProp1: Optional[str] = Field() |
| 314 | + additionalProperties: Optional[dict[Any, Any]] = Field() |
| 315 | + |
| 316 | +class Union2(BaseModel): |
| 317 | + testProp2: Optional[str] = Field() |
| 318 | + additionalProperties: Optional[dict[Any, Any]] = Field() |
| 319 | +``` |
| 320 | +
|
| 321 | +## Go |
| 322 | +
|
| 323 | +Is not affected by this change. |
| 324 | +
|
| 325 | +## Dart |
| 326 | +
|
| 327 | +Is not affected by this change. |
| 328 | +
|
| 329 | +## C++ |
| 330 | +
|
| 331 | +Is not affected by this change. |
| 332 | +
|
| 333 | +## Options in constraints |
| 334 | +
|
| 335 | +As part of https://github.com/asyncapi/modelina/issues/1475 we had the need to access options in the constraint logic, therefore all constraints now have direct access to the provided options. |
| 336 | +
|
| 337 | +To make it easier we now expose types for each of the constraints in each language to make it easier to re-use in TS integrations. They can be accessed as following: |
| 338 | +
|
| 339 | +```ts |
| 340 | +import { <language>ConstantConstraint, <language>EnumKeyConstraint, <language>EnumValueConstraint, <language>ModelNameConstraint, <language>PropertyKeyConstraint } from @asyncapi/modelina |
| 341 | +``` |
0 commit comments