genResponseUnmarshal generates unmarshaling steps for structured response payloads
(op *OperationDefinition)
| 129 | |
| 130 | // genResponseUnmarshal generates unmarshaling steps for structured response payloads |
| 131 | func genResponseUnmarshal(op *OperationDefinition) string { |
| 132 | var handledCaseClauses = make(map[string]string) |
| 133 | var unhandledCaseClauses = make(map[string]string) |
| 134 | |
| 135 | // Get the type definitions from the operation: |
| 136 | typeDefinitions, err := op.GetResponseTypeDefinitions() |
| 137 | if err != nil { |
| 138 | panic(err) |
| 139 | } |
| 140 | |
| 141 | if len(typeDefinitions) == 0 { |
| 142 | // No types. |
| 143 | return "" |
| 144 | } |
| 145 | |
| 146 | // Add a case for each possible response: |
| 147 | buffer := new(bytes.Buffer) |
| 148 | responses := op.Spec.Responses |
| 149 | for _, typeDefinition := range typeDefinitions { |
| 150 | |
| 151 | responseRef := responses.Value(typeDefinition.ResponseName) |
| 152 | if responseRef == nil { |
| 153 | continue |
| 154 | } |
| 155 | |
| 156 | // We can't do much without a value: |
| 157 | if responseRef.Value == nil { |
| 158 | fmt.Fprintf(os.Stderr, "Response %s.%s has nil value\n", op.OperationId, typeDefinition.ResponseName) |
| 159 | continue |
| 160 | } |
| 161 | |
| 162 | // If there is no content-type then we have no unmarshaling to do: |
| 163 | if len(responseRef.Value.Content) == 0 { |
| 164 | caseAction := "break // No content-type" |
| 165 | caseClauseKey := "case " + getConditionOfResponseName("rsp.StatusCode", typeDefinition.ResponseName) + ":" |
| 166 | unhandledCaseClauses[prefixLeastSpecific+caseClauseKey] = fmt.Sprintf("%s\n%s\n", caseClauseKey, caseAction) |
| 167 | continue |
| 168 | } |
| 169 | |
| 170 | // If we made it this far then we need to handle unmarshaling for each content-type: |
| 171 | SortedMapKeys := SortedMapKeys(responseRef.Value.Content) |
| 172 | jsonCount := 0 |
| 173 | for _, contentTypeName := range SortedMapKeys { |
| 174 | if slices.Contains(contentTypesJSON, contentTypeName) || util.IsMediaTypeJson(contentTypeName) { |
| 175 | jsonCount++ |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | for _, contentTypeName := range SortedMapKeys { |
| 180 | |
| 181 | // We get "interface{}" when using "anyOf" or "oneOf" (which doesn't work with Go types): |
| 182 | if typeDefinition.TypeName == "interface{}" { |
| 183 | // Unable to unmarshal this, so we leave it out: |
| 184 | continue |
| 185 | } |
| 186 | |
| 187 | // Add content-types here (json / yaml / xml etc): |
| 188 | switch { |
nothing calls this directly
no test coverage detected
searching dependent graphs…