Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP committed Apr 9, 2024
1 parent adc2076 commit b2d4971
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name: integration-tests

on:
workflow_dispatch:
push:
branches: [main]
pull_request:
# workflow_dispatch:
# push:
# branches: [main]
# pull_request:

concurrency:
# Only run once for latest commit per ref and cancel other (previous) runs.
Expand Down
37 changes: 32 additions & 5 deletions it/python/discriminator/test_serdeser.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,56 @@
import pytest

from kiota_serialization_json.json_parse_node_factory import JsonParseNodeFactory
from kiota_serialization_json.json_serialization_writer_factory import JsonSerializationWriterFactory

from client.discriminateme.discriminateme_request_builder import DiscriminatemeRequestBuilder
from client.models.object import Object
from client.models.object1 import Object1
from client.models.object2 import Object2

@pytest.mark.asyncio
async def test_object1_ser_deser():
async def test_object1_deser():
factory = JsonParseNodeFactory()
root = factory.get_root_parse_node('application/json', '{"objectType": "obj1", "one": "foo"}'.encode('utf-8'))
result = root.get_object_value(DiscriminatemeRequestBuilder.DiscriminatemeGetResponse)
result = root.get_object_value(Object)
assert hasattr(result, "object1")
assert not hasattr(result, "object2")
assert isinstance(result.object1, Object1)
assert result.object1.object_type == "obj1"
assert result.object1.one == "foo"

@pytest.mark.asyncio
async def test_object1_ser_deser():
async def test_object2_deser():
factory = JsonParseNodeFactory()
root = factory.get_root_parse_node('application/json', '{"objectType": "obj2", "two": "bar"}'.encode('utf-8'))
result = root.get_object_value(DiscriminatemeRequestBuilder.DiscriminatemeGetResponse)
result = root.get_object_value(Object)
assert hasattr(result, "object2")
assert not hasattr(result, "object1")
assert isinstance(result.object2, Object2)
assert result.object2.object_type == "obj2"
assert result.object2.two == "bar"

@pytest.mark.asyncio
async def test_object1_ser():
obj = Object()
obj1 = Object1()
obj1.object_type = "obj1"
obj1.one = "foo"
obj.object1 = obj1
factory = JsonSerializationWriterFactory()
writer = factory.get_serialization_writer('application/json')
obj.serialize(writer)
content = writer.get_serialized_content().decode('utf-8')
assert content == '{"objectType": "obj1", "one": "foo"}'

@pytest.mark.asyncio
async def test_object2_ser():
obj = Object()
obj2 = Object2()
obj2.object_type = "obj2"
obj2.two = "bar"
obj.object2 = obj2
factory = JsonSerializationWriterFactory()
writer = factory.get_serialization_writer('application/json')
obj.serialize(writer)
content = writer.get_serialized_content().decode('utf-8')
assert content == '{"objectType": "obj2", "two": "bar"}'
2 changes: 1 addition & 1 deletion src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2115,7 +2115,7 @@ internal static void AddSerializationMembers(CodeClass model, bool includeAdditi
IsAsync = false,
Documentation = new()
{
DescriptionTemplate = "Serializes information the current object",
DescriptionTemplate = "",
},
ReturnType = new CodeType { Name = VoidType, IsNullable = false, IsExternal = true },
Parent = model,
Expand Down
4 changes: 2 additions & 2 deletions src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ private void WriteSerializerBodyForUnionModel(CodeClass parentClass, LanguageWri
.OrderBy(static x => x, CodePropertyTypeForwardComparer)
.ThenBy(static x => x.Name))
{
writer.StartBlock($"{(includeElse ? "el" : string.Empty)}if self.{otherProp.Name}:");
writer.StartBlock($"{(includeElse ? "el" : string.Empty)}if hasattr(self, \"{otherProp.Name}\"):");
writer.WriteLine($"writer.{GetSerializationMethodName(otherProp.Type)}(None, self.{otherProp.Name})");
writer.DecreaseIndent();
if (!includeElse)
Expand All @@ -683,7 +683,7 @@ private void WriteSerializerBodyForIntersectionModel(CodeClass parentClass, Lang
.OrderBy(static x => x, CodePropertyTypeBackwardComparer)
.ThenBy(static x => x.Name))
{
writer.StartBlock($"{(includeElse ? "el" : string.Empty)}if self.{otherProp.Name}:");
writer.StartBlock($"{(includeElse ? "el" : string.Empty)}if hasattr(self, \"{otherProp.Name}\"):");
writer.WriteLine($"writer.{GetSerializationMethodName(otherProp.Type)}(None, self.{otherProp.Name})");
writer.DecreaseIndent();
if (!includeElse)
Expand Down
15 changes: 11 additions & 4 deletions tests/Kiota.Builder.IntegrationTests/DiscriminatorSample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,31 @@ servers:
- url: https://mytodos.doesnotexist/
paths:
/discriminateme:
get:
post:
description: Return something
responses:
"200":
description: OK
content:
application/json:
schema:
type: string -> still generates duplicates
# $ref: "#/components/schemas/Object"
$ref: "#/components/schemas/Object"
requestBody:
description: A new or existing `Artifact` to be associated with the `ModelVersion`.
content:
application/json:
schema:
$ref: "#/components/schemas/Object"
components:
schemas:
Object:
oneOf:
- $ref: "#/components/schemas/Object1"
- $ref: "#/components/schemas/Object2"
discriminator:
propertyName: objectType
mapping:
obj1: "#/components/schemas/Object1"
obj2: "#/components/schemas/Object2"
Object1:
type: object
required:
Expand Down

0 comments on commit b2d4971

Please sign in to comment.