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

Function GenerateBodyDefinitions

pkg/codegen/operations.go:925–1029  ·  view source on GitHub ↗

GenerateBodyDefinitions turns the Swagger body definitions into a list of our body definitions which will be used for code generation. pathItemRef is the path item's $ref (if any). When non-empty and pointing at an external file, the body type that would otherwise be hoisted locally is replaced by

(operationID string, bodyOrRef *openapi3.RequestBodyRef, pathItemRef string)

Source from the content-addressed store, hash-verified

923// redeclaring locally would just produce an awkward duplicate with
924// package-qualified union elements.
925func GenerateBodyDefinitions(operationID string, bodyOrRef *openapi3.RequestBodyRef, pathItemRef string) ([]RequestBodyDefinition, []TypeDefinition, error) {
926 if bodyOrRef == nil {
927 return nil, nil, nil
928 }
929 body := bodyOrRef.Value
930
931 var bodyDefinitions []RequestBodyDefinition
932 var typeDefinitions []TypeDefinition
933
934 for _, contentType := range SortedMapKeys(body.Content) {
935 content := body.Content[contentType]
936 var tag string
937 var defaultBody bool
938
939 switch {
940 case contentType == "application/json":
941 tag = "JSON"
942 defaultBody = true
943 case util.IsMediaTypeJson(contentType):
944 tag = mediaTypeToCamelCase(contentType)
945 case strings.HasPrefix(contentType, "multipart/"):
946 tag = "Multipart"
947 case contentType == "application/x-www-form-urlencoded":
948 tag = "Formdata"
949 case contentType == "text/plain":
950 tag = "Text"
951 default:
952 bd := RequestBodyDefinition{
953 Required: body.Required,
954 ContentType: contentType,
955 }
956 bodyDefinitions = append(bodyDefinitions, bd)
957 continue
958 }
959
960 bodyTypeName := operationID + tag + "Body"
961 bodySchema, err := GenerateGoSchema(content.Schema, []string{bodyTypeName})
962 if err != nil {
963 return nil, nil, fmt.Errorf("error generating request body definition: %w", err)
964 }
965
966 // If the body is a pre-defined type
967 if content.Schema != nil && IsGoTypeReference(content.Schema.Ref) {
968 // Convert the reference path to Go type
969 refType, err := RefPathToGoType(content.Schema.Ref)
970 if err != nil {
971 return nil, nil, fmt.Errorf("error turning reference (%s) into a Go type: %w", content.Schema.Ref, err)
972 }
973 bodySchema.RefType = refType
974 }
975
976 // If the request has a body, but it's not a user defined
977 // type under #/components, we'll define a type for it, so
978 // that we have an easy to use type for marshaling.
979 if bodySchema.RefType == "" {
980 if externalPkg := externalPackageFor(pathItemRef); externalPkg != "" {
981 // The operation's path item came from an external file; the
982 // imported package already declares this body type with the

Callers 1

OperationDefinitionsFunction · 0.85

Calls 8

IsMediaTypeJsonFunction · 0.92
SortedMapKeysFunction · 0.85
mediaTypeToCamelCaseFunction · 0.85
GenerateGoSchemaFunction · 0.85
IsGoTypeReferenceFunction · 0.85
RefPathToGoTypeFunction · 0.85
externalPackageForFunction · 0.85
GenStructFromSchemaFunction · 0.85

Tested by

no test coverage detected