diff --git a/.github/workflows/integration-tests.yml b/.github/workflows/integration-tests.yml index c3ae9d6b9b..17bc7139d9 100644 --- a/.github/workflows/integration-tests.yml +++ b/.github/workflows/integration-tests.yml @@ -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. diff --git a/it/python/discriminator/test_serdeser.py b/it/python/discriminator/test_serdeser.py index 125cb6875a..e80a5f1f61 100644 --- a/it/python/discriminator/test_serdeser.py +++ b/it/python/discriminator/test_serdeser.py @@ -1,16 +1,17 @@ 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) @@ -18,12 +19,38 @@ async def test_object1_ser_deser(): 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"}' diff --git a/src/Kiota.Builder/KiotaBuilder.cs b/src/Kiota.Builder/KiotaBuilder.cs index 1f8499d3d6..9590cef7d9 100644 --- a/src/Kiota.Builder/KiotaBuilder.cs +++ b/src/Kiota.Builder/KiotaBuilder.cs @@ -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, diff --git a/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs index ea12d33a04..fead21630b 100644 --- a/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs @@ -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) @@ -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) diff --git a/tests/Kiota.Builder.IntegrationTests/DiscriminatorSample.yaml b/tests/Kiota.Builder.IntegrationTests/DiscriminatorSample.yaml index f4d3db53ea..889da630b4 100644 --- a/tests/Kiota.Builder.IntegrationTests/DiscriminatorSample.yaml +++ b/tests/Kiota.Builder.IntegrationTests/DiscriminatorSample.yaml @@ -6,7 +6,7 @@ servers: - url: https://mytodos.doesnotexist/ paths: /discriminateme: - get: + post: description: Return something responses: "200": @@ -14,16 +14,23 @@ paths: 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: