MCPcopy Index your code
hub / github.com/oapi-codegen/oapi-codegen / GenStructFromAllOf

Function GenStructFromAllOf

pkg/codegen/merge_schemas_v1.go:67–112  ·  view source on GitHub ↗

GenStructFromAllOf generates an object that is the union of the objects in the input array. In the case of Ref objects, we use an embedded struct, otherwise, we inline the fields.

(allOf []*openapi3.SchemaRef, path []string)

Source from the content-addressed store, hash-verified

65// input array. In the case of Ref objects, we use an embedded struct, otherwise,
66// we inline the fields.
67func GenStructFromAllOf(allOf []*openapi3.SchemaRef, path []string) (string, error) {
68 // Start out with struct {
69 objectParts := []string{"struct {"}
70 for _, schemaOrRef := range allOf {
71 ref := schemaOrRef.Ref
72 if IsGoTypeReference(ref) {
73 // We have a referenced type, we will generate an inlined struct
74 // member.
75 // struct {
76 // InlinedMember
77 // ...
78 // }
79 goType, err := RefPathToGoType(ref)
80 if err != nil {
81 return "", err
82 }
83 objectParts = append(objectParts,
84 fmt.Sprintf(" // Embedded struct due to allOf(%s)", ref))
85 objectParts = append(objectParts,
86 fmt.Sprintf(" %s `yaml:\",inline\"`", goType))
87 } else {
88 // Inline all the fields from the schema into the output struct,
89 // just like in the simple case of generating an object.
90 goSchema, err := GenerateGoSchema(schemaOrRef, path)
91 if err != nil {
92 return "", err
93 }
94 objectParts = append(objectParts, " // Embedded fields due to inline allOf schema")
95 objectParts = append(objectParts, GenFieldsFromProperties(goSchema.Properties)...)
96
97 if goSchema.HasAdditionalProperties {
98 addPropsType := goSchema.AdditionalPropertiesType.GoType
99 if goSchema.AdditionalPropertiesType.RefType != "" {
100 addPropsType = goSchema.AdditionalPropertiesType.RefType
101 }
102
103 additionalPropertiesPart := fmt.Sprintf("AdditionalProperties map[string]%s `json:\"-\"`", addPropsType)
104 if !slices.Contains(objectParts, additionalPropertiesPart) {
105 objectParts = append(objectParts, additionalPropertiesPart)
106 }
107 }
108 }
109 }
110 objectParts = append(objectParts, "}")
111 return strings.Join(objectParts, "\n"), nil
112}

Callers 1

mergeSchemasV1Function · 0.85

Calls 4

IsGoTypeReferenceFunction · 0.85
RefPathToGoTypeFunction · 0.85
GenerateGoSchemaFunction · 0.85
GenFieldsFromPropertiesFunction · 0.85

Tested by

no test coverage detected