Skip to content

Commit

Permalink
Merge pull request microsoft#4652 from Balkoth/issue_4625
Browse files Browse the repository at this point in the history
Change C# code generation to follow the usual C# code style
  • Loading branch information
baywet authored May 13, 2024
2 parents 7a66465 + aa859f2 commit dd7c9ad
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed incorrect optional types in method parameters in Python [#4507](https://github.com/microsoft/kiota/issues/4507)
- Changed enum parsing methods to return nil in the default case in Go [#4621](https://github.com/microsoft/kiota/issues/4621)
- Changes the cached description file name to `openapi.yml|json` from `description.yml|json` [#4641](https://github.com/microsoft/kiota/issues/4641)
- Put opening brace after namespace definition on new line [#4625](https://github.com/microsoft/kiota/issues/4625)
- Put opening brace after property definition on new line, if property has getter and setter [#4625](https://github.com/microsoft/kiota/issues/4625)
- Put spaces correctly around dictionary entries [#4625](https://github.com/microsoft/kiota/issues/4625)
- Remove trailing space after class definition [#4625](https://github.com/microsoft/kiota/issues/4625)

## [1.14.0] - 2024-05-02

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWrit
.OrderBy(static x => x, StringComparer.Ordinal)
.ToList()
.ForEach(x => writer.WriteLine(x));
writer.StartBlock($"namespace {codeElement.Parent.Parent.Name} {{");
writer.WriteLine($"namespace {codeElement.Parent.Parent.Name}");
writer.StartBlock();
}

var derivedTypes = (codeElement.Inherits is null ? Enumerable.Empty<string?>() : new string?[] { conventions.GetTypeString(codeElement.Inherits, parentClass) })
.Union(codeElement.Implements.Select(static x => x.Name))
.OfType<string>()
.Select(static x => x.ToFirstCharacterUpperCase())
.ToArray();
var derivation = derivedTypes.Length != 0 ? ": " + derivedTypes.Aggregate(static (x, y) => $"{x}, {y}") + " " : string.Empty;
var derivation = derivedTypes.Length != 0 ? ": " + derivedTypes.Aggregate(static (x, y) => $"{x}, {y}") : string.Empty;
bool hasDescription = conventions.WriteLongDescription(parentClass, writer);
conventions.WriteDeprecationAttribute(parentClass, writer);
if (!hasDescription) writer.WriteLine("#pragma warning disable CS1591");
Expand Down
3 changes: 2 additions & 1 deletion src/Kiota.Builder/Writers/CSharp/CodeEnumWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter write
.Distinct(StringComparer.Ordinal)
.OrderBy(static x => x, StringComparer.Ordinal))
writer.WriteLine(x);
writer.StartBlock($"namespace {codeNamespace.Name} {{");
writer.WriteLine($"namespace {codeNamespace.Name}");
writer.StartBlock();
}
bool hasDescription = conventions.WriteShortDescription(codeElement, writer);
if (codeElement.Flags)
Expand Down
4 changes: 2 additions & 2 deletions src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ private void WriteDeserializerBodyForInheritedModel(bool shouldHide, CodeMethod
.Where(static x => !x.ExistsInBaseType)
.OrderBy(static x => x.Name, StringComparer.Ordinal))
{
writer.WriteLine($"{{\"{otherProp.WireName}\", n => {{ {otherProp.Name.ToFirstCharacterUpperCase()} = n.{GetDeserializationMethodName(otherProp.Type, codeElement)}; }} }},");
writer.WriteLine($"{{ \"{otherProp.WireName}\", n => {{ {otherProp.Name.ToFirstCharacterUpperCase()} = n.{GetDeserializationMethodName(otherProp.Type, codeElement)}; }} }},");
}
writer.CloseBlock("};");
}
Expand Down Expand Up @@ -381,7 +381,7 @@ protected void WriteRequestExecutorBody(CodeMethod codeElement, RequestParams re
writer.StartBlock();
foreach (var errorMapping in codeElement.ErrorMappings.Where(errorMapping => errorMapping.Value.AllTypes.FirstOrDefault()?.TypeDefinition is CodeClass))
{
writer.WriteLine($"{{\"{errorMapping.Key.ToUpperInvariant()}\", {conventions.GetTypeString(errorMapping.Value, codeElement, false)}.CreateFromDiscriminatorValue}},");
writer.WriteLine($"{{ \"{errorMapping.Key.ToUpperInvariant()}\", {conventions.GetTypeString(errorMapping.Value, codeElement, false)}.CreateFromDiscriminatorValue }},");
}
writer.CloseBlock("};");
}
Expand Down
7 changes: 3 additions & 4 deletions src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,11 @@ private void WritePropertyInternal(CodeProperty codeElement, LanguageWriter writ
var backingStoreKey = codeElement.WireName;
var nullableOp = !codeElement.IsOfKind(CodePropertyKind.AdditionalData) ? "?" : string.Empty;
var defaultPropertyValue = codeElement.IsOfKind(CodePropertyKind.AdditionalData) ? " ?? new Dictionary<string, object>()" : string.Empty;
writer.WriteLine($"{conventions.GetAccessModifier(codeElement.Access)} {propertyType} {codeElement.Name.ToFirstCharacterUpperCase()} {{");
writer.IncreaseIndent();
writer.WriteLine($"{conventions.GetAccessModifier(codeElement.Access)} {propertyType} {codeElement.Name.ToFirstCharacterUpperCase()}");
writer.StartBlock();
writer.WriteLine($"get {{ return {backingStoreProperty.Name.ToFirstCharacterUpperCase()}{nullableOp}.Get<{propertyType}>(\"{backingStoreKey}\"){defaultPropertyValue}; }}");
writer.WriteLine($"set {{ {backingStoreProperty.Name.ToFirstCharacterUpperCase()}{nullableOp}.Set(\"{backingStoreKey}\", value); }}");
writer.DecreaseIndent();
writer.WriteLine("}");
writer.CloseBlock();
break;
case CodePropertyKind.ErrorMessageOverride when parentClass.IsErrorDefinition:
if (parentClass.GetPrimaryMessageCodePath(static x => x.Name.ToFirstCharacterUpperCase(), static x => x.Name.ToFirstCharacterUpperCase(), "?.") is string primaryMessageCodePath && !string.IsNullOrEmpty(primaryMessageCodePath))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,9 @@ public void WritesRequestExecutorBody()
Assert.Contains("var requestInfo", result);
Assert.Contains("var errorMapping = new Dictionary<string, ParsableFactory<IParsable>>", result);
Assert.Contains("<exception cref=", result);
Assert.Contains("{\"4XX\", Error4XX.CreateFromDiscriminatorValue},", result);
Assert.Contains("{\"5XX\", Error5XX.CreateFromDiscriminatorValue},", result);
Assert.Contains("{\"401\", Error401.CreateFromDiscriminatorValue},", result);
Assert.Contains("{ \"4XX\", Error4XX.CreateFromDiscriminatorValue },", result);
Assert.Contains("{ \"5XX\", Error5XX.CreateFromDiscriminatorValue },", result);
Assert.Contains("{ \"401\", Error401.CreateFromDiscriminatorValue },", result);
Assert.Contains("SendAsync", result);
Assert.Contains($"{ReturnTypeName}.CreateFromDiscriminatorValue", result);
Assert.Contains(AsyncKeyword, result);
Expand Down Expand Up @@ -514,7 +514,7 @@ public void WritesRequestExecutorBodyForCollection()
var result = tw.ToString();
Assert.Contains("var requestInfo", result);
Assert.Contains("var errorMapping = new Dictionary<string, ParsableFactory<IParsable>>", result);
Assert.Contains("{\"4XX\", Error4XX.CreateFromDiscriminatorValue},", result);
Assert.Contains("{ \"4XX\", Error4XX.CreateFromDiscriminatorValue },", result);
Assert.Contains("SendCollectionAsync", result);
Assert.Contains("return collectionResult?.ToList()", result);
Assert.Contains($"{ReturnTypeName}.CreateFromDiscriminatorValue", result);
Expand Down Expand Up @@ -1137,8 +1137,8 @@ public void WritesDeSerializerBody()
Assert.Contains("GetCollectionOfObjectValues", result);
Assert.Contains("GetEnumValue", result);
Assert.Contains("definedInParent", result, StringComparison.OrdinalIgnoreCase);
Assert.Contains("{\"DummyUCaseProp", result);
Assert.Contains("{\"dummyProp", result);
Assert.Contains("{ \"DummyUCaseProp", result);
Assert.Contains("{ \"dummyProp", result);
}
[Fact]
public void WritesInheritedSerializerBody()
Expand Down

0 comments on commit dd7c9ad

Please sign in to comment.