Skip to content

Commit

Permalink
feat!: release v3 (#1599)
Browse files Browse the repository at this point in the history
* feat!: convert to use multi parser (#1587)
* feat!: adds support for duration in java (#1604)
* feat: enable AsyncAPI v3 (#1600)
* fix!: add csharp support for DateTime, TimeSpan, Guid (#1612)
* feat!: adds extend in common, meta, and constrained models (#1613)
* feat!: adds inheritance with interfaces for java (#1593)
* chore: remove duplicate version entry for AsyncAPI processor (#1609)
* feat!: render python union in pydantic in the pre 3.10 way (#1626)
* feat: add file path as input (#1601)
* feat!: add options as parameter to constraints (#1667)
* fix: (un)marshalling tuple and dictionary unwrapping for Typescript (#1717)
* fix: jsonbinpack preset and runtime tests (#1718)
* chore: fix linting
* chore: remove unused directory
* feat!: add useJavascriptReservedKeywords option for TS (#1727)
* feat: enable raw properties for interface (#1729)
* fix: newtonsoft could not handle enum values (#1731)
* chore: update Java runtime tests (#1739)
* fix: edge case where references was incorrect handled for JSON Schema (#1754)
* fix: recursion bug for get nearest dependencies (#1757)

---------

Co-authored-by: Kenneth Aasan <[email protected]>
Co-authored-by: jano-petras <[email protected]>
Co-authored-by: Nilkanth Parmar <[email protected]>
  • Loading branch information
4 people authored Feb 1, 2024
1 parent a91b3d0 commit 0aab5b9
Show file tree
Hide file tree
Showing 229 changed files with 4,877 additions and 1,249 deletions.
6 changes: 3 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"security",
"github",
"jest",
"prettier"
"prettier",
"unused-imports"
],
"extends": [
"eslint:recommended",
Expand Down Expand Up @@ -55,7 +56,7 @@
"no-empty-character-class": 2,
"no-self-compare": 2,
"valid-typeof": 2,
"no-unused-vars": 0,
"unused-imports/no-unused-imports": 2,
"handle-callback-err": 2,
"no-shadow-restricted-names": 2,
"no-new-require": 2,
Expand Down Expand Up @@ -138,7 +139,6 @@
"prefer-const": 2,
"prefer-spread": 2,
"prefer-template": 2,
"@typescript-eslint/no-unused-vars": 2,
"prettier/prettier": 2,
"sonarjs/no-identical-functions": "off",
"sonarjs/prefer-single-boolean-return": "off",
Expand Down
9 changes: 8 additions & 1 deletion docs/languages/TypeScript.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ There are special use-cases that each language supports; this document pertains
- [Generate example data function](#generate-example-data-function)
- [Rendering complete models to a specific module system](#rendering-complete-models-to-a-specific-module-system)
- [Rendering comments from description and example fields](#rendering-comments-from-description-and-example-fields)
- [Rendering raw properties for interface](#rendering-raw-properties-for-interface)

<!-- tocstop -->

Expand Down Expand Up @@ -101,4 +102,10 @@ Check out this [example for a live demonstration how to generate the complete Ty
## Rendering comments from description and example fields
You can use the `TS_DESCRIPTION_PRESET` to generate JSDoc style comments from description and example fields in your model.

See [this example](../../examples/typescript-generate-comments) for how this can be used.
See [this example](../../examples/typescript-generate-comments) for how this can be used.

## Rendering raw properties for interface

You can use the `rawPropertyNames: true` and `modelType: 'interface'` together to generate models that use raw properties.

See [this example](../../examples/typescript-generate-raw-properties) for how this can be used.
341 changes: 341 additions & 0 deletions docs/migrations/version-2-to-3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,341 @@
# Migration from v2 to v3

This document contains all the breaking changes and migrations guidelines for adapting your code to the new version.

## allowInheritance set to true will enable inheritance

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).

Here is an example of how to use the new feature and the `allowInheritance` option in your code:

```ts
const generator = new JavaFileGenerator({
processorOptions: {
interpreter: {
allowInheritance: true
}
}
});
```

## TypeScript

### JS reserved keywords are no longer applied by default
By default up until now, JS reserved keywords have been checked for TS as well. Which means that something like:

```
{
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
additionalProperties: false,
properties: {
location: {
type: 'string'
}
}
}
```

Would be default be rendered as:
```ts
class Root {
private _reservedLocation?: string;

constructor(input: {
reservedLocation?: string,
}) {
this._reservedLocation = input.reservedLocation;
}

get reservedLocation(): string | undefined { return this._reservedLocation; }
set reservedLocation(reservedLocation: string | undefined) { this._reservedLocation = reservedLocation; }
}
```

However, without setting `useJavascriptReservedKeywords: true` by default the following will be generated:

```ts
class Root {
private _location?: string;

constructor(input: {
location?: string,
}) {
this._location = input.location;
}

get location(): string | undefined { return this._location; }
set location(location: string | undefined) { this._location = location; }
}
```

## JavaScript

Is not affected by this change.

## C#

### System.TimeSpan is used when format is time

This example used to generate a `string`, but is now instead using `System.TimeSpan`.

```yaml
type: object
properties:
duration:
type: string
format: time
```
will generate
```csharp
public class TestClass {
private System.TimeSpan duration;
...
}
```

### System.DateTime is used when format is date-time

This example used to generate a `string`, but is now instead using `System.DateTime`.

```yaml
type: object
properties:
dob:
type: string
format: date-time
```
will generate
```csharp
public class TestClass {
private System.DateTime dob;
...
}
```

### System.Guid is used when format is uuid

This example used to generate a `string`, but is now instead using `System.Guid`.

```yaml
type: object
properties:
uniqueId:
type: string
format: uuid
```
will generate
```csharp
public class TestClass {
private System.Guid uniqueId;
...
}
```

## Java

### java.time.Duration is used when format is duration

This example used to generate a `String`, but is now instead using `java.time.Duration`.

```yaml
type: object
properties:
duration:
type: string
format: duration
```
will generate
```java
public class TestClass {
private java.time.Duration duration;
...
}
```

### inheritance will generate interfaces

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`:

```yaml
components:
messages:
Vehicle:
payload:
oneOf:
- $ref: '#/components/schemas/Car'
- $ref: '#/components/schemas/Truck'
schemas:
Vehicle:
title: Vehicle
type: object
discriminator: vehicleType
properties:
vehicleType:
title: VehicleType
type: string
length:
type: number
format: float
required:
- vehicleType
Car:
allOf:
- '#/components/schemas/Vehicle'
- type: object
properties:
vehicleType:
const: Car
Truck:
allOf:
- '#/components/schemas/Vehicle'
- type: object
properties:
vehicleType:
const: Truck
```
will generate
```java
public interface NewVehicle {
VehicleType getVehicleType();
}

public class Car implements NewVehicle, Vehicle {
private final VehicleType vehicleType = VehicleType.CAR;
private Float length;
private Map<String, Object> additionalProperties;

public VehicleType getVehicleType() { return this.vehicleType; }

@Override
public Float getLength() { return this.length; }
@Override
public void setLength(Float length) { this.length = length; }
}

public enum VehicleType {
CAR((String)\\"Car\\"), TRUCK((String)\\"Truck\\");

private String value;

VehicleType(String value) {
this.value = value;
}

public String getValue() {
return value;
}

public static VehicleType fromValue(String value) {
for (VehicleType e : VehicleType.values()) {
if (e.value.equals(value)) {
return e;
}
}
throw new IllegalArgumentException(\\"Unexpected value '\\" + value + \\"'\\");
}

@Override
public String toString() {
return String.valueOf(value);
}
}

public interface Vehicle {
public Float getLength();
public void setLength(Float length);
}

public class Truck implements NewVehicle, Vehicle {
private final VehicleType vehicleType = VehicleType.TRUCK;
private Float length;
private Map<String, Object> additionalProperties;

public VehicleType getVehicleType() { return this.vehicleType; }

@Override
public Float getLength() { return this.length; }
@Override
public void setLength(Float length) { this.length = length; }
}
```

## Kotlin

Is not affected by this change.

## Rust

Is not affected by this change.

## Python

### Union type for the Pydantic preset supports Python pre 3.10

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:

```yaml
title: UnionTest
type: object
properties:
unionTest:
oneOf:
- title: Union1
type: object
properties:
testProp1:
type: string
- title: Union2
type: object
properties:
testProp2:
type: string
```
will generate
```python
class UnionTest(BaseModel):
unionTest: Optional[Union[Union1, Union2]] = Field()
additionalProperties: Optional[dict[Any, Any]] = Field()

class Union1(BaseModel):
testProp1: Optional[str] = Field()
additionalProperties: Optional[dict[Any, Any]] = Field()

class Union2(BaseModel):
testProp2: Optional[str] = Field()
additionalProperties: Optional[dict[Any, Any]] = Field()
```
## Go
Is not affected by this change.
## Dart
Is not affected by this change.
## C++
Is not affected by this change.
## Options in constraints
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.
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:
```ts
import { <language>ConstantConstraint, <language>EnumKeyConstraint, <language>EnumValueConstraint, <language>ModelNameConstraint, <language>PropertyKeyConstraint } from @asyncapi/modelina
```
3 changes: 3 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ These are all specific examples only relevant to the TypeScript generator:
- [typescript-use-esm](./typescript-use-esm) - A basic example that generate the models to use ESM module system.
- [typescript-use-cjs](./typescript-use-cjs) - A basic example that generate the models to use CJS module system.
- [typescript-generate-jsonbinpack](./typescript-generate-jsonbinpack) - A basic example showing how to generate models that include [jsonbinpack](https://github.com/sourcemeta/jsonbinpack) functionality.
- [typescript-generate-raw-properties](./typescript-generate-raw-properties) - A basic example showing how to generate models that use raw properties for interface.
- [typescript-use-js-reserved-keyword](./typescript-use-js-reserved-keyword) - A basic example showing how you can generate the models that take reserved JS keywords into account for model names, properties or enum keys.


### Kotlin
These are all specific examples only relevant to the Kotlin generator:
Expand Down
Loading

0 comments on commit 0aab5b9

Please sign in to comment.