Skip to content
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

[DO NOT MERGE]Naive support for compound schema in the request body allowing to der… #88

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 57 additions & 6 deletions almdrlib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
import json
import jsonschema
from jsonschema.validators import validator_for
from collections import OrderedDict
from functools import reduce

import alsdkdefs
from almdrlib.util import *

from almdrlib.exceptions import AlmdrlibValueError
from almdrlib.config import Config
Expand Down Expand Up @@ -247,7 +251,7 @@ def add_content(self, content_type, schema, al_schema):
if not schema:
return

datatype = schema.get(OpenAPIKeyWord.TYPE)
datatype = schema.get(OpenAPIKeyWord.TYPE, derive_type_from_decomposed_schema(schema))
name = al_schema.get(OpenAPIKeyWord.NAME, OpenAPIKeyWord.DATA)
if datatype == OpenAPIKeyWord.OBJECT:
parameter = RequestBodyObjectParameter(
Expand Down Expand Up @@ -786,18 +790,65 @@ def __getattr__(self, op_name):

def _normalize_schema(name, schema, required=False):
properties = schema.get(OpenAPIKeyWord.PROPERTIES)
oneof = schema.get(OpenAPIKeyWord.ONE_OF)
anyof = schema.get(OpenAPIKeyWord.ANY_OF)
allof = schema.get(OpenAPIKeyWord.ALL_OF)
compound_schema = oneof or anyof or allof

if properties and bool(properties):
return schema

result = {
OpenAPIKeyWord.TYPE: OpenAPIKeyWord.OBJECT,
OpenAPIKeyWord.PROPERTIES: {
if compound_schema:
# TODO move to alertlogic-sdk-definitions normalize_node
if oneof:
xof = OpenAPIKeyWord.ONE_OF
elif anyof:
xof = OpenAPIKeyWord.ANY_OF
elif allof:
xof = OpenAPIKeyWord.ALL_OF

def reduce_props(acc, prop):
(k, v) = prop
existing_prop = acc.get(k, [])
if existing_prop and isinstance(existing_prop, list):
existing_prop.append(v)
acc[k] = existing_prop
return acc
elif existing_prop:
if existing_prop == v:
return acc
acc[k] = [v, existing_prop]
return acc
else:
acc[k] = v
return acc

def add_xof(item):
(k, v) = item
if isinstance(v, list):
return k, OrderedDict({xof: v})
else:
return item

allprops = reduce(lambda acc, s: acc + list(s.get(OpenAPIKeyWord.PROPERTIES, OrderedDict()).items()),
compound_schema, [])
reduced = reduce(reduce_props, allprops, OrderedDict())
compound_props = OrderedDict(map(add_xof, reduced.items()))
properties = compound_props

if not properties:
properties = OrderedDict({
name: schema
}
}
})

result = OrderedDict({
OpenAPIKeyWord.TYPE: derive_type_from_decomposed_schema(schema) or OpenAPIKeyWord.OBJECT,
OpenAPIKeyWord.PROPERTIES: properties
})

if required:
result[OpenAPIKeyWord.REQUIRED] = [name]

return result


Expand Down
14 changes: 14 additions & 0 deletions almdrlib/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from alsdkdefs import OpenAPIKeyWord


def derive_type_from_decomposed_schema(schema):
oneof = schema.get(OpenAPIKeyWord.ONE_OF)
anyof = schema.get(OpenAPIKeyWord.ANY_OF)
allof = schema.get(OpenAPIKeyWord.ALL_OF)

if oneof or anyof or allof:
# Attempt to find type in the decomposed schema
find_type = [t.get('type') for t in allof or anyof or oneof if t.get('type')]
return find_type[0] if find_type else None
else:
return None
29 changes: 29 additions & 0 deletions tests/apis/testapi/testapi.v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,35 @@ servers:
description: integration
x-alertlogic-session-endpoint: true
paths:
'/testapi/v1/postdata':
post:
operationId: post_data_compound
requestBody:
content:
application/json:
schema:
oneOf:
- type: object
properties:
prop:
type: string
additionalProperties: true
- type: object
properties:
prop:
type: integer
- type: string
responses:
'200':
description: 200 resp
content:
application/json:
schema:
type: object
properties:
prop:
type: string

'/testapi/v1/{account_id}/test_get_data':
get:
summary: Test Get Data Operation
Expand Down
13 changes: 12 additions & 1 deletion tests/test_open_api_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from almdrlib.client import Config
from almdrlib.client import Operation
from alsdkdefs import OpenAPIKeyWord

from collections import OrderedDict

class TestSdk_open_api_support(unittest.TestCase):
"""Tests for `python_boilerplate` package."""
Expand Down Expand Up @@ -83,3 +83,14 @@ def test_003_default_objects_creation(self):
self.assertIsInstance(Session(), Session)
self.assertIsInstance(Config(), Config)
self.assertIsInstance(Client(self._service_name), Client)

def test_004_test_operations_compound_schema(self):
"""Test request body properties are properly normalized when schema is compound at the top level"""
client = Client(self._service_name)
operation = client.operations.get('post_data_compound')
self.assertEqual(OrderedDict([('prop',
OrderedDict([('oneOf', [
OrderedDict([('type', 'integer')]),
OrderedDict([('type', 'string')])
])]))]),
operation.body._content['application/json']._properties)