Skip to content

Commit

Permalink
fix!: add csharp support for DateTime, TimeSpan, Guid (#1612)
Browse files Browse the repository at this point in the history
  • Loading branch information
jano-petras authored Nov 17, 2023
1 parent ef56f3e commit 3aa1026
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 4 deletions.
63 changes: 62 additions & 1 deletion docs/migrations/version-2-to-3.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,68 @@ Is not affected by this change.

### C#

Is not affected by this change.
#### 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

Expand Down
21 changes: 21 additions & 0 deletions examples/generate-csharp-models/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,33 @@ Array [
"public class Root
{
private string? email;
private System.DateTime? today;
private System.TimeSpan? duration;
private System.Guid? userId;
public string? Email
{
get { return email; }
set { email = value; }
}
public System.DateTime? Today
{
get { return today; }
set { today = value; }
}
public System.TimeSpan? Duration
{
get { return duration; }
set { duration = value; }
}
public System.Guid? UserId
{
get { return userId; }
set { userId = value; }
}
}",
]
`;
12 changes: 12 additions & 0 deletions examples/generate-csharp-models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ const jsonSchemaDraft7 = {
email: {
type: 'string',
format: 'email'
},
today: {
type: 'string',
format: 'date-time'
},
duration: {
type: 'string',
format: 'time'
},
userId: {
type: 'string',
format: 'uuid'
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion examples/generate-csharp-models/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions src/generators/csharp/CSharpConstrainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,19 @@ export const CSharpDefaultTypeMapping: CSharpTypeMapping = {
Integer({ partOfProperty }): string {
return getFullTypeDefinition('int', partOfProperty);
},
String({ partOfProperty }): string {
return getFullTypeDefinition('string', partOfProperty);
String({ constrainedModel, partOfProperty }): string {
switch (constrainedModel.options.format) {
case 'time':
return getFullTypeDefinition('System.TimeSpan', partOfProperty);
case 'date':
case 'dateTime':
case 'date-time':
return getFullTypeDefinition('System.DateTime', partOfProperty);
case 'uuid':
return getFullTypeDefinition('System.Guid', partOfProperty);
default:
return getFullTypeDefinition('string', partOfProperty);
}
},
Boolean({ partOfProperty }): string {
return getFullTypeDefinition('bool', partOfProperty);
Expand Down
15 changes: 15 additions & 0 deletions test/blackbox/docs/AsyncAPI-2_6/dummy.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@
"type": "string",
"format": "email",
"description": "Email of the user"
},
"signedUpAt": {
"type": "string",
"format": "date-time",
"description": "Sign-up processed date and time"
},
"sessionDuration": {
"type": "string",
"format": "time",
"description": "Maximum session duration"
},
"uniqueUserId": {
"type": "string",
"format": "uuid",
"description": "Unique user id"
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions test/generators/csharp/CSharpConstrainer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,45 @@ describe('CSharpConstrainer', () => {
});
expect(type).toEqual('string');
});
test('should render System.DateTime', () => {
const model = new ConstrainedStringModel(
'test',
undefined,
{ format: 'date-time' },
''
);
const type = CSharpDefaultTypeMapping.String({
constrainedModel: model,
...defaultOptions
});
expect(type).toEqual('System.DateTime');
});
test('should render TimeSpan', () => {
const model = new ConstrainedStringModel(
'test',
undefined,
{ format: 'time' },
''
);
const type = CSharpDefaultTypeMapping.String({
constrainedModel: model,
...defaultOptions
});
expect(type).toEqual('System.TimeSpan');
});
test('should render Guid', () => {
const model = new ConstrainedStringModel(
'test',
undefined,
{ format: 'uuid' },
''
);
const type = CSharpDefaultTypeMapping.String({
constrainedModel: model,
...defaultOptions
});
expect(type).toEqual('System.Guid');
});
});
describe('Boolean', () => {
test('should render type', () => {
Expand Down

0 comments on commit 3aa1026

Please sign in to comment.