Skip to content

Commit 35c57d6

Browse files
committed
feat: DocumentationManager ResponseDto bug fixed
1 parent 842d238 commit 35c57d6

7 files changed

Lines changed: 81 additions & 19 deletions

File tree

‎Tests.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from python_helper import TestHelper
88
TestHelper.run(__file__)
9-
109
# TestHelper.run(
1110
# __file__,
1211
# runOnly = [
@@ -34,7 +33,8 @@
3433
# 'SerializerTest.convertFromObjectToObject_whenTargetClassIsList',
3534
# 'SerializerTest.isModelTest',
3635
# 'SerializerTest.isJsonifyable',
37-
# 'SerializerTest.jsonifyIt'
36+
# 'SerializerTest.jsonifyIt',
37+
# 'SerializerTest.weirdIdList'
3838
# ]
3939
# )
4040
# TestHelper.run(
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from python_helper import DateTimeHelper
2-
from python_helper import ObjectHelper
1+
from python_helper import DateTimeHelper, ObjectHelper
2+
from python_framework.api.src.helper import Serializer
33

44
def getValueOrDefault(value, default) :
55
return value if ObjectHelper.isNotNone(value) else default
@@ -8,3 +8,21 @@ def overrideDateData(model) :
88
now = DateTimeHelper.dateTimeNow()
99
model.createdAt = getValueOrDefault(model.createdAt, now)
1010
model.updatedAt = now
11+
12+
def overrideUserData(model, loggedUser) :
13+
model.createdBy = getValueOrDefault(model.createdBy, loggedUser)
14+
model.updatedBy = loggedUser
15+
16+
def overrideData(model, loggedUser) :
17+
overrideDateData(model)
18+
overrideUserData(model, loggedUser)
19+
20+
def toResponseDtoList(dtoList, toClass) :
21+
if not (ObjectHelper.isNone(dtoList) or ObjectHelper.isNone(toClass)) :
22+
return Serializer.convertFromObjectToObject(dtoList, toClass)
23+
return dtoList
24+
25+
def toResponseDto(dto, toClass) :
26+
if not (ObjectHelper.isNone(dto) or ObjectHelper.isNone(toClass)) :
27+
return Serializer.convertFromObjectToObject(dto, toClass)
28+
return dto

‎python_framework/api/src/helper/Serializer.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ def jsonifyIt(instance, fieldsToExpand=[EXPAND_ALL_FIELDS]) :
8181

8282
@Function
8383
def serializeIt(fromJson, toClass, fatherClass=None) :
84-
# print(fromJson)
85-
# print(f'toClass: {toClass}')
84+
# print(f'fromJson: {fromJson}, toClass: {toClass}, fatherClass: {fatherClass}')
8685
if ObjectHelper.isDictionary(fromJson) and ObjectHelper.isDictionaryClass(toClass) :
8786
# objectInstance = {}
8887
# for key, value in fromJson.items() :
@@ -135,6 +134,7 @@ def serializeIt(fromJson, toClass, fatherClass=None) :
135134
args.append(value)
136135
del kwargs[key]
137136
# print(f'args = {args}, kwargs = {kwargs}')
137+
# print(f'args = {args}, kwargs = {kwargs}')
138138
if ObjectHelper.isNone(objectInstance) :
139139
raise Exception(f'Not possible to instanciate {ReflectionHelper.getName(toClass, muteLogs=True)} class')
140140
# print(objectInstance)
@@ -148,6 +148,7 @@ def serializeIt(fromJson, toClass, fatherClass=None) :
148148
def convertFromJsonToObject(fromJson, toClass, fatherClass=None) :
149149
if ObjectHelper.isNone(fatherClass) :
150150
fatherClass = toClass
151+
# print(f'isSerializerList(toClass): {isSerializerList(toClass)}')
151152
if isSerializerList(toClass) :
152153
for innerToObjectClass in toClass :
153154
if isSerializerList(innerToObjectClass) :
@@ -163,8 +164,11 @@ def convertFromJsonToObject(fromJson, toClass, fatherClass=None) :
163164

