-
Notifications
You must be signed in to change notification settings - Fork 306
Handle nullable types in OpenAPI schemas and add corresponding unit t… #7170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6c42ae8
89e9c09
592710a
8de37c7
d1eed2d
8833173
f82b3c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |||
| using Kiota.Builder.CodeDOM; | ||||
| using Kiota.Builder.Extensions; | ||||
| using Kiota.Builder.OrderComparers; | ||||
| using Microsoft.OpenApi; | ||||
|
|
||||
| namespace Kiota.Builder.Writers.CSharp; | ||||
|
|
||||
|
|
@@ -146,8 +147,16 @@ private void WriteFactoryMethodBodyForUnionModel(CodeMethod codeElement, CodeCla | |||
| var includeNullableInfo = propertyType.IsCollection; | ||||
| var typeName = conventions.GetTypeString(propertyType, codeElement, true, includeNullableInfo); | ||||
| var valueVarName = $"{property.Name.ToFirstCharacterLowerCase()}Value"; | ||||
| writer.WriteLine($"{(includeElse ? "else " : string.Empty)}if({parseNodeParameter.Name.ToFirstCharacterLowerCase()}.{GetDeserializationMethodName(propertyType, codeElement)} is {typeName} {valueVarName})"); | ||||
| writer.WriteBlock(lines: $"{ResultVarName}.{property.Name.ToFirstCharacterUpperCase()} = {valueVarName};"); | ||||
| if (propertyType.TypeDefinition is CodeEnum {BackingType: JsonSchemaType.Integer}) | ||||
| { | ||||
| writer.WriteLine($"{(includeElse ? "else " : string.Empty)}if({parseNodeParameter.Name.ToFirstCharacterLowerCase()}.{GetDeserializationMethodName(propertyType, codeElement)} is int {valueVarName})"); | ||||
| writer.WriteBlock(lines: $"{ResultVarName}.{property.Name.ToFirstCharacterUpperCase()} = ({typeName}){valueVarName};"); | ||||
| } | ||||
| else | ||||
| { | ||||
| writer.WriteLine($"{(includeElse ? "else " : string.Empty)}if({parseNodeParameter.Name.ToFirstCharacterLowerCase()}.{GetDeserializationMethodName(propertyType, codeElement)} is {typeName} {valueVarName})"); | ||||
| writer.WriteBlock(lines: $"{ResultVarName}.{property.Name.ToFirstCharacterUpperCase()} = {valueVarName};"); | ||||
| } | ||||
| } | ||||
| if (!includeElse) | ||||
| includeElse = true; | ||||
|
|
@@ -170,8 +179,16 @@ private void WriteFactoryMethodBodyForIntersectionModel(CodeMethod codeElement, | |||
| var includeNullableInfo = propertyType.IsCollection; | ||||
| var typeName = conventions.GetTypeString(propertyType, codeElement, true, includeNullableInfo); | ||||
| var valueVarName = $"{property.Name.ToFirstCharacterLowerCase()}Value"; | ||||
| writer.WriteLine($"{(includeElse ? "else " : string.Empty)}if({parseNodeParameter.Name.ToFirstCharacterLowerCase()}.{GetDeserializationMethodName(propertyType, codeElement)} is {typeName} {valueVarName})"); | ||||
| writer.WriteBlock(lines: $"{ResultVarName}.{property.Name.ToFirstCharacterUpperCase()} = {valueVarName};"); | ||||
| if (propertyType.TypeDefinition is CodeEnum {BackingType: JsonSchemaType.Integer}) | ||||
| { | ||||
| writer.WriteLine($"{(includeElse ? "else " : string.Empty)}if({parseNodeParameter.Name.ToFirstCharacterLowerCase()}.{GetDeserializationMethodName(propertyType, codeElement)} is int {valueVarName})"); | ||||
| writer.WriteBlock(lines: $"{ResultVarName}.{property.Name.ToFirstCharacterUpperCase()} = ({typeName}){valueVarName};"); | ||||
| } | ||||
| else | ||||
| { | ||||
| writer.WriteLine($"{(includeElse ? "else " : string.Empty)}if({parseNodeParameter.Name.ToFirstCharacterLowerCase()}.{GetDeserializationMethodName(propertyType, codeElement)} is {typeName} {valueVarName})"); | ||||
| writer.WriteBlock(lines: $"{ResultVarName}.{property.Name.ToFirstCharacterUpperCase()} = {valueVarName};"); | ||||
| } | ||||
| } | ||||
| if (!includeElse) | ||||
| includeElse = true; | ||||
|
|
@@ -355,7 +372,13 @@ 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)}; }} }},"); | ||||
| if (otherProp is {Type: CodeType { TypeDefinition: CodeEnum {BackingType: JsonSchemaType.Integer}} propertyType}) | ||||
| { | ||||
| var typeName = conventions.GetTypeString(propertyType, codeElement, true, (propertyType.TypeDefinition is CodeEnum || conventions.IsPrimitiveType(propertyType.Name)) && propertyType.CollectionKind is not CodeTypeBase.CodeTypeCollectionKind.None); | ||||
| writer.WriteLine($"{{ \"{otherProp.WireName}\", n => {{ {otherProp.Name.ToFirstCharacterUpperCase()} = ({typeName}?) n.{GetDeserializationMethodName(otherProp.Type, codeElement)}; }} }},"); | ||||
| } | ||||
| else | ||||
| writer.WriteLine($"{{ \"{otherProp.WireName}\", n => {{ {otherProp.Name.ToFirstCharacterUpperCase()} = n.{GetDeserializationMethodName(otherProp.Type, codeElement)}; }} }},"); | ||||
| } | ||||
| writer.CloseBlock("};"); | ||||
| } | ||||
|
|
@@ -376,7 +399,10 @@ private string GetDeserializationMethodName(CodeTypeBase propType, CodeMethod me | |||
| return $"GetCollectionOfObjectValues<{propertyType}>({propertyType}.CreateFromDiscriminatorValue){collectionMethod}"; | ||||
| } | ||||
| else if (currentType.TypeDefinition is CodeEnum enumType) | ||||
| return $"GetEnumValue<{enumType.GetFullName()}>()"; | ||||
| if (enumType.BackingType is JsonSchemaType.Integer) | ||||
| return $"GetIntValue()"; | ||||
| else | ||||
| return $"GetEnumValue<{enumType.GetFullName()}>()"; | ||||
| } | ||||
| return propertyType switch | ||||
| { | ||||
|
|
@@ -488,7 +514,10 @@ private void WriteSerializerBodyForInheritedModel(bool shouldHide, CodeMethod me | |||
| .OrderBy(static x => x.Name)) | ||||
| { | ||||
| var serializationMethodName = GetSerializationMethodName(otherProp.Type, method); | ||||
| writer.WriteLine($"writer.{serializationMethodName}(\"{otherProp.WireName}\", {otherProp.Name.ToFirstCharacterUpperCase()});"); | ||||
| if (otherProp.Type is CodeType{TypeDefinition: CodeEnum {BackingType: JsonSchemaType.Integer}}) | ||||
| writer.WriteLine($"writer.{serializationMethodName}(\"{otherProp.WireName}\", (int?){otherProp.Name.ToFirstCharacterUpperCase()});"); | ||||
| else | ||||
| writer.WriteLine($"writer.{serializationMethodName}(\"{otherProp.WireName}\", {otherProp.Name.ToFirstCharacterUpperCase()});"); | ||||
| } | ||||
| } | ||||
| private void WriteSerializerBodyForUnionModel(CodeMethod method, CodeClass parentClass, LanguageWriter writer) | ||||
|
|
@@ -500,8 +529,12 @@ private void WriteSerializerBodyForUnionModel(CodeMethod method, CodeClass paren | |||
| .OrderBy(static x => x, CodePropertyTypeForwardComparer) | ||||
| .ThenBy(static x => x.Name)) | ||||
| { | ||||
| var serializationMethodName = GetSerializationMethodName(otherProp.Type, method); | ||||
| writer.WriteLine($"{(includeElse ? "else " : string.Empty)}if({otherProp.Name.ToFirstCharacterUpperCase()} != null)"); | ||||
| writer.WriteBlock(lines: $"writer.{GetSerializationMethodName(otherProp.Type, method)}(null, {otherProp.Name.ToFirstCharacterUpperCase()});"); | ||||
| if (otherProp.Type is CodeType{TypeDefinition: CodeEnum {BackingType: JsonSchemaType.Integer}}) | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not seeing any tests for serialization of union models that include Integer Enums, let's make sure test cases for this are added. It feels a bit weird that the integer case is passing
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am very aware of that, wanted to avoid cross-repository changes tho and have this easily integrateable on my end. Could you hint me at where those tests are currently placed and how they look? Thanks in advance.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's where we have an existing test for the union model serialization
It focuses on the code that is generated, rather than the behavior of the serialization library that we have as a dependency. |
||||
| writer.WriteBlock(lines: $"writer.{serializationMethodName}(null, (int?){otherProp.Name.ToFirstCharacterUpperCase()});"); | ||||
| else | ||||
| writer.WriteBlock(lines: $"writer.{serializationMethodName}(null, {otherProp.Name.ToFirstCharacterUpperCase()});"); | ||||
| if (!includeElse) | ||||
| includeElse = true; | ||||
| } | ||||
|
|
@@ -516,8 +549,12 @@ private void WriteSerializerBodyForIntersectionModel(CodeMethod method, CodeClas | |||
| .OrderBy(static x => x, CodePropertyTypeBackwardComparer) | ||||
| .ThenBy(static x => x.Name)) | ||||
| { | ||||
| var serializationMethodName = GetSerializationMethodName(otherProp.Type, method); | ||||
| writer.WriteLine($"{(includeElse ? "else " : string.Empty)}if({otherProp.Name.ToFirstCharacterUpperCase()} != null)"); | ||||
| writer.WriteBlock(lines: $"writer.{GetSerializationMethodName(otherProp.Type, method)}(null, {otherProp.Name.ToFirstCharacterUpperCase()});"); | ||||
| if (otherProp.Type is CodeType{TypeDefinition: CodeEnum {BackingType: JsonSchemaType.Integer}}) | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not seeing any tests for serialization of intersection models that include Integer Enums, let's make sure test cases for this are added.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not entirely sure what you mean by that, could you provide a sample json? Thanks in advance
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's the existing test for generating the serialization method
I have supplied sample open api for these types of schema on the factory method/deserialization comments |
||||
| writer.WriteBlock(lines: $"writer.{serializationMethodName}(null, (int?){otherProp.Name.ToFirstCharacterUpperCase()});"); | ||||
| else | ||||
| writer.WriteBlock(lines: $"writer.{serializationMethodName}(null, {otherProp.Name.ToFirstCharacterUpperCase()});"); | ||||
| if (!includeElse) | ||||
| includeElse = true; | ||||
| } | ||||
|
|
@@ -535,7 +572,12 @@ private void WriteSerializerBodyForIntersectionModel(CodeMethod method, CodeClas | |||
| .Select(static x => x.Name.ToFirstCharacterUpperCase()) | ||||
| .OrderBy(static x => x) | ||||
| .Aggregate(static (x, y) => $"{x}, {y}"); | ||||
| writer.WriteLine($"writer.{GetSerializationMethodName(complexProperties.First().Type, method)}(null, {propertiesNames});"); | ||||
| var prop = complexProperties.First(); | ||||
| var serializationMethodName = GetSerializationMethodName(prop.Type, method); | ||||
| if (prop.Type is CodeType{TypeDefinition: CodeEnum {BackingType: JsonSchemaType.Integer}}) | ||||
| writer.WriteLine($"writer.{serializationMethodName}(\"{prop.WireName}\", (int?){prop.Name.ToFirstCharacterUpperCase()});"); | ||||
| else | ||||
| writer.WriteLine($"writer.{GetSerializationMethodName(complexProperties.First().Type, method)}(null, {propertiesNames});"); | ||||
| if (includeElse) | ||||
| { | ||||
| writer.CloseBlock(); | ||||
|
|
@@ -684,7 +726,10 @@ private string GetSerializationMethodName(CodeTypeBase propType, CodeMethod meth | |||
| else | ||||
| return $"WriteCollectionOfObjectValues<{propertyType}>"; | ||||
| else if (currentType.TypeDefinition is CodeEnum enumType) | ||||
| return $"WriteEnumValue<{enumType.GetFullName()}>"; | ||||
| if (enumType.BackingType is JsonSchemaType.Integer) | ||||
| return $"WriteIntValue"; | ||||
| else | ||||
| return $"WriteEnumValue<{enumType.GetFullName()}>"; | ||||
|
|
||||
| } | ||||
| return propertyType switch | ||||
|
|
||||
Uh oh!
There was an error while loading. Please reload this page.