-
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 3 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; | ||||
|
|
||||
|
|
@@ -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}}) | ||||
|
||||
| public void WritesModelFactoryBodyForUnionModels() |
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'
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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?
Also, where are those tests currently located at?
Thanks in advance
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Intersection model tests start here:
| public void WritesModelFactoryBodyForIntersectionModels() |
Example of an intersection model based on the previous union model example
StatusIntersection:
allOf:
- $ref: '#/components/schemas/StatusEnum'
- $ref: '#/components/schemas/DetailedStatus'There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 otherProp.WireName which is inconsistent with how the existing case works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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
| public void WritesUnionDeSerializerBody() |
It focuses on the code that is generated, rather than the behavior of the serialization library that we have as a dependency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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?
Also, where are those tests currently located at?
Thanks in advance
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's the existing test for generating the serialization method
| public void WritesIntersectionDeSerializerBody() |
I have supplied sample open api for these types of schema on the factory method/deserialization comments
Uh oh!
There was an error while loading. Please reload this page.