go-xml icon indicating copy to clipboard operation
go-xml copied to clipboard

teach xsd.Schema how to merge type maps

Open davidalpert opened this issue 4 years ago • 2 comments

a new MergeTypes method is added to the xsd.Schema type which merges the types from one xsd.Schema into another provided that the TargetNS values are equivalent.

this MergeTypes method is then used when collecting the map of namespaces to schemas so that when a single schema is spread across multiple files (e.g. when imports are used to tie them together) the types from a namespace are all collected together, regardless of which files they came from.

this allows the resolvePartialTypes method to operate as expected, since following the imports will now merge the types into a single xsd.Schema.

davidalpert avatar Mar 11 '21 23:03 davidalpert

This is much needed otherwise schemas are overwritten if a file imports another one with the same namespace, and the error is misleading ("can't find type X" while what's happening is that only one file at a time can define types for a namespace)

john8329 avatar Sep 27 '24 09:09 john8329

One thing we should keep in consideration though is what happens if another file overwrites a type already defined. This will create misleading results, and in order for it to work we need to namespace types (giving prefixes for example) or generate files in separate go packages to avoid collisions. The way Swagger works is generating long names with the appropriate prefixes to give context for what objects they belong to.

john8329 avatar Sep 28 '24 08:09 john8329