-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
teach xsd.Schema how to merge type maps #132
base: master
Are you sure you want to change the base?
Conversation
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.
previously when a single target namespace was spread across multiple files, the last schema file read for each target namespace would clobber any previously read type maps this commit adds some minimal target namespace collision detection and delegates to the xsd.Schema class the responsibility to resolve that conflict.
// the receiving schema, returns error if the schemas have | ||
// different target namespaces. | ||
func (s *Schema) MergeTypes(incoming *Schema) error { | ||
if !strings.EqualFold(s.TargetNS, incoming.TargetNS) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was under the impression that XML namespaces are case sensitive. What is the reason for using EqualFold over == here?
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) |
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. |
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.