Skip to content

Commit

Permalink
Merge pull request #69 from OkieOth/x_ref_to_attribs
Browse files Browse the repository at this point in the history
x-ref to attribs
  • Loading branch information
OkieOth authored Feb 16, 2022
2 parents ba3f63d + 8d2d6e7 commit 9f8db5e
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 660 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 4.0.0
* extend x-ref/foreign key reference with a properties reference. **Attention** this may break some templates (e.g. plantUml)
* shows foreign key references in puml diagrams

# 3.4.1
* enable automatic releases with github actions

Expand Down
123 changes: 13 additions & 110 deletions docs/puml/yacg_model.puml
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,6 @@ class IntegerType {
IntegerType exclusiveMinimum
IntegerType maximum
IntegerType exclusiveMaximum
IntegerTypeFormatEnum format
IntegerType default
IntegerType minimum
IntegerType exclusiveMinimum
IntegerType maximum
IntegerType exclusiveMaximum
IntegerTypeFormatEnum format
IntegerType default
IntegerType minimum
IntegerType exclusiveMinimum
IntegerType maximum
IntegerType exclusiveMaximum
}

note top: integer values
Expand All @@ -49,18 +37,6 @@ class NumberType {
NumberType exclusiveMinimum
NumberType maximum
NumberType exclusiveMaximum
NumberTypeFormatEnum format
NumberType default
NumberType minimum
NumberType exclusiveMinimum
NumberType maximum
NumberType exclusiveMaximum
NumberTypeFormatEnum format
NumberType default
NumberType minimum
NumberType exclusiveMinimum
NumberType maximum
NumberType exclusiveMaximum
}

note top: floating point values
Expand All @@ -74,8 +50,6 @@ enum NumberTypeFormatEnum {

class BooleanType {
BooleanType default
BooleanType default
BooleanType default
}

note top: boolean values
Expand All @@ -86,23 +60,13 @@ class StringType {
NumberType minLength
NumberType maxLength
StringType pattern
StringType default
NumberType minLength
NumberType maxLength
StringType pattern
StringType default
NumberType minLength
NumberType maxLength
StringType pattern
}

note top: integer values

StringType --|> Type
class UuidType {
StringType default
StringType default
StringType default
}

note top: UUID values
Expand All @@ -117,22 +81,6 @@ class EnumType {
StringType[] values
StringType default
Tag[] tags
StringType version
StringType name
StringType domain
StringType source
StringType description
StringType[] values
StringType default
Tag[] tags
StringType version
StringType name
StringType domain
StringType source
StringType description
StringType[] values
StringType default
Tag[] tags
}

note top: type for enum values -\nfixed value types
Expand All @@ -151,16 +99,6 @@ class DateType {
DateType exclusiveMinimum
DateType maximum
DateType exclusiveMaximum
DateType default
DateType minimum
DateType exclusiveMinimum
DateType maximum
DateType exclusiveMaximum
DateType default
DateType minimum
DateType exclusiveMinimum
DateType maximum
DateType exclusiveMaximum
}

note top: type for date values
Expand All @@ -172,25 +110,13 @@ class DateTimeType {
DateTimeType exclusiveMinimum
DateTimeType maximum
DateTimeType exclusiveMaximum
DateTimeType default
DateTimeType minimum
DateTimeType exclusiveMinimum
DateTimeType maximum
DateTimeType exclusiveMaximum
DateTimeType default
DateTimeType minimum
DateTimeType exclusiveMinimum
DateTimeType maximum
DateTimeType exclusiveMaximum
}

note top: type for timestamp values

DateTimeType --|> Type
class BytesType {
BytesType default
BytesType default
BytesType default
}

note top: type for byte values, it\nwill usually be rendered to\na byte array
Expand All @@ -207,26 +133,6 @@ class ComplexType {
ComplexType[] referencedBy
Property[] properties
Tag[] tags
StringType version
StringType name
StringType description
StringType domain
StringType source
ComplexType extendsType
ComplexType[] extendedBy
ComplexType[] referencedBy
Property[] properties
Tag[] tags
StringType version
StringType name
StringType description
StringType domain
StringType source
ComplexType extendsType
ComplexType[] extendedBy
ComplexType[] referencedBy
Property[] properties
Tag[] tags
}

note top: complex type description
Expand Down Expand Up @@ -260,27 +166,19 @@ class DictionaryType {
ComplexType[] referencedBy
Type valueType
Tag[] tags
StringType version
StringType name
StringType description
StringType domain
StringType source
ComplexType[] referencedBy
Type valueType
Tag[] tags
StringType version
StringType name
StringType description
StringType domain
StringType source
ComplexType[] referencedBy
Type valueType
Tag[] tags
}

note top: key/value dictionary type. Keys are\nalways strings, the value type\ncan be specified

DictionaryType --|> Type
class ForeignKey {
Type type
StringType propertyName
Property property
}

note top: Type describes the reference of\na property to another field\nin the model




Expand Down Expand Up @@ -321,6 +219,11 @@ DictionaryType *-- Type

DictionaryType "0" *-- "n" Tag


ForeignKey *-- Type

ForeignKey *-- Property


footer \ngenerated with yacg (https://github.com/OkieOth/yacg),\n(template: plantUml.mako v1.1.0)\npowered by plantuml (https://plantuml.com/)
@enduml
15 changes: 15 additions & 0 deletions resources/models/json/yacg_model_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,21 @@
}
}
},
"ForeignKey": {
"description": "Type describes the reference of a property to another field in the model",
"type": "object",
"properties": {
"type": {
"$ref": "#/definitions/Type"
},
"propertyName": {
"type": "string"
},
"property": {
"$ref": "#/definitions/Property"
}
}
},
"Tag": {
"type": "object",
"description": "a tag type",
Expand Down
10 changes: 6 additions & 4 deletions tests/builder/test_json_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ def testSchemaWithExternalRef(self):
self._checkUpType(0, 'OneType', 2, modelTypes, [])
self._checkUpType(1, 'TwoType', 4, modelTypes, [])
# TwoType->implicitRef
self.assertIsNotNone(modelTypes[1].properties[3].foreignKey)
self.assertEqual(modelTypes[1].properties[2].type, modelTypes[1].properties[3].foreignKey)
self.assertIsNotNone(modelTypes[1].properties[3].foreignKey.type)
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)
self._checkUpType(2, 'AnotherType', 2, modelTypes, [])
self._checkUpType(3, 'DemoEnum', 0, modelTypes, [])

