Skip to content

Commit

Permalink
Merge branch 'main' into feature/intersection-types
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet authored May 14, 2024
2 parents 75d86bb + 2501ba7 commit 2d58387
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed a bug where allOf structure with one entry and properties would not be considered as inheritance case. [#4346](https://github.com/microsoft/kiota/issues/4346)
- Fixed a bug where some allOf scenarios would be missing properties if type object wasn't set on the schema. [#4074](https://github.com/microsoft/kiota/issues/4074)
- Fixed a bug where schema with multiple allOf entries would incorrectly get merged to inherit from the first entry [#4428] (https://github.com/microsoft/kiota/issues/4428)
- Fixes constructor generation for nullable properties that are initialized as null in C#,Java and PHP

## [1.14.0] - 2024-05-02

Expand Down
8 changes: 8 additions & 0 deletions src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho
{
defaultValue = $"{conventions.GetTypeString(propWithDefault.Type, currentMethod).TrimEnd('?')}.{defaultValue.Trim('"').CleanupSymbolName().ToFirstCharacterUpperCase()}";
}
// avoid setting null as a string.
if (propWithDefault.Type.IsNullable &&
defaultValue.TrimQuotes().Equals(NullValueString, StringComparison.OrdinalIgnoreCase))
{
defaultValue = NullValueString;
}
writer.WriteLine($"{propWithDefault.Name.ToFirstCharacterUpperCase()} = {defaultValue};");
}
if (parentClass.IsOfKind(CodeClassKind.RequestBuilder) &&
Expand All @@ -267,6 +273,8 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho
.ToArray());
}
}

private const string NullValueString = "null";
private string DefaultDeserializerValue => $"new Dictionary<string, Action<{conventions.ParseNodeInterfaceName}>>";
private void WriteDeserializerBody(bool shouldHide, CodeMethod codeElement, CodeClass parentClass, LanguageWriter writer)
{
Expand Down
7 changes: 7 additions & 0 deletions src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho
{
defaultValue = $"{enumDefinition.Name}.forValue({defaultValue})";
}
// avoid setting null as a string.
if (propWithDefault.Type.IsNullable &&
defaultValue.TrimQuotes().Equals(NullValueString, StringComparison.OrdinalIgnoreCase))
{
defaultValue = NullValueString;
}
writer.WriteLine($"this.{setterName}({defaultValue});");
}
if (parentClass.IsOfKind(CodeClassKind.RequestBuilder) &&
Expand All @@ -376,6 +382,7 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho
.ToArray());
}
}
private const string NullValueString = "null";
private static void WriteSetterBody(CodeMethod codeElement, LanguageWriter writer, CodeClass parentClass)
{
if (parentClass.GetBackingStoreProperty() is not CodeProperty backingStore || (codeElement.AccessedProperty?.IsOfKind(CodePropertyKind.BackingStore) ?? false))
Expand Down
7 changes: 7 additions & 0 deletions src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,17 @@ private void WriteModelConstructorBody(CodeClass parentClass, LanguageWriter wri
{
defaultValue = $"new {enumDefinition.Name.ToFirstCharacterUpperCase()}({defaultValue})";
}
// avoid setting null as a string.
if (propWithDefault.Type.IsNullable &&
defaultValue.TrimQuotes().Equals(NullValueString, StringComparison.OrdinalIgnoreCase))
{
defaultValue = NullValueString;
}
writer.WriteLine($"$this->{setterName}({defaultValue});");
}
}

private const string NullValueString = "null";
private void WriteRequestBuilderConstructorBody(CodeClass parentClass, CodeMethod currentMethod, LanguageWriter writer)
{
foreach (var propWithDefault in parentClass.GetPropertiesOfKind(
Expand Down
14 changes: 14 additions & 0 deletions tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1442,12 +1442,26 @@ public void WritesConstructor()
Name = "string"
},
});
var defaultValueNull = "\"null\"";
var nullPropName = "propWithDefaultNullValue";
parentClass.AddProperty(new CodeProperty
{
Name = nullPropName,
DefaultValue = defaultValueNull,
Kind = CodePropertyKind.Custom,
Type = new CodeType
{
Name = "int",
IsNullable = true
}
});
writer.Write(method);
var result = tw.ToString();
Assert.Contains("<summary>", result);
Assert.Contains("<see cref=", result);
Assert.Contains(parentClass.Name.ToFirstCharacterUpperCase(), result);
Assert.Contains($"{propName.ToFirstCharacterUpperCase()} = {defaultValue}", result);
Assert.Contains($"{nullPropName.ToFirstCharacterUpperCase()} = {defaultValueNull.TrimQuotes()}", result);
}
[Fact]
public void WritesWithUrl()
Expand Down
14 changes: 14 additions & 0 deletions tests/Kiota.Builder.Tests/Writers/Java/CodeMethodWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,19 @@ public void WritesConstructor()
Name = "String"
}
});
var defaultValueNull = "\"null\"";
var nullPropName = "propWithDefaultNullValue";
parentClass.AddProperty(new CodeProperty
{
Name = nullPropName,
DefaultValue = defaultValueNull,
Kind = CodePropertyKind.Custom,
Type = new CodeType
{
Name = "int",
IsNullable = true
}
});
AddRequestProperties();
method.AddParameter(new CodeParameter
{
Expand All @@ -1801,6 +1814,7 @@ public void WritesConstructor()
var result = tw.ToString();
Assert.Contains(parentClass.Name, result);
Assert.Contains($"this.set{propName.ToFirstCharacterUpperCase()}({defaultValue})", result);
Assert.Contains($"this.set{nullPropName.ToFirstCharacterUpperCase()}({defaultValueNull.TrimQuotes()})", result);
Assert.Contains("super", result);
}
[Fact]
Expand Down
15 changes: 14 additions & 1 deletion tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1125,14 +1125,27 @@ public async Task WriteConstructorBody()
Type = new CodeType { Name = "countryCode", TypeDefinition = countryCode }
};
parentClass.AddProperty(propWithDefaultValue, enumProp);

var defaultValueNull = "\"null\"";
var nullPropName = "propWithDefaultNullValue";
parentClass.AddProperty(new CodeProperty
{
Name = nullPropName,
DefaultValue = defaultValueNull,
Kind = CodePropertyKind.Custom,
Type = new CodeType
{
Name = "int",
IsNullable = true
}
});
await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.PHP }, root);
_codeMethodWriter.WriteCodeElement(constructor, languageWriter);
var result = stringWriter.ToString();

Assert.Contains("public function __construct", result);
Assert.Contains("$this->setType('#microsoft.graph.entity')", result);
Assert.Contains("$this->setCountryCode(new CountryCode('+254'));", result);
Assert.Contains("$this->setPropWithDefaultNullValue(null)", result);
}
[Fact]
public void DoesNotWriteConstructorWithDefaultFromComposedType()
Expand Down

0 comments on commit 2d58387

Please sign in to comment.