Skip to content

Commit

Permalink
Merge pull request #5742 from microsoft/validateOneOfs
Browse files Browse the repository at this point in the history
validate #4435
  • Loading branch information
andrueastman authored Nov 10, 2024
2 parents 2b04b18 + 87cc71e commit ad93aa0
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions it/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
60 changes: 60 additions & 0 deletions it/python/discriminator/test_serdeser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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.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 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"

@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 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"

@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"}'
51 changes: 51 additions & 0 deletions tests/Kiota.Builder.IntegrationTests/DiscriminatorSample.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ad93aa0

Please sign in to comment.