(outSchema *Schema, elements openapi3.SchemaRefs, discriminator *openapi3.Discriminator, path []string)
| 930 | } |
| 931 | |
| 932 | func generateUnion(outSchema *Schema, elements openapi3.SchemaRefs, discriminator *openapi3.Discriminator, path []string) error { |
| 933 | if discriminator != nil { |
| 934 | outSchema.Discriminator = &Discriminator{ |
| 935 | Property: discriminator.PropertyName, |
| 936 | Mapping: make(map[string]string), |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | refToGoTypeMap := make(map[string]string) |
| 941 | for i, element := range elements { |
| 942 | elementPath := append(path, fmt.Sprint(i)) |
| 943 | elementSchema, err := GenerateGoSchema(element, elementPath) |
| 944 | if err != nil { |
| 945 | return err |
| 946 | } |
| 947 | |
| 948 | if element.Ref == "" { |
| 949 | elementName := SchemaNameToTypeName(PathToTypeName(elementPath)) |
| 950 | if elementSchema.TypeDecl() == elementName { |
| 951 | elementSchema.GoType = elementName |
| 952 | } else { |
| 953 | td := TypeDefinition{Schema: elementSchema, TypeName: elementName} |
| 954 | outSchema.AdditionalTypes = append(outSchema.AdditionalTypes, td) |
| 955 | elementSchema.GoType = td.TypeName |
| 956 | } |
| 957 | outSchema.AdditionalTypes = append(outSchema.AdditionalTypes, elementSchema.AdditionalTypes...) |
| 958 | } else { |
| 959 | refToGoTypeMap[element.Ref] = elementSchema.GoType |
| 960 | } |
| 961 | |
| 962 | if discriminator != nil { |
| 963 | if len(discriminator.Mapping) != 0 && element.Ref == "" { |
| 964 | return errors.New("ambiguous discriminator.mapping: please replace inlined object with $ref") |
| 965 | } |
| 966 | |
| 967 | // Explicit mapping. |
| 968 | var mapped bool |
| 969 | for k, v := range discriminator.Mapping { |
| 970 | if v.Ref == element.Ref { |
| 971 | outSchema.Discriminator.Mapping[k] = elementSchema.GoType |
| 972 | mapped = true |
| 973 | } |
| 974 | } |
| 975 | // Implicit mapping. |
| 976 | if !mapped { |
| 977 | outSchema.Discriminator.Mapping[RefPathToObjName(element.Ref)] = elementSchema.GoType |
| 978 | } |
| 979 | } |
| 980 | outSchema.UnionElements = append(outSchema.UnionElements, UnionElement(elementSchema.GoType)) |
| 981 | } |
| 982 | |
| 983 | if (outSchema.Discriminator != nil) && len(outSchema.Discriminator.Mapping) < len(elements) { |
| 984 | return errors.New("discriminator: not all schemas were mapped") |
| 985 | } |
| 986 | |
| 987 | return nil |
| 988 | } |
| 989 |
no test coverage detected