|
1 | | -using NJsonSchema; |
2 | | -using NJsonSchema.CodeGeneration; |
3 | | -using NJsonSchema.CodeGeneration.CSharp; |
4 | | -using NJsonSchema.CodeGeneration.CSharp.Models; |
5 | 1 | using NSwag; |
6 | 2 | using NSwag.CodeGeneration.CSharp; |
7 | 3 | using NSwag.CodeGeneration.CSharp.Models; |
8 | 4 |
|
9 | 5 | namespace Refitter.Core; |
10 | 6 |
|
11 | | -internal class CustomCSharpClientGenerator(OpenApiDocument document, CSharpClientGeneratorSettings settings, bool usePolymorphicSerialization) |
12 | | -#pragma warning disable CS9107 // Parameter is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well. |
| 7 | +internal class CustomCSharpClientGenerator(OpenApiDocument document, CSharpClientGeneratorSettings settings) |
13 | 8 | : CSharpClientGenerator(document, settings) |
14 | | -#pragma warning restore CS9107 // Parameter is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well. |
15 | 9 | { |
16 | 10 | internal CSharpOperationModel CreateOperationModel(OpenApiOperation operation) => |
17 | | - CreateOperationModel(operation, Settings); |
18 | | - |
19 | | - /// <summary> |
20 | | - /// override to generate DTO types with our custom CSharpGenerator |
21 | | - /// This code should be removed when NSwag supports STJ polymorphic serialization |
22 | | - /// </summary> |
23 | | - protected override IEnumerable<CodeArtifact> GenerateDtoTypes() |
24 | | - { |
25 | | - var generator = new CCustomSharpGenerator(document, Settings.CSharpGeneratorSettings, (CSharpTypeResolver)Resolver, usePolymorphicSerialization); |
26 | | - return generator.GenerateTypes(); |
27 | | - } |
28 | | - |
29 | | - private class CCustomSharpGenerator(OpenApiDocument document, CSharpGeneratorSettings settings, CSharpTypeResolver resolver, bool usePolymorphicSerialization) |
30 | | -#pragma warning disable CS9107 // Parameter is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well. |
31 | | - : CSharpGenerator(document, settings, resolver) |
32 | | -#pragma warning restore CS9107 // Parameter is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well. |
33 | | - { |
34 | | - /// <summary> |
35 | | - /// override to generate Class with our custom ClassTemplateModel |
36 | | - /// code is taken from NJsonSchema.CodeGeneration.CSharp.CSharpGenerator.GenerateType |
37 | | - /// </summary> |
38 | | - protected override CodeArtifact GenerateType(JsonSchema schema, string typeNameHint) |
39 | | - { |
40 | | - var typeName = resolver.GetOrGenerateTypeName(schema, typeNameHint); |
41 | | - |
42 | | - if (schema.IsEnumeration) |
43 | | - { |
44 | | - return base.GenerateType(schema, typeName); |
45 | | - } |
46 | | - else |
47 | | - { |
48 | | - return GenerateClass(schema, typeName); |
49 | | - } |
50 | | - } |
51 | | - |
52 | | - /// <summary> |
53 | | - /// override to generate JsonInheritanceAttribute, JsonInheritanceConverter with our custom template models |
54 | | - /// code is taken from NJsonSchema.CodeGeneration.CSharp.CSharpGenerator.GenerateTypes |
55 | | - /// </summary> |
56 | | - public override IEnumerable<CodeArtifact> GenerateTypes() |
57 | | - { |
58 | | - var baseArtifacts = base.GenerateTypes(); |
59 | | - var artifacts = new List<CodeArtifact>(); |
60 | | - |
61 | | - if (baseArtifacts.Any(r => r.Code.Contains("JsonInheritanceConverter"))) |
62 | | - { |
63 | | - if (Settings.ExcludedTypeNames?.Contains("JsonInheritanceAttribute") != true) |
64 | | - { |
65 | | - var template = Settings.TemplateFactory.CreateTemplate("CSharp", "JsonInheritanceAttribute", new CustomJsonInheritanceConverterTemplateModel(Settings, usePolymorphicSerialization)); |
66 | | - artifacts.Add(new CodeArtifact("JsonInheritanceAttribute", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Utility, template)); |
67 | | - } |
68 | | - |
69 | | - if (Settings.ExcludedTypeNames?.Contains("JsonInheritanceConverter") != true) |
70 | | - { |
71 | | - var template = Settings.TemplateFactory.CreateTemplate("CSharp", "JsonInheritanceConverter", new CustomJsonInheritanceConverterTemplateModel(Settings, usePolymorphicSerialization)); |
72 | | - artifacts.Add(new CodeArtifact("JsonInheritanceConverter", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Utility, template)); |
73 | | - } |
74 | | - } |
75 | | - |
76 | | - return baseArtifacts.Concat(artifacts); |
77 | | - } |
78 | | - |
79 | | - /// <summary> |
80 | | - /// Code is taken from NJsonSchema.CodeGeneration.CSharp.CSharpGenerator.GenerateClass |
81 | | - /// to instantiate our custom ClassTemplateModel |
82 | | - /// </summary> |
83 | | - private CodeArtifact GenerateClass(JsonSchema schema, string typeName) |
84 | | - { |
85 | | - var model = new CustomClassTemplateModel(typeName, Settings, resolver, schema, RootObject, usePolymorphicSerialization); |
86 | | - |
87 | | - RenamePropertyWithSameNameAsClass(typeName, model.Properties); |
88 | | - |
89 | | - var template = Settings.TemplateFactory.CreateTemplate("CSharp", "Class", model); |
90 | | - return new CodeArtifact(typeName, model.BaseClassName, CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Contract, template); |
91 | | - } |
92 | | - |
93 | | - /// <summary> |
94 | | - /// Code is taken from NJsonSchema.CodeGeneration.CSharp.CSharpGenerator.RenamePropertyWithSameNameAsClass |
95 | | - /// </summary> |
96 | | - private static void RenamePropertyWithSameNameAsClass(string typeName, IEnumerable<PropertyModel> properties) |
97 | | - { |
98 | | - var propertyModels = properties as PropertyModel[] ?? properties.ToArray(); |
99 | | - PropertyModel? propertyWithSameNameAsClass = null; |
100 | | - foreach (var p in propertyModels) |
101 | | - { |
102 | | - if (p.PropertyName == typeName) |
103 | | - { |
104 | | - propertyWithSameNameAsClass = p; |
105 | | - break; |
106 | | - } |
107 | | - } |
108 | | - |
109 | | - if (propertyWithSameNameAsClass != null) |
110 | | - { |
111 | | - var number = 1; |
112 | | - var candidate = typeName + number; |
113 | | - while (propertyModels.Any(p => p.PropertyName == candidate)) |
114 | | - { |
115 | | - number++; |
116 | | - } |
117 | | - |
118 | | - propertyWithSameNameAsClass.PropertyName = propertyWithSameNameAsClass.PropertyName + number; |
119 | | - } |
120 | | - } |
121 | | - |
122 | | - /// <summary> |
123 | | - /// finally, our custom ClassTemplateModel and CustomJsonInheritanceConverterTemplateModel |
124 | | - /// to have access to UsePolymorphicSerialization |
125 | | - /// This code should be removed when NSwag supports STJ polymorphic serialization |
126 | | - /// </summary> |
127 | | - private class CustomClassTemplateModel(string typeName, CSharpGeneratorSettings settings, CSharpTypeResolver resolver, JsonSchema schema, object rootObject, bool usePolymorphicSerialization) |
128 | | - : ClassTemplateModel(typeName, settings, resolver, schema, rootObject) |
129 | | - { |
130 | | - public bool UsePolymorphicSerialization => usePolymorphicSerialization; |
131 | | - } |
132 | | - |
133 | | - private class CustomJsonInheritanceConverterTemplateModel(CSharpGeneratorSettings settings, bool usePolymorphicSerialization) |
134 | | - : JsonInheritanceConverterTemplateModel(settings) |
135 | | - { |
136 | | - public bool UsePolymorphicSerialization => usePolymorphicSerialization; |
137 | | - } |
138 | | - } |
| 11 | + base.CreateOperationModel(operation, Settings); |
139 | 12 | } |
0 commit comments