From abaaaa9420dd10e65303cf6e19257b5fb7102457 Mon Sep 17 00:00:00 2001 From: Eiko Thomas Date: Fri, 15 Mar 2024 20:59:30 +0100 Subject: [PATCH] add format to StringType --- docs/puml/yacg_model.puml | 2 ++ resources/models/json/yacg_model_schema.json | 7 +++++-- tests/builder/test_json_builder.py | 13 ++++++++++--- .../models/json/examples/single_type_schema.json | 12 ++++++++++-- yacg/builder/impl/dictionaryBuilder.py | 7 +++---- yacg/model/model.py | 7 ++++++- yacg/model/shared/info.py | 4 ++++ 7 files changed, 40 insertions(+), 12 deletions(-) diff --git a/docs/puml/yacg_model.puml b/docs/puml/yacg_model.puml index 0e1e1855..b0b0aa2a 100644 --- a/docs/puml/yacg_model.puml +++ b/docs/puml/yacg_model.puml @@ -67,6 +67,7 @@ class "**StringType**" as StringType { NumberType minLength NumberType maxLength StringType pattern + StringType format } note top: integer values @@ -275,6 +276,7 @@ note top: Type describes the reference of\na property to another field\nin the m + diff --git a/resources/models/json/yacg_model_schema.json b/resources/models/json/yacg_model_schema.json index 3ca3dee9..8e97c1ff 100644 --- a/resources/models/json/yacg_model_schema.json +++ b/resources/models/json/yacg_model_schema.json @@ -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": { @@ -145,6 +145,9 @@ }, "pattern": { "type": "string" + }, + "format": { + "type": "string" } } } @@ -737,4 +740,4 @@ } } } -} \ No newline at end of file +} diff --git a/tests/builder/test_json_builder.py b/tests/builder/test_json_builder.py index c4bc9054..3901a629 100644 --- a/tests/builder/test_json_builder.py +++ b/tests/builder/test_json_builder.py @@ -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) @@ -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)) @@ -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)) @@ -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): @@ -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' diff --git a/tests/resources/models/json/examples/single_type_schema.json b/tests/resources/models/json/examples/single_type_schema.json index d88cb9c8..8e1a63d2 100644 --- a/tests/resources/models/json/examples/single_type_schema.json +++ b/tests/resources/models/json/examples/single_type_schema.json @@ -9,7 +9,8 @@ "type": "string", "minLength": 2, "maxLength": 200, - "pattern": "^\\d$" + "pattern": "^\\d$", + "format": "dummy" }, "aValue": { "type": "number", @@ -47,8 +48,15 @@ }, "another2": { "type": "number" + }, + "another3": { + "type": "string", + "format": "email" + }, + "another4": { + "type": "string" } } } } -} \ No newline at end of file +} diff --git a/yacg/builder/impl/dictionaryBuilder.py b/yacg/builder/impl/dictionaryBuilder.py index 851f0580..6ec2928b 100644 --- a/yacg/builder/impl/dictionaryBuilder.py +++ b/yacg/builder/impl/dictionaryBuilder.py @@ -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): diff --git a/yacg/model/model.py b/yacg/model/model.py index 1bb59943..0ef31417 100644 --- a/yacg/model/model.py +++ b/yacg/model/model.py @@ -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 @@ -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 @@ -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 @@ -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: diff --git a/yacg/model/shared/info.py b/yacg/model/shared/info.py index 5cae2b98..6364d659 100644 --- a/yacg/model/shared/info.py +++ b/yacg/model/shared/info.py @@ -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