Skip to content

Commit

Permalink
add format to StringType
Browse files Browse the repository at this point in the history
  • Loading branch information
Eiko Thomas committed Mar 15, 2024
1 parent 4d8a599 commit abaaaa9
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 12 deletions.
2 changes: 2 additions & 0 deletions docs/puml/yacg_model.puml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class "**StringType**" as StringType {
NumberType minLength
NumberType maxLength
StringType pattern
StringType format
}
note top: integer values

Expand Down Expand Up @@ -275,6 +276,7 @@ note top: Type describes the reference of\na property to another field\nin the m







Expand Down
7 changes: 5 additions & 2 deletions resources/models/json/yacg_model_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "yacg inner type model",
"description": "model types used internally to provide loaded models for the code generators",
"version": "5.6.6",
"version": "5.7.0",
"x-domain": "yacg.model.model",
"definitions": {
"Type": {
Expand Down Expand Up @@ -145,6 +145,9 @@
},
"pattern": {
"type": "string"
},
"format": {
"type": "string"
}
}
}
Expand Down Expand Up @@ -737,4 +740,4 @@
}
}
}
}
}
13 changes: 10 additions & 3 deletions tests/builder/test_json_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def testSingleTypeSchema(self):
self.assertEqual(mainType.properties[0].type.minLength, 2)
self.assertEqual(mainType.properties[0].type.maxLength, 200)
self.assertEqual(mainType.properties[0].type.pattern, "^\\d$")
self.assertEqual(mainType.properties[0].type.format, "dummy")

self.assertTrue(isinstance(mainType.properties[1].type, NumberType))
self.assertEqual(mainType.properties[1].type.minimum, 0.5)
Expand All @@ -49,7 +50,7 @@ def testSingleTypeSchema(self):
self.assertTrue(isinstance(mainType.properties[3].type, ComplexType))

self.assertIsNotNone(anotherType)
self.assertEqual(2, len(anotherType.properties))
self.assertEqual(4, len(anotherType.properties))
self.assertTrue(isinstance(anotherType.properties[0].type, DateTimeType))
self.assertTrue(isinstance(anotherType.properties[1].type, NumberType))

Expand All @@ -59,6 +60,12 @@ def testSingleTypeSchema(self):
self.assertIsNone(anotherType.properties[1].type.exclusiveMinimum)
self.assertIsNone(anotherType.properties[1].type.exclusiveMaximum)

self.assertTrue(isinstance(anotherType.properties[2].type, StringType))
self.assertEqual(anotherType.properties[2].type.format, "email")
self.assertTrue(isinstance(anotherType.properties[3].type, StringType))
self.assertIsNone(anotherType.properties[3].type.format)


self.assertIsNotNone(innerComplexType)
self.assertEqual(3, len(innerComplexType.properties))
self.assertTrue(isinstance(innerComplexType.properties[0].type, StringType))
Expand Down Expand Up @@ -212,7 +219,7 @@ def testSchemaWithExternalRef(self):
self.assertIsNotNone(modelTypes[1].properties[3].foreignKey.property)
self.assertEqual(modelTypes[1].properties[2].type, modelTypes[1].properties[3].foreignKey.type)
self.assertEqual(modelTypes[1].properties[3].foreignKey.property.name, modelTypes[1].properties[3].foreignKey.propertyName) # noqa: E501
self._checkUpType(2, 'AnotherType', 2, modelTypes, [])
self._checkUpType(2, 'AnotherType', 4, modelTypes, [])
self._checkUpType(3, 'DemoEnum', 0, modelTypes, [])

def testSchemaWithExternalRef_ignoreXref(self):
Expand Down Expand Up @@ -260,7 +267,7 @@ def testSchemaWithExternalCircularRefs(self):
self._checkUpType(1, 'RefBackType', 4, modelTypes, [])
self._checkUpType(2, 'RefBackType2', 3, modelTypes, [])
self._checkUpType(3, 'TwoType', 3, modelTypes, [])
self._checkUpType(4, 'AnotherType', 2, modelTypes, [])
self._checkUpType(4, 'AnotherType', 4, modelTypes, [])

def testSimpleAllOf(self):
modelFile = 'tests/resources/models/json/examples/simple_allof.json'
Expand Down
12 changes: 10 additions & 2 deletions tests/resources/models/json/examples/single_type_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"type": "string",
"minLength": 2,
"maxLength": 200,
"pattern": "^\\d$"
"pattern": "^\\d$",
"format": "dummy"
},
"aValue": {
"type": "number",
Expand Down Expand Up @@ -47,8 +48,15 @@
},
"another2": {
"type": "number"
},
"another3": {
"type": "string",
"format": "email"
},
"another4": {
"type": "string"
}
}
}
}
}
}
7 changes: 3 additions & 4 deletions yacg/builder/impl/dictionaryBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1086,10 +1086,9 @@ def _extractStringType(newTypeName, newProperty, propDict, modelTypes, modelFile
elif formatValue == 'byte':
return BytesType()
else:
logging.error(
"modelFile: %s, type=%s, property=%s: unknown string type format: %s"
% (modelFileContainer.fileName, newTypeName, newProperty.name, formatValue))
return StringType()
s = StringType()
s.format = formatValue
return s


def __initEnumValuesFromContent(enumType, enumValuesArray):
Expand Down
7 changes: 6 additions & 1 deletion yacg/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ def __init__(self, dictObj=None):
self.minLength = None
self.maxLength = None
self.pattern = None
self.format = None

if dictObj is not None:
d = vars(dictObj) if not isinstance(dictObj, dict) else dictObj
Expand All @@ -265,6 +266,8 @@ def toDict(self):
ret["maxLength"] = self.maxLength
if self.pattern is not None:
ret["pattern"] = self.pattern
if self.format is not None:
ret["format"] = self.format
return ret


Expand All @@ -280,6 +283,8 @@ def initFromDict(self, dictObj):

self.pattern = dictObj.get('pattern', None)

self.format = dictObj.get('format', None)


class UuidType (Type):
"""UUID values
Expand Down Expand Up @@ -412,7 +417,7 @@ def toDict(self):
if (self.values is not None) and (len(self.values) > 0):
ret["values"] = self.values
if (self.valuesMap is not None) and (len(self.valuesMap) > 0):
ret["valuesMap"] = self.valuesMap.toDict()
ret["valuesMap"] = self.valuesMap
if self.default is not None:
ret["default"] = self.default
if self.topLevelType is not None:
Expand Down
4 changes: 4 additions & 0 deletions yacg/model/shared/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ class InfoSection:
"""

def __init__(self, dictObj=None):

#: title of the model
self.title = None

#: version of the model
self.version = None
self.description = None
self.license = None
Expand Down

0 comments on commit abaaaa9

Please sign in to comment.