(allOf []*openapi3.SchemaRef, path []string)
| 10 | ) |
| 11 | |
| 12 | func mergeSchemasV1(allOf []*openapi3.SchemaRef, path []string) (Schema, error) { |
| 13 | var outSchema Schema |
| 14 | for _, schemaOrRef := range allOf { |
| 15 | ref := schemaOrRef.Ref |
| 16 | |
| 17 | var refType string |
| 18 | var err error |
| 19 | if IsGoTypeReference(ref) { |
| 20 | refType, err = RefPathToGoType(ref) |
| 21 | if err != nil { |
| 22 | return Schema{}, fmt.Errorf("error converting reference path to a go type: %w", err) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | schema, err := GenerateGoSchema(schemaOrRef, path) |
| 27 | if err != nil { |
| 28 | return Schema{}, fmt.Errorf("error generating Go schema in allOf: %w", err) |
| 29 | } |
| 30 | schema.RefType = refType |
| 31 | |
| 32 | for _, p := range schema.Properties { |
| 33 | err = outSchema.AddProperty(p) |
| 34 | if err != nil { |
| 35 | return Schema{}, fmt.Errorf("error merging properties: %w", err) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | if schema.HasAdditionalProperties { |
| 40 | if outSchema.HasAdditionalProperties { |
| 41 | // Both this schema, and the aggregate schema have additional |
| 42 | // properties, they must match. |
| 43 | if schema.AdditionalPropertiesType.TypeDecl() != outSchema.AdditionalPropertiesType.TypeDecl() { |
| 44 | return Schema{}, errors.New("additional properties in allOf have incompatible types") |
| 45 | } |
| 46 | } else { |
| 47 | // We're switching from having no additional properties to having |
| 48 | // them |
| 49 | outSchema.HasAdditionalProperties = true |
| 50 | outSchema.AdditionalPropertiesType = schema.AdditionalPropertiesType |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // Now, we generate the struct which merges together all the fields. |
| 56 | var err error |
| 57 | outSchema.GoType, err = GenStructFromAllOf(allOf, path) |
| 58 | if err != nil { |
| 59 | return Schema{}, fmt.Errorf("unable to generate aggregate type for AllOf: %w", err) |
| 60 | } |
| 61 | return outSchema, nil |
| 62 | } |
| 63 | |
| 64 | // GenStructFromAllOf generates an object that is the union of the objects in the |
| 65 | // input array. In the case of Ref objects, we use an embedded struct, otherwise, |
no test coverage detected
searching dependent graphs…