@@ -192,7 +192,7 @@ def get_metaschema() -> Tuple[Names, List[Dict[str, str]], Loader]:
192192 _logger .error ("%s" , j2 )
193193 raise SchemaParseException (f"Not a list: { j2 } " )
194194 else :
195- sch_obj = make_avro (j2 , loader )
195+ sch_obj = make_avro (j2 , loader , loader . vocab )
196196 try :
197197 sch_names = make_avro_schema_from_avro (sch_obj )
198198 except SchemaParseException :
@@ -258,7 +258,7 @@ def load_schema(
258258
259259 # Make the Avro validation that will be used to validate the target
260260 # document
261- avsc_names = make_avro_schema (schema_doc , document_loader )
261+ avsc_names = make_avro_schema (schema_doc , document_loader , metaschema_loader . vocab )
262262
263263 return document_loader , avsc_names , schema_metadata , metaschema_loader
264264
@@ -350,6 +350,7 @@ def validate_doc(
350350 raise_ex = False ,
351351 skip_foreign_properties = loader .skip_schemas ,
352352 strict_foreign_properties = strict_foreign_properties ,
353+ vocab = loader .vocab ,
353354 )
354355 if success :
355356 break
@@ -372,6 +373,7 @@ def validate_doc(
372373 raise_ex = True ,
373374 skip_foreign_properties = loader .skip_schemas ,
374375 strict_foreign_properties = strict_foreign_properties ,
376+ vocab = loader .vocab ,
375377 )
376378 except ClassValidationException as exc1 :
377379 errors = [
@@ -493,42 +495,6 @@ def replace_type(
493495 return items
494496
495497
496- primitives = {
497- "http://www.w3.org/2001/XMLSchema#string" : "string" ,
498- "http://www.w3.org/2001/XMLSchema#boolean" : "boolean" ,
499- "http://www.w3.org/2001/XMLSchema#int" : "int" ,
500- "http://www.w3.org/2001/XMLSchema#long" : "long" ,
501- "http://www.w3.org/2001/XMLSchema#float" : "float" ,
502- "http://www.w3.org/2001/XMLSchema#double" : "double" ,
503- saladp + "null" : "null" ,
504- saladp + "enum" : "enum" ,
505- saladp + "array" : "array" ,
506- saladp + "record" : "record" ,
507- }
508-
509-
510- def avro_type_name (url : str ) -> str :
511- """
512- Turn a URL into an Avro-safe name.
513-
514- If the URL has no fragment, return this plain URL.
515-
516- Extract either the last part of the URL fragment past the slash, otherwise
517- the whole fragment.
518- """
519- global primitives
520-
521- if url in primitives :
522- return primitives [url ]
523-
524- if url .startswith ("http://" ):
525- url = url [7 :]
526- elif url .startswith ("https://" ):
527- url = url [8 :]
528- url = url .replace ("/" , "." ).replace ("#" , "." )
529- return url
530-
531-
532498def avro_field_name (url : str ) -> str :
533499 """
534500 Turn a URL into an Avro-safe name.
@@ -553,18 +519,24 @@ def make_valid_avro(
553519 found : Set [str ],
554520 union : bool = False ,
555521 fielddef : bool = False ,
522+ vocab : Optional [Dict [str , str ]] = None ,
556523) -> Union [
557524 Avro , MutableMapping [str , str ], str , List [Union [Any , MutableMapping [str , str ], str ]]
558525]:
559526 """Convert our schema to be more avro like."""
527+
528+ if vocab is None :
529+ _ , _ , metaschema_loader = get_metaschema ()
530+ vocab = metaschema_loader .vocab
531+
560532 # Possibly could be integrated into our fork of avro/schema.py?
561533 if isinstance (items , MutableMapping ):
562534 avro = copy .copy (items )
563535 if avro .get ("name" ) and avro .get ("inVocab" , True ):
564536 if fielddef :
565537 avro ["name" ] = avro_field_name (avro ["name" ])
566538 else :
567- avro ["name" ] = avro_type_name (avro ["name" ])
539+ avro ["name" ] = validate . avro_type_name (avro ["name" ])
568540
569541 if "type" in avro and avro ["type" ] in (
570542 saladp + "record" ,
@@ -585,6 +557,7 @@ def make_valid_avro(
585557 found ,
586558 union = True ,
587559 fielddef = (field == "fields" ),
560+ vocab = vocab ,
588561 )
589562 if "symbols" in avro :
590563 avro ["symbols" ] = [avro_field_name (sym ) for sym in avro ["symbols" ]]
@@ -593,13 +566,20 @@ def make_valid_avro(
593566 ret = []
594567 for i in items :
595568 ret .append (
596- make_valid_avro (i , alltypes , found , union = union , fielddef = fielddef )
569+ make_valid_avro (
570+ i , alltypes , found , union = union , fielddef = fielddef , vocab = vocab
571+ )
597572 )
598573 return ret
599574 if union and isinstance (items , str ):
600- if items in alltypes and avro_type_name (items ) not in found :
601- return make_valid_avro (alltypes [items ], alltypes , found , union = union )
602- return avro_type_name (items )
575+ if items in alltypes and validate .avro_type_name (items ) not in found :
576+ return make_valid_avro (
577+ alltypes [items ], alltypes , found , union = union , vocab = vocab
578+ )
579+ if items in vocab :
580+ return validate .avro_type_name (vocab [items ])
581+ else :
582+ return validate .avro_type_name (items )
603583 else :
604584 return items
605585
@@ -697,7 +677,7 @@ def extend_and_specialize(
697677 for ex in aslist (result ["extends" ]):
698678 if ex_types [ex ].get ("abstract" ):
699679 add_dictlist (extended_by , ex , ex_types [result ["name" ]])
700- add_dictlist (extended_by , avro_type_name (ex ), ex_types [ex ])
680+ add_dictlist (extended_by , validate . avro_type_name (ex ), ex_types [ex ])
701681
702682 for result in results :
703683 if result .get ("abstract" ) and result ["name" ] not in extended_by :
@@ -717,27 +697,28 @@ def extend_and_specialize(
717697def make_avro (
718698 i : List [Dict [str , Any ]],
719699 loader : Loader ,
700+ metaschema_vocab : Optional [Dict [str , str ]] = None ,
720701) -> List [Any ]:
721702
722703 j = extend_and_specialize (i , loader )
723704
724705 name_dict = {} # type: Dict[str, Dict[str, Any]]
725706 for entry in j :
726707 name_dict [entry ["name" ]] = entry
727- avro = make_valid_avro (j , name_dict , set ())
708+
709+ avro = make_valid_avro (j , name_dict , set (), vocab = metaschema_vocab )
728710
729711 return [
730712 t
731713 for t in avro
732714 if isinstance (t , MutableMapping )
733715 and not t .get ("abstract" )
734- and t .get ("type" ) != "documentation"
716+ and t .get ("type" ) != "org.w3id.cwl.salad. documentation"
735717 ]
736718
737719
738720def make_avro_schema (
739- i : List [Any ],
740- loader : Loader ,
721+ i : List [Any ], loader : Loader , metaschema_vocab : Optional [Dict [str , str ]] = None
741722) -> Names :
742723 """
743724 All in one convenience function.
@@ -746,7 +727,7 @@ def make_avro_schema(
746727 the intermediate result for diagnostic output.
747728 """
748729 names = Names ()
749- avro = make_avro (i , loader )
730+ avro = make_avro (i , loader , metaschema_vocab )
750731 make_avsc_object (convert_to_dict (avro ), names )
751732 return names
752733
0 commit comments