@@ -123,6 +123,32 @@ def create_complex_type(self, parent: Element, **attributes) -> Element:
123123 """Create an XML complexType element."""
124124 return self .create_element (parent , "complexType" , ** attributes )
125125
126+ def add_custom_properties_as_appinfo (self , element : Element , type_def : Dict ) -> None :
127+ """Add custom JSON Structure properties as xs:appinfo annotations."""
128+ custom_props = {}
129+
130+ # Collect custom properties that should be preserved
131+ for key in ['altnames' , 'unit' , 'currency' , 'symbol' , 'contentEncoding' ,
132+ 'contentMediaType' , 'multipleOf' ]:
133+ if key in type_def :
134+ custom_props [key ] = type_def [key ]
135+
136+ if custom_props :
137+ # Find or create annotation element
138+ annotation = element .find (f"{{{ self .xmlns ['xs' ]} }}annotation" )
139+ if annotation is None :
140+ # Insert annotation as first child
141+ annotation = Element (f"{{{ self .xmlns ['xs' ]} }}annotation" )
142+ element .insert (0 , annotation )
143+
144+ # Add appinfo with custom properties
145+ appinfo = self .create_element (annotation , "appinfo" )
146+ appinfo .set ("source" , "json-structure-extensions" )
147+
148+ # Add custom properties as text content (JSON format)
149+ import json
150+ appinfo .text = json .dumps (custom_props , indent = 2 )
151+
126152 def create_simple_type_with_restriction (self , parent : Element , name : str , base_type : str , facets : Dict [str , Any ]) -> Element :
127153 """Create a simple type with restrictions (facets)."""
128154 simple_type = self .create_element (parent , "simpleType" , name = name )
@@ -369,12 +395,29 @@ def set_element_type(self, schema_root: Element, record_name: str, element: Elem
369395 if key in type_def :
370396 facets [key ] = type_def [key ]
371397
372- if facets :
398+ # Check for enum or const constraints
399+ has_enum = 'enum' in type_def
400+ has_const = 'const' in type_def
401+
402+ if facets or has_enum or has_const :
373403 # Create an anonymous simple type with restriction
374404 simple_type = self .create_element (element , "simpleType" )
375405 restriction = self .create_element (simple_type , "restriction" , base = xsd_type )
376406 element .attrib .pop ('type' , None ) # Remove type attribute
377407
408+ # Handle enum constraint
409+ if has_enum :
410+ enum_values = type_def ['enum' ]
411+ if isinstance (enum_values , list ):
412+ for enum_value in enum_values :
413+ self .create_element (restriction , "enumeration" , value = str (enum_value ))
414+
415+ # Handle const constraint (single enumeration value)
416+ elif has_const :
417+ const_value = type_def ['const' ]
418+ self .create_element (restriction , "enumeration" , value = str (const_value ))
419+
420+ # Handle other facets
378421 for facet_name , facet_value in facets .items ():
379422 if facet_name == 'minLength' :
380423 self .create_element (restriction , "minLength" , value = str (facet_value ))
@@ -436,6 +479,10 @@ def convert_object_properties(self, schema_root: Element, type_name: str, parent
436479 documentation = self .create_element (annotation , "documentation" )
437480 documentation .text = prop_def ['description' ]
438481
482+ # Add custom properties as appinfo
483+ if isinstance (prop_def , dict ):
484+ self .add_custom_properties_as_appinfo (prop_element , prop_def )
485+
439486 self .set_element_type (schema_root , type_name , prop_element , prop_def )
440487
441488 def process_type_definition (self , schema_root : Element , type_name : str , type_def : Dict ):
0 commit comments