Expand All @@ -145,8 +147,8 @@ def testSchemaWithHttpRef(self):
self._checkUpType(0, 'OneType', 2, modelTypes, [])
self._checkUpType(1, 'TwoType', 4, modelTypes, [])
# TwoType->implicitRef
self.assertIsNotNone(modelTypes[1].properties[3].foreignKey)
self.assertEqual(modelTypes[1].properties[2].type, modelTypes[1].properties[3].foreignKey)
self.assertIsNotNone(modelTypes[1].properties[3].foreignKey.type)
self.assertEqual(modelTypes[1].properties[2].type, modelTypes[1].properties[3].foreignKey.type)
self._checkUpType(2, 'AnotherType', 2, modelTypes, [])
self._checkUpType(3, 'DemoEnum', 0, modelTypes, [])

Expand Down
4 changes: 4 additions & 0 deletions tests/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def testDictionaryType(self):
x = yacg.model.model.DictionaryType()
self.assertIsNotNone(x)

def testForeignKey(self):
x = yacg.model.model.ForeignKey()
self.assertIsNotNone(x)


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"implicitRef": {
"type": "string",
"format": "uuid",
"x-ref": "./single_type_schema.json#/definitions/AnotherType"
"x-ref": "./single_type_schema.json#/definitions/AnotherType.another2"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.4.0
4.0.0
30 changes: 28 additions & 2 deletions yacg/builder/impl/dictionaryBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import json
import yaml

from yacg.model.model import Property
from yacg.model.model import ForeignKey, Property
from yacg.util.stringUtils import toUpperCamelCase
from yacg.model.model import IntegerType, NumberType, BooleanType, NumberTypeFormatEnum, IntegerTypeFormatEnum
from yacg.model.model import StringType, UuidType, BytesType, ObjectType
Expand Down Expand Up @@ -137,6 +137,13 @@ def extractTypes(parsedSchema, modelFile, modelTypes, skipOpenApi=False):
modelFileContainer = ModelFileContainer(sourceFile, parsedSchema)
_getTypeFromParsedSchema(modelFileContainer, type.name, modelTypes)

# fix missing foreign key property references
for type in modelTypes:
if (hasattr(type, 'properties')):
for property in type.properties:
if (property.foreignKey) and (property.foreignKey.propertyName) and (not property.foreignKey.property):
property.foreignKey.property = __getPropertyByName(property.foreignKey.type, property.foreignKey.propertyName)

# load additional types, e.g. openapi.PathType
if not skipOpenApi:
if (parsedSchema.get('openapi', None) is not None) or (parsedSchema.get('swagger', None) is not None):
Expand All @@ -145,6 +152,14 @@ def extractTypes(parsedSchema, modelFile, modelTypes, skipOpenApi=False):
return modelTypes


def __getPropertyByName(type, propertyName):
if hasattr(type, "properties"):
for property in type.properties:
if property.name == propertyName:
return property
return None


def _extractTypeAndRelatedTypes(modelFileContainer, desiredTypeName, modelTypes):
"""extract the types from the parsed schema
Expand Down Expand Up @@ -362,7 +377,18 @@ def _extractAttributes(type, properties, modelTypes, modelFileContainer):
newProperty.isVisualKey = propDict.get('x-visualKey', False)
implicitRefEntry = propDict.get('x-ref', None)
if implicitRefEntry is not None:
newProperty.foreignKey = _extractReferenceType(implicitRefEntry, modelTypes, modelFileContainer)
# either {TYPE_NAME} or {TYPE_NAME}.{PROPERTY_NAME}
propertyRefName = None
lastDefSeparator = implicitRefEntry.find("#")
if lastDefSeparator != -1:
tmpStr = implicitRefEntry[lastDefSeparator + 1:]
lastDot = tmpStr.find(".")
if lastDot != -1:
implicitRefEntry = implicitRefEntry[0: lastDefSeparator + lastDot + 1]
propertyRefName = tmpStr[lastDot + 1:]
newProperty.foreignKey = ForeignKey()
newProperty.foreignKey.type = _extractReferenceType(implicitRefEntry, modelTypes, modelFileContainer)
newProperty.foreignKey.propertyName = propertyRefName
tags = propDict.get('x-tags', None)
if tags is not None:
newProperty.tags = _extractTags(tags)
Expand Down
Loading

0 comments on commit 9f8db5e

Please sign in to comment.