Skip to content

Commit 9efe151

Browse files
Fix missing inheritance to IData and JSON Seriliazation supported
1 parent 97d621b commit 9efe151

354 files changed

Lines changed: 17644 additions & 30890 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="UmlCoreJsonDtoSerializerGeneratorTestFixture.cs" company="Starion Group S.A.">
3+
//
4+
// Copyright 2022-2025 Starion Group S.A.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// </copyright>
19+
// ------------------------------------------------------------------------------------------------
20+
21+
namespace SysML2.NET.CodeGenerator.Tests.Generators.UmlHandleBarsGenerators
22+
{
23+
using System.IO;
24+
using System.Threading.Tasks;
25+
26+
using NUnit.Framework;
27+
28+
using SysML2.NET.CodeGenerator.Generators.UmlHandleBarsGenerators;
29+
30+
[TestFixture]
31+
public class UmlCoreJsonDtoSerializerGeneratorTestFixture
32+
{
33+
private DirectoryInfo umlPocoDirectoryInfo;
34+
private UmlCoreJsonDtoSerializerGenerator umlCoreDtoSerializerGenerator;
35+
36+
[OneTimeSetUp]
37+
public void OneTimeSetup()
38+
{
39+
var directoryInfo = new DirectoryInfo(TestContext.CurrentContext.TestDirectory);
40+
41+
var path = Path.Combine("UML", "_SysML2.NET.Serializer.Json.Core.AutoGenSerializer");
42+
43+
this.umlPocoDirectoryInfo = directoryInfo.CreateSubdirectory(path);
44+
this.umlCoreDtoSerializerGenerator = new UmlCoreJsonDtoSerializerGenerator();
45+
}
46+
47+
[Test]
48+
public async Task VerifyCanGenerateDtoJsonSerializer()
49+
{
50+
await Assert.ThatAsync(() => this.umlCoreDtoSerializerGenerator.GenerateAsync(GeneratorSetupFixture.XmiReaderResult, this.umlPocoDirectoryInfo), Throws.Nothing);
51+
}
52+
}
53+
}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="UmlCoreJsonDtoSerializerGenerator.cs" company="Starion Group S.A.">
3+
//
4+
// Copyright 2022-2025 Starion Group S.A.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// </copyright>
19+
// ------------------------------------------------------------------------------------------------
20+
21+
namespace SysML2.NET.CodeGenerator.Generators.UmlHandleBarsGenerators
22+
{
23+
using System;
24+
using System.IO;
25+
using System.Linq;
26+
using System.Threading.Tasks;
27+
28+
using uml4net.Extensions;
29+
using uml4net.HandleBars;
30+
using uml4net.StructuredClassifiers;
31+
using uml4net.xmi.Readers;
32+
33+
using NamedElementHelper = SysML2.NET.CodeGenerator.HandleBarHelpers.NamedElementHelper;
34+
using PropertyHelper = SysML2.NET.CodeGenerator.HandleBarHelpers.PropertyHelper;
35+
36+
/// <summary>
37+
/// A UML Handlebars based DTO Json Serializer code generator
38+
/// </summary>
39+
public class UmlCoreJsonDtoSerializerGenerator : UmlHandleBarsGenerator
40+
{
41+
/// <summary>
42+
/// Gets the name of the template used to generate DTO Json Serializaer
43+
/// </summary>
44+
private const string DtoSerializerTemplateName = "core-json-dto-serializer-uml-template";
45+
46+
/// <summary>
47+
/// Gets the name of the template used to generate Json Serializer provider
48+
/// </summary>
49+
private const string DtoSerializerProviderTemplateName = "core-json-dto-serialization-provider-uml-template";
50+
51+
/// <summary>
52+
/// Generates code specific to the concrete implementation
53+
/// </summary>
54+
/// <param name="xmiReaderResult">
55+
/// the <see cref="XmiReaderResult" /> that contains the UML model to generate from
56+
/// </param>
57+
/// <param name="outputDirectory">
58+
/// The target <see cref="DirectoryInfo" />
59+
/// </param>
60+
/// <returns>
61+
/// an awaitable <see cref="Task" />
62+
/// </returns>
63+
public override async Task GenerateAsync(XmiReaderResult xmiReaderResult, DirectoryInfo outputDirectory)
64+
{
65+
await this.GenerateDtoJsonSerializer(xmiReaderResult, outputDirectory);
66+
await this.GenerateSerializationProvider(xmiReaderResult, outputDirectory);
67+
}
68+
69+
/// <summary>
70+
/// Register the custom helpers
71+
/// </summary>
72+
protected override void RegisterHelpers()
73+
{
74+
this.Handlebars.RegisterStringHelper();
75+
this.Handlebars.RegisterClassHelper();
76+
this.Handlebars.RegisterPropertyHelper();
77+
78+
NamedElementHelper.RegisterNamedElementHelper(this.Handlebars);
79+
PropertyHelper.RegisterPropertyHelper(this.Handlebars);
80+
}
81+
82+
/// <summary>
83+
/// Register the code templates
84+
/// </summary>
85+
protected override void RegisterTemplates()
86+
{
87+
this.RegisterTemplate(DtoSerializerTemplateName);
88+
this.RegisterTemplate(DtoSerializerProviderTemplateName);
89+
}
90+
91+
/// <summary>
92+
/// Generates the Serialization Provider class
93+
/// </summary>
94+
/// <param name="xmiReaderResult">the <see cref="XmiReaderResult" /> that contains the UML model to generate from</param>
95+
/// <param name="outputDirectory">The target <see cref="DirectoryInfo" /></param>
96+
/// <returns>
97+
/// an awaitable <see cref="Task" />
98+
/// </returns>
99+
/// <exception cref="ArgumentNullException">
100+
/// In case of null value for <paramref name="xmiReaderResult" /> or
101+
/// <paramref name="outputDirectory" />
102+
/// </exception>
103+
private Task GenerateSerializationProvider(XmiReaderResult xmiReaderResult, DirectoryInfo outputDirectory)
104+
{
105+
ArgumentNullException.ThrowIfNull(xmiReaderResult);
106+
ArgumentNullException.ThrowIfNull(outputDirectory);
107+
108+
return this.GenerateSerializationProviderInternal(xmiReaderResult, outputDirectory);
109+
}
110+
111+
/// <summary>
112+
/// Generates the Serialization Provider class
113+
/// </summary>
114+
/// <param name="xmiReaderResult">the <see cref="XmiReaderResult" /> that contains the UML model to generate from</param>
115+
/// <param name="outputDirectory">The target <see cref="DirectoryInfo" /></param>
116+
/// <returns>
117+
/// an awaitable <see cref="Task" />
118+
/// </returns>
119+
private async Task GenerateSerializationProviderInternal(XmiReaderResult xmiReaderResult, DirectoryInfo outputDirectory)
120+
{
121+
var template = this.Templates[DtoSerializerProviderTemplateName];
122+
123+
var classes = xmiReaderResult.Root.QueryPackages()
124+
.SelectMany(x => x.PackagedElement.OfType<IClass>())
125+
.Where(x => !x.IsAbstract)
126+
.OrderBy(x => x.Name)
127+
.ToList();
128+
129+
var generatedSerializationProvider = template(classes);
130+
131+
generatedSerializationProvider = this.CodeCleanup(generatedSerializationProvider);
132+
133+
const string fileName = "SerializationProvider.cs";
134+
135+
await Write(generatedSerializationProvider, outputDirectory, fileName);
136+
}
137+
138+
/// <summary>
139+
/// Generates DTO Json Serializer files
140+
/// </summary>
141+
/// <param name="xmiReaderResult">the <see cref="XmiReaderResult" /> that contains the UML model to generate from</param>
142+
/// <param name="outputDirectory">The target <see cref="DirectoryInfo" /></param>
143+
/// <returns>
144+
/// an awaitable <see cref="Task" />
145+
/// </returns>
146+
/// <exception cref="ArgumentNullException">
147+
/// In case of null value for <paramref name="xmiReaderResult" /> or
148+
/// <paramref name="outputDirectory" />
149+
/// </exception>
150+
private Task GenerateDtoJsonSerializer(XmiReaderResult xmiReaderResult, DirectoryInfo outputDirectory)
151+
{
152+
ArgumentNullException.ThrowIfNull(xmiReaderResult);
153+
ArgumentNullException.ThrowIfNull(outputDirectory);
154+
155+
return this.GenerateDtoJsonSerializerInternal(xmiReaderResult, outputDirectory);
156+
}
157+
158+
/// <summary>
159+
/// Generates DTO Json Serializer files
160+
/// </summary>
161+
/// <param name="xmiReaderResult">the <see cref="XmiReaderResult" /> that contains the UML model to generate from</param>
162+
/// <param name="outputDirectory">The target <see cref="DirectoryInfo" /></param>
163+
/// <returns>
164+
/// an awaitable <see cref="Task" />
165+
/// </returns>
166+
private async Task GenerateDtoJsonSerializerInternal(XmiReaderResult xmiReaderResult, DirectoryInfo outputDirectory)
167+
{
168+
var template = this.Templates[DtoSerializerTemplateName];
169+
170+
var classes = xmiReaderResult.Root.QueryPackages()
171+
.SelectMany(x => x.PackagedElement.OfType<IClass>())
172+
.Where(x => !x.IsAbstract)
173+
.ToList();
174+
175+
foreach (var @class in classes)
176+
{
177+
var generatedInterface = template(@class);
178+
179+
generatedInterface = this.CodeCleanup(generatedInterface);
180+
181+
var fileName = $"{@class.Name.CapitalizeFirstLetter()}Serializer.cs";
182+
183+
await WriteAsync(generatedInterface, outputDirectory, fileName);
184+
}
185+
}
186+
}
187+
}

