-
Notifications
You must be signed in to change notification settings - Fork 299
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
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; | ||||
|
|
||||
|
|
@@ -141,10 +142,21 @@ private void WriteFactoryMethodBodyForUnionModel(CodeMethod codeElement, CodeCla | |||
| } | ||||
| else if (propertyType.TypeDefinition is CodeClass && propertyType.IsCollection || propertyType.TypeDefinition is null || propertyType.TypeDefinition is CodeEnum) | ||||
| { | ||||
| var readerReference = parseNodeParameter.Name.ToFirstCharacterLowerCase(); | ||||
| var selfReference = $"{ResultVarName}.{property.Name.ToFirstCharacterUpperCase()}"; | ||||
| var deserializationMethodName = GetDeserializationMethodName(propertyType, codeElement); | ||||
| var typeName = conventions.GetTypeString(propertyType, codeElement, true, (propertyType.TypeDefinition is CodeEnum || conventions.IsPrimitiveType(propertyType.Name)) && propertyType.CollectionKind is not CodeTypeBase.CodeTypeCollectionKind.None); | ||||
| 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 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. This need test coverage to ensure the deserialization code is generated correctly for the case of a union model with an integer enum.
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 where the tests for this method start:
A union model with and integer enum might looks something like: openapi: 3.0.3
info:
title: Union with Integer Enum API
version: 1.0.0
servers:
- url: https://api.example.com/v1
paths:
/status:
get:
summary: Get status that can be multiple types
responses:
'200':
description: A union type containing an integer enum
content:
application/json:
schema:
$ref: '#/components/schemas/StatusUnion'
components:
schemas:
# Integer-backed enum
StatusEnum:
type: integer
enum: [0, 1, 2]
x-ms-enum:
name: StatusEnum
modelAsString: false
values:
- name: Active
value: 0
description: "The resource is active"
- name: Inactive
value: 1
description: "The resource is inactive"
- name: Pending
value: 2
description: "The resource is pending"
# A complex object type
DetailedStatus:
type: object
required:
- message
properties:
message:
type: string
code:
type: integer
# Union model combining integer enum and object
StatusUnion:
oneOf:
- $ref: '#/components/schemas/StatusEnum'
- $ref: '#/components/schemas/DetailedStatus'
discriminator:
propertyName: '@odata.type'
mapping:
'#kiota.statusEnum': '#/components/schemas/StatusEnum'
'#kiota.detailedStatus': '#/components/schemas/DetailedStatus'
|
||||
| { | ||||
| writer.WriteLine($"{(includeElse ? "else " : string.Empty)}if({readerReference}.{deserializationMethodName} is int {valueVarName})"); | ||||
| writer.WriteBlock(lines: $"{selfReference} = ({typeName}){valueVarName};"); | ||||
| } | ||||
| else | ||||
| { | ||||
| writer.WriteLine($"{(includeElse ? "else " : string.Empty)}if({readerReference}.{deserializationMethodName} is {typeName} {valueVarName})"); | ||||
| writer.WriteBlock(lines: $"{selfReference} = {valueVarName};"); | ||||
| } | ||||
| } | ||||
| if (!includeElse) | ||||
| includeElse = true; | ||||
|
|
@@ -162,10 +174,21 @@ private void WriteFactoryMethodBodyForIntersectionModel(CodeMethod codeElement, | |||
| { | ||||
| if (property.Type is CodeType propertyType) | ||||
| { | ||||
| var readerReference = parseNodeParameter.Name.ToFirstCharacterLowerCase(); | ||||
| var selfReference = $"{ResultVarName}.{property.Name.ToFirstCharacterUpperCase()}"; | ||||
| var deserializationMethodName = GetDeserializationMethodName(propertyType, codeElement); | ||||
| var typeName = conventions.GetTypeString(propertyType, codeElement, true, propertyType.TypeDefinition is CodeEnum && propertyType.CollectionKind is not CodeTypeBase.CodeTypeCollectionKind.None); | ||||
| 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 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. This need test coverage to ensure the deserialization code is generated correctly for the case of a intersection model with an integer enum.
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. Intersection model tests start here:
Example of an intersection model based on the previous union model example StatusIntersection:
allOf:
- $ref: '#/components/schemas/StatusEnum'
- $ref: '#/components/schemas/DetailedStatus' |
||||
| { | ||||
| writer.WriteLine($"{(includeElse ? "else " : string.Empty)}if({readerReference}.{deserializationMethodName} is int {valueVarName})"); | ||||
| writer.WriteBlock(lines: $"{selfReference} = ({typeName}){valueVarName};"); | ||||
| } | ||||
| else | ||||
| { | ||||
| writer.WriteLine($"{(includeElse ? "else " : string.Empty)}if({readerReference}.{deserializationMethodName} is {typeName} {valueVarName})"); | ||||
| writer.WriteBlock(lines: $"{selfReference} = {valueVarName};"); | ||||
| } | ||||
| } | ||||
| if (!includeElse) | ||||
| includeElse = true; | ||||
|
|
@@ -349,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("};"); | ||||
| } | ||||
|
|
@@ -370,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 | ||||
| { | ||||
|
|
@@ -482,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) | ||||
|
|
@@ -494,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}(\"{otherProp.WireName}\", (int?){otherProp.Name.ToFirstCharacterUpperCase()});"); | ||||
| else | ||||
| writer.WriteBlock(lines: $"writer.{serializationMethodName}(null, {otherProp.Name.ToFirstCharacterUpperCase()});"); | ||||
| if (!includeElse) | ||||
| includeElse = true; | ||||
| } | ||||
|
|
@@ -510,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}(\"{otherProp.WireName}\", (int?){otherProp.Name.ToFirstCharacterUpperCase()});"); | ||||
| else | ||||
| writer.WriteBlock(lines: $"writer.{serializationMethodName}(null, {otherProp.Name.ToFirstCharacterUpperCase()});"); | ||||
| if (!includeElse) | ||||
| includeElse = true; | ||||
| } | ||||
|
|
@@ -529,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(); | ||||
|
|
@@ -678,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.