From 0407b9272e45c5bb30ecdee935e70df0982b44df Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Fri, 8 Nov 2024 12:21:36 +0300 Subject: [PATCH 1/3] validate https://github.com/microsoft/kiota/pull/4435 --- .github/workflows/integration-tests.yml | 1 + it/config.json | 9 +++ it/python/discriminator/test_serdeser.py | 56 +++++++++++++++++++ .../DiscriminatorSample.yaml | 51 +++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 it/python/discriminator/test_serdeser.py create mode 100644 tests/Kiota.Builder.IntegrationTests/DiscriminatorSample.yaml diff --git a/.github/workflows/integration-tests.yml b/.github/workflows/integration-tests.yml index af6046f4b4..c3ae9d6b9b 100644 --- a/.github/workflows/integration-tests.yml +++ b/.github/workflows/integration-tests.yml @@ -49,6 +49,7 @@ jobs: - "./tests/Kiota.Builder.IntegrationTests/NoUnderscoresInModel.yaml" - "./tests/Kiota.Builder.IntegrationTests/ToDoApi.yaml" - "./tests/Kiota.Builder.IntegrationTests/GeneratesUritemplateHints.yaml" + - "./tests/Kiota.Builder.IntegrationTests/DiscriminatorSample.yaml" - "oas::petstore" - "apisguru::twitter.com:current" - "apisguru::notion.com" diff --git a/it/config.json b/it/config.json index 1e7120f803..ac324660e0 100644 --- a/it/config.json +++ b/it/config.json @@ -17,6 +17,15 @@ } ] }, + "./tests/Kiota.Builder.IntegrationTests/DiscriminatorSample.yaml": { + "MockServerITFolder": "discriminator", + "Suppressions": [ + { + "Language": "ruby", + "Rationale": "https://github.com/microsoft/kiota/issues/2484" + } + ] + }, "apisguru::github.com:api.github.com": { "MockServerITFolder": "gh", "Suppressions": [ diff --git a/it/python/discriminator/test_serdeser.py b/it/python/discriminator/test_serdeser.py new file mode 100644 index 0000000000..50dcd21e7d --- /dev/null +++ b/it/python/discriminator/test_serdeser.py @@ -0,0 +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.models.component import Component +from client.models.component1 import Component1 +from client.models.component2 import Component2 + +@pytest.mark.asyncio +async def test_component1_deser(): + factory = JsonParseNodeFactory() + root = factory.get_root_parse_node('application/json', '{"objectType": "obj1", "one": "foo"}'.encode('utf-8')) + result = root.get_object_value(Component) + assert hasattr(result, "component1") + assert not hasattr(result, "component2") + assert isinstance(result.component1, Component1) + assert result.component1.object_type == "obj1" + assert result.component1.one == "foo" + +@pytest.mark.asyncio +async def test_component2_deser(): + factory = JsonParseNodeFactory() + root = factory.get_root_parse_node('application/json', '{"objectType": "obj2", "two": "bar"}'.encode('utf-8')) + result = root.get_object_value(Component) + assert hasattr(result, "component2") + assert not hasattr(result, "component1") + assert isinstance(result.component2, Component2) + assert result.component2.object_type == "obj2" + assert result.component2.two == "bar" + +@pytest.mark.asyncio +async def test_component1_ser(): + obj = Component() + obj1 = Component1() + obj1.object_type = "obj1" + obj1.one = "foo" + obj.component1 = 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_component2_ser(): + obj = Component() + obj2 = Component2() + obj2.object_type = "obj2" + obj2.two = "bar" + obj.component2 = 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"}' \ No newline at end of file diff --git a/tests/Kiota.Builder.IntegrationTests/DiscriminatorSample.yaml b/tests/Kiota.Builder.IntegrationTests/DiscriminatorSample.yaml new file mode 100644 index 0000000000..18dbc08b35 --- /dev/null +++ b/tests/Kiota.Builder.IntegrationTests/DiscriminatorSample.yaml @@ -0,0 +1,51 @@ +openapi: 3.0.0 +info: + title: "Discriminator API" + version: "1.0.0" +servers: + - url: https://mytodos.doesnotexist/ +paths: + /discriminateme: + post: + description: Return something + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/Component" + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/Component" +components: + schemas: + Component: + oneOf: + - $ref: "#/components/schemas/Component1" + - $ref: "#/components/schemas/Component2" + discriminator: + propertyName: objectType + mapping: + obj1: "#/components/schemas/Component1" + obj2: "#/components/schemas/Component2" + Component1: + type: object + required: + - objectType + properties: + objectType: + type: string + one: + type: string + Component2: + type: object + required: + - objectType + properties: + objectType: + type: string + two: + type: string \ No newline at end of file From 593a887b08d4845925e235887dfda37d119ef3dd Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Fri, 8 Nov 2024 12:35:24 +0300 Subject: [PATCH 2/3] fix module import --- it/python/discriminator/test_serdeser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/it/python/discriminator/test_serdeser.py b/it/python/discriminator/test_serdeser.py index 50dcd21e7d..40fa1ef985 100644 --- a/it/python/discriminator/test_serdeser.py +++ b/it/python/discriminator/test_serdeser.py @@ -3,7 +3,7 @@ from kiota_serialization_json.json_parse_node_factory import JsonParseNodeFactory from kiota_serialization_json.json_serialization_writer_factory import JsonSerializationWriterFactory -from client.models.component import Component +from client.discriminateme.component import Component from client.models.component1 import Component1 from client.models.component2 import Component2 From 87cc71e31cf4e258a5b43c9483afe7f0a8c944a3 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Fri, 8 Nov 2024 13:00:30 +0300 Subject: [PATCH 3/3] the property is always present as its initialized. assert its None instead --- it/python/discriminator/test_serdeser.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/it/python/discriminator/test_serdeser.py b/it/python/discriminator/test_serdeser.py index 40fa1ef985..429146baaf 100644 --- a/it/python/discriminator/test_serdeser.py +++ b/it/python/discriminator/test_serdeser.py @@ -13,7 +13,9 @@ async def test_component1_deser(): root = factory.get_root_parse_node('application/json', '{"objectType": "obj1", "one": "foo"}'.encode('utf-8')) result = root.get_object_value(Component) assert hasattr(result, "component1") - assert not hasattr(result, "component2") + assert hasattr(result, "component2") + assert result.component2 is None + assert result.component1 is not None assert isinstance(result.component1, Component1) assert result.component1.object_type == "obj1" assert result.component1.one == "foo" @@ -24,7 +26,9 @@ async def test_component2_deser(): root = factory.get_root_parse_node('application/json', '{"objectType": "obj2", "two": "bar"}'.encode('utf-8')) result = root.get_object_value(Component) assert hasattr(result, "component2") - assert not hasattr(result, "component1") + assert hasattr(result, "component1") + assert result.component1 is None + assert result.component2 is not None assert isinstance(result.component2, Component2) assert result.component2.object_type == "obj2" assert result.component2.two == "bar"