SysML2.NET.CodeGenerator/HandleBarHelpers/PropertyHelper.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public static void RegisterPropertyHelper(this IHandlebars handlebars)
312312

313313
if (!hasSameNameAsClass && !classHaveToImplementTwiceSameProperty)
314314
{
315-
sb.AppendLine(property.Visibility.ToString().ToLower(CultureInfo.InvariantCulture));
315+
sb.Append(property.Visibility.ToString().ToLower(CultureInfo.InvariantCulture));
316316
sb.Append(' ');
317317
}
318318

@@ -368,6 +368,33 @@ public static void RegisterPropertyHelper(this IHandlebars handlebars)
368368

369369
writer.WriteSafeString(sb + Environment.NewLine);
370370
});
371+
372+
handlebars.RegisterHelper("Property.ContainsPropertyRedifinitionWithSameName", (context, arguments) =>
373+
{
374+
if (context.Value is not IProperty property)
375+
{
376+
throw new ArgumentException("The #Property.ContainsPropertyRedifinitionWithSameName context supposed to be IProperty");
377+
}
378+
379+
if (arguments.Length != 2)
380+
{
381+
throw new ArgumentException("The #Property.ContainsPropertyRedifinitionWithSameName supposed to be have 2 arguments IProperty");
382+
}
383+
384+
if (arguments[1] is not IClass generatedClass)
385+
{
386+
throw new ArgumentException("The #Property.ContainsPropertyRedifinitionWithSameName supposed to be have an IClass as second argument");
387+
}
388+
389+
if (!property.QueryIsRedefined())
390+
{
391+
return false;
392+
}
393+
394+
var allProperties = generatedClass.QueryAllProperties();
395+
396+
return allProperties.Any(x => x.RedefinedProperty.Any(p => p.XmiGuid == property.XmiGuid) && x.Name == property.Name);
397+
});
371398
}
372399
}
373400
}

SysML2.NET.CodeGenerator/SysML2.NET.CodeGenerator.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,15 @@
206206
<None Update="Templates\core-poco-class-uml-template.hbs">
207207
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
208208
</None>
209+
<None Update="Templates\core-json-dto-serializer-uml-template.hbs">
210+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
211+
</None>
212+
<None Update="Templates\core-json-dto-serialization-provider-uml-template.hbs">
213+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
214+
</None>
215+
<None Update="Templates\core-json-dto-deserializer-uml-template.hbs">
216+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
217+
</None>
209218
</ItemGroup>
210219

211220
<ItemGroup>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="SerializationProvider.cs" company="Starion Group S.A.">
3+
//
4+
// Copyright 2022-2025 Starion Group S.A.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// </copyright>
19+
// ------------------------------------------------------------------------------------------------
20+
21+
// ------------------------------------------------------------------------------------------------
22+
// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!--------
23+
// ------------------------------------------------------------------------------------------------
24+
25+
namespace SysML2.NET.Serializer.Json.Core.DTO
26+
{
27+
using System;
28+
using System.Collections.Generic;
29+
using System.Text.Json;
30+
31+
using SysML2.NET.Serializer.Json;
32+
33+
/// <summary>
34+
/// Delegate provider for the appropriate serialization method to serialize a <see cref="Type" />
35+
/// </summary>
36+
internal static class SerializationProvider
37+
{
38+
/// <summary>
39+
/// Caches the delegate <see cref="Action{object, Utf8JsonWriter, SerializationModeKind}"/> for the
40+
/// <see cref="System.Type"/> that is to be serialized
41+
/// </summary>
42+
private static readonly Dictionary<System.Type, Action<object, Utf8JsonWriter, SerializationModeKind>> SerializerActionMap = new Dictionary<System.Type, Action<object, Utf8JsonWriter, SerializationModeKind>>
43+
{
44+
{{#each this as | eClass |}}
45+
{ typeof(SysML2.NET.Core.DTO.{{ #NamedElement.WriteFullyQualifiedNameSpace this }}.{{ eClass.Name }}), {{ eClass.Name }}Serializer.Serialize },
46+
{{/each}}
47+
};
48+
49+
/// <summary>
50+
/// Provides the delegate <see cref="Action{object, Utf8JsonWriter, SerializationModeKind}"/> for the
51+
/// <see cref="System.Type"/> that is to be serialized
52+
/// </summary>
53+
/// <param name="type">
54+
/// The subject <see cref="System.Type"/> that is to be serialized
55+
/// </param>
56+
/// <returns>
57+
/// A Delegate of <see cref="Action{object, Utf8JsonWriter, SerializationModeKind}"/>
58+
/// </returns>
59+
/// <exception cref="NotSupportedException">
60+
/// Thrown when the <see cref="System.Type"/> is not supported.
61+
/// </exception>
62+
internal static Action<object, Utf8JsonWriter, SerializationModeKind> Provide(System.Type type)
63+
{
64+
return !SerializerActionMap.TryGetValue(type, out var action) ? throw new NotSupportedException($"The {type.Name} is not supported by the SerializationProvider.") : action;
65+
}
66+
67+
/// <summary>
68+
/// Asserts whether the <paramref name="type"/> is supported by the provider
69+
/// </summary>
70+
/// <param name="type">
71+
/// The <see cref="System.Type"/> for which support is asserted
72+
/// </param>
73+
/// <returns></returns>
74+
internal static bool IsTypeSupported(System.Type type)
75+
{
76+
return SerializerActionMap.ContainsKey(type);
77+
}
78+
}
79+
}
80+
81+
// ------------------------------------------------------------------------------------------------
82+
// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!--------
83+
// ------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)