164165
@Function
165166
def convertFromObjectToObject(fromObject, toClass) :
167+
# print(f'jsonifyIt({fromObject}): {jsonifyIt(fromObject)}, toClass: {toClass}')
166168
fromJson = json.loads(jsonifyIt(fromObject))
169+
# print(f'fromJson: {fromJson}')
167170
# log.prettyPython(convertFromObjectToObject, 'convertFromObjectToObject', fromJson, logLevel=log.DEBUG)
171+
# print(f'convertFromJsonToObject(fromJson, toClass): {convertFromJsonToObject(fromJson, toClass)}')
168172
return convertFromJsonToObject(fromJson, toClass)
169173

170174
@Function

‎python_framework/api/src/service/flask/ResourceManager.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,19 @@ def getResourceList(apiInstance, resourceType) :
8282
resourceNameList += getPythonFrameworkResourceByType(resourceType)
8383
resourceList = []
8484
for resourceName in resourceNameList :
85+
resource = None
8586
ajustedResourceName = getResourceNameAjusted(apiInstance, resourceType, resourceName)
8687
ajustedResourceModuleName = getResourceModuleNameAjusted(apiInstance, resourceType, resourceName)
8788
if isControllerResourceName(resourceName) :
88-
resourceList += getControllerList(ajustedResourceName, ajustedResourceModuleName)
89+
resource = getControllerList(ajustedResourceName, ajustedResourceModuleName)
8990
else :
9091
resource = globals.importResource(ajustedResourceName, resourceModuleName=ajustedResourceModuleName)
91-
if resource :
92-
resourceList.append(resource)
92+
if ObjectHelper.isEmpty(resource) :
93+
raise Exception(f'Error while importing {ajustedResourceName} resource from {ajustedResourceModuleName} module. Resource not found.')
94+
elif ObjectHelper.isList(resource) :
95+
resourceList += resource
96+
else :
97+
resourceList.append(resource)
9398
return resourceList
9499

95100
@Function

‎python_framework/api/src/service/openapi/OpenApiManager.py‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
]
2424

2525
ABLE_TO_RECIEVE_BODY_LIST = [
26-
KW_POST,
27-
KW_PUT,
28-
KW_PATCH
26+
KW_POST
27+
, KW_PUT
28+
# , KW_PATCH
2929
]
3030

3131
DEFAULT_CONTENT_TYPE = 'application/json'
@@ -244,7 +244,7 @@ def addDtoToUrlVerb(verb, url, dtoClass, documentation, dtoType=v.OBJECT, where=
244244
k.DESCRIPTION : None,
245245
k.SCHEMA : getDtoSchema(dtoName, dtoType, dtoClass)
246246
})
247-
if KW_RESPONSE == where :
247+
elif KW_RESPONSE == where :
248248
documentation[k.PATHS][url][verb][k.RESPONSES][k.DEFAULT_STATUS_CODE] = {
249249
k.DESCRIPTION : v.DEFAULT_RESPONSE,
250250
k.SCHEMA : getDtoSchema(dtoName, dtoType, dtoClass)
@@ -257,7 +257,7 @@ def addDtoToUrlVerb(verb, url, dtoClass, documentation, dtoType=v.OBJECT, where=
257257
dtoClassDoc[k.REQUIRED] = ReflectionHelper.getAttributeNameList(dtoClass)
258258
for attributeName in dtoClassDoc[k.REQUIRED] :
259259
attributeType = getTypeFromAttributeNameAndChildDtoClass(attributeName, dtoType)
260-
childDtoClass = getNullableChildDtoClass(attributeName, dtoClass, verb, url, documentation, where=where)
260+
childDtoClass = getNullableChildDtoClass(attributeName, dtoClass, verb, url, documentation)
261261
if childDtoClass :
262262
dtoClassDoc[k.PROPERTIES][attributeName] = getDtoSchema(attributeName, attributeType, childDtoClass)
263263
else :
@@ -276,7 +276,7 @@ def addDtoToUrlVerb(verb, url, dtoClass, documentation, dtoType=v.OBJECT, where=
276276
k.DESCRIPTION : None,
277277
k.SCHEMA : getDtoSchema(dtoName, dtoType, dtoClass)
278278
})
279-
if KW_RESPONSE == where :
279+
elif KW_RESPONSE == where :
280280
documentation[k.PATHS][url][verb][k.RESPONSES][k.DEFAULT_STATUS_CODE] = {
281281
k.DESCRIPTION : v.DEFAULT_RESPONSE,
282282
k.SCHEMA : getDtoSchema(dtoName, dtoType, dtoClass)
@@ -352,13 +352,13 @@ def addSecurity(verb, url, roleRequired, documentation):
352352
})
353353

354354

355-
def getNullableChildDtoClass(attributeName, dtoClass, verb, url, documentation, where=None):
355+
def getNullableChildDtoClass(attributeName, dtoClass, verb, url, documentation):
356356
log.log(getNullableChildDtoClass, f'attributeName: {attributeName}, dtoClass: {dtoClass}, verb: {verb}, url: {url}')
357357
childDtoClass = Serializer.getTargetClassFromFatherClassAndChildMethodName(dtoClass, attributeName)
358358
log.log(getNullableChildDtoClass, f'childDtoClass: {childDtoClass}')
359359
if childDtoClass :
360360
if ReflectionHelper.getName(type(type)) == ReflectionHelper.getName(type(childDtoClass)) :
361-
addDtoToUrlVerb(verb, url, childDtoClass, documentation, where=where)
361+
addDtoToUrlVerb(verb, url, childDtoClass, documentation)
362362
else :
363-
addDtoToUrlVerb(verb, url, type(childDtoClass), documentation, where=where)
363+
addDtoToUrlVerb(verb, url, type(childDtoClass), documentation)
364364
return childDtoClass

‎python_framework/api/test/api/src/helper/SerializerTest.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,3 +682,38 @@ def convertFromJsonToObject_whenThereAreEnums() :
682682
],
683683
Serializer.getObjectAsDictionary(toAssert)
684684
)
685+
686+
@Test()
687+
def weirdIdList() :
688+
#arrange
689+
class TestContact(MODEL):
690+
__tablename__ = 'TestContact'
691+
id = sap.Column(sap.Integer(), sap.Sequence(f'{__tablename__}{sap.ID}{sap.SEQ}'), primary_key=True)
692+
key = sap.Column(sap.String(128), nullable=False)
693+
def __init__(self,
694+
id = None,
695+
key = None
696+
):
697+
self.id = id
698+
self.key = key
699+
def __repr__(self):
700+
return f'{self.__tablename__}(id: {self.id}, key: {self.key})'
701+
class TestContactRequestDto :
702+
def __init__(self,
703+
key = None
704+
) :
705+
self.key = key
706+
class TestContactResponseDto :
707+
def __init__(self,
708+
id = None,
709+
key = None
710+
) :
711+
self.id = id
712+
self.key = key
713+
model = TestContact(id=2, key="a")
714+
715+
#act
716+
dto = Serializer.convertFromObjectToObject(model, TestContactResponseDto)
717+
718+
assert int == type(dto.id)
719+
assert 2 == dto.id

‎setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
sudo apt install libpq-dev python3-dev
66
pip3.9 install --no-cache-dir python-framework --force --upgrade''')
77

8-
VERSION = '0.1.10-26'
8+
VERSION = '0.1.10-27'
99
NAME = 'python_framework'
1010
URL = f'https://github.com/SamuelJansen/{NAME}/'
1111

0 commit comments

Comments
 (0)