diff --git a/docs/migrations/version-2-to-3.md b/docs/migrations/version-2-to-3.md index 68186b8c1c..e32f012407 100644 --- a/docs/migrations/version-2-to-3.md +++ b/docs/migrations/version-2-to-3.md @@ -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 diff --git a/examples/generate-csharp-models/__snapshots__/index.spec.ts.snap b/examples/generate-csharp-models/__snapshots__/index.spec.ts.snap index 8f4d5cb935..aa5cb03b9b 100644 --- a/examples/generate-csharp-models/__snapshots__/index.spec.ts.snap +++ b/examples/generate-csharp-models/__snapshots__/index.spec.ts.snap @@ -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; } + } }", ] `; diff --git a/examples/generate-csharp-models/index.ts b/examples/generate-csharp-models/index.ts index cccf80e255..48ac558a6b 100644 --- a/examples/generate-csharp-models/index.ts +++ b/examples/generate-csharp-models/index.ts @@ -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' } } }; diff --git a/examples/generate-csharp-models/package-lock.json b/examples/generate-csharp-models/package-lock.json index 56ab4e873b..c6c4ae4e1d 100644 --- a/examples/generate-csharp-models/package-lock.json +++ b/examples/generate-csharp-models/package-lock.json @@ -1,5 +1,5 @@ { - "name": "typescript-interface", + "name": "generate-csharp-models", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/src/generators/csharp/CSharpConstrainer.ts b/src/generators/csharp/CSharpConstrainer.ts index 463cf8b784..912dcc08fa 100644 --- a/src/generators/csharp/CSharpConstrainer.ts +++ b/src/generators/csharp/CSharpConstrainer.ts @@ -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); diff --git a/test/blackbox/docs/AsyncAPI-2_6/dummy.json b/test/blackbox/docs/AsyncAPI-2_6/dummy.json index 1ed6f19df8..46cfbbd6b8 100644 --- a/test/blackbox/docs/AsyncAPI-2_6/dummy.json +++ b/test/blackbox/docs/AsyncAPI-2_6/dummy.json @@ -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" } } } diff --git a/test/generators/csharp/CSharpConstrainer.spec.ts b/test/generators/csharp/CSharpConstrainer.spec.ts index e9285dc01a..26fbb989d9 100644 --- a/test/generators/csharp/CSharpConstrainer.spec.ts +++ b/test/generators/csharp/CSharpConstrainer.spec.ts @@ -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', () => {