diff --git a/CHANGELOG.md b/CHANGELOG.md index f0afb3cd4d..88fbc334d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs index a34312b563..889e348f43 100644 --- a/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs @@ -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) && @@ -267,6 +273,8 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho .ToArray()); } } + + private const string NullValueString = "null"; private string DefaultDeserializerValue => $"new Dictionary>"; private void WriteDeserializerBody(bool shouldHide, CodeMethod codeElement, CodeClass parentClass, LanguageWriter writer) { diff --git a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs index 8bf41ca027..3c9432bb58 100644 --- a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs @@ -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) && @@ -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)) diff --git a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs index 9fd505b322..4a96bd3eec 100644 --- a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs @@ -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( diff --git a/tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs index 50a5621c59..06fdbf7d21 100644 --- a/tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs @@ -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("", result); Assert.Contains("setType('#microsoft.graph.entity')", result); Assert.Contains("$this->setCountryCode(new CountryCode('+254'));", result); + Assert.Contains("$this->setPropWithDefaultNullValue(null)", result); } [Fact] public void DoesNotWriteConstructorWithDefaultFromComposedType()