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

Function GenerateResponseDefinitions

pkg/codegen/operations.go:1031–1167  ·  view source on GitHub ↗
(operationID string, responses map[string]*openapi3.ResponseRef, pathItemRef string)

Source from the content-addressed store, hash-verified

1029}
1030
1031func GenerateResponseDefinitions(operationID string, responses map[string]*openapi3.ResponseRef, pathItemRef string) ([]ResponseDefinition, error) {
1032 externalPkg := externalPackageFor(pathItemRef)
1033
1034 var responseDefinitions []ResponseDefinition
1035 // do not let multiple status codes ref to same response, it will break the type switch
1036 refSet := make(map[string]struct{})
1037
1038 for _, statusCode := range SortedMapKeys(responses) {
1039 responseOrRef := responses[statusCode]
1040 if responseOrRef == nil {
1041 continue
1042 }
1043 response := responseOrRef.Value
1044
1045 var responseContentDefinitions []ResponseContentDefinition
1046
1047 for _, contentType := range SortedMapKeys(response.Content) {
1048 content := response.Content[contentType]
1049 var tag string
1050 switch {
1051 case contentType == "application/json":
1052 tag = "JSON"
1053 case util.IsMediaTypeJson(contentType):
1054 tag = mediaTypeToCamelCase(contentType)
1055 case contentType == "application/x-www-form-urlencoded":
1056 tag = "Formdata"
1057 case strings.HasPrefix(contentType, "multipart/"):
1058 tag = "Multipart"
1059 case contentType == "text/plain":
1060 tag = "Text"
1061 default:
1062 rcd := ResponseContentDefinition{
1063 ContentType: contentType,
1064 }
1065 responseContentDefinitions = append(responseContentDefinitions, rcd)
1066 continue
1067 }
1068
1069 responseTypeName := operationID + statusCode + tag + "Response"
1070 // The strict-server envelope keeps the bare ...Response name
1071 // (e.g. "GetPing200JSONResponse"); the hoisted body type is
1072 // suffixed so the envelope can reference it without colliding
1073 // (the strict envelope is sometimes a struct that wraps the
1074 // body in a Body field, which would self-reference if the
1075 // names matched).
1076 responseBodyTypeName := responseTypeName + "Body"
1077 contentSchema, err := GenerateGoSchema(content.Schema, []string{responseBodyTypeName})
1078 if err != nil {
1079 return nil, fmt.Errorf("error generating request body definition: %w", err)
1080 }
1081
1082 // Hoist inline response-root schemas that need method-emitting
1083 // boilerplate (UnionElements / AdditionalProperties) to a
1084 // synthetic top-level TypeDefinition. The hoisted typedef flows
1085 // via op.TypeDefinitions (collected in
1086 // GenerateTypeDefsForOperation) and gets declared once via
1087 // typedef.tmpl with full union/additionalProperties methods.
1088 // The strict-server template references it as the body type

Callers 2

GenerateFunction · 0.85
OperationDefinitionsFunction · 0.85

Calls 9

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

Tested by

no test coverage detected