Validate checks that the response definition is consistent: its status is set and the result type definition if any is valid.
(e *HTTPEndpointExpr)
| 132 | // Validate checks that the response definition is consistent: its status is set |
| 133 | // and the result type definition if any is valid. |
| 134 | func (r *HTTPResponseExpr) Validate(e *HTTPEndpointExpr) *eval.ValidationErrors { |
| 135 | verr := new(eval.ValidationErrors) |
| 136 | |
| 137 | if r.StatusCode == 0 { |
| 138 | verr.Add(r, "HTTP response status not defined") |
| 139 | } else if !bodyAllowedForStatus(r.StatusCode) && !e.MethodExpr.IsStreaming() { |
| 140 | ep, ok := r.Parent.(*HTTPEndpointExpr) |
| 141 | if ok && httpResponseBody(ep, r).Type != Empty { |
| 142 | verr.Add(r, "Response body defined for status code %d which does not allow response body.", r.StatusCode) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // text/html and text/plain can only encode strings so make sure there isn't |
| 147 | // an explicit conflict with the content-type and response. |
| 148 | if (r.ContentType == "text/html" || r.ContentType == "text/plain") && !e.SkipRequestBodyEncodeDecode { |
| 149 | if e.MethodExpr.Result.Type != nil && e.MethodExpr.Result.Type != String && e.MethodExpr.Result.Type != Bytes && r.Body == nil { |
| 150 | verr.Add(r, "Result type must be String or Bytes when ContentType is '%s'", r.ContentType) |
| 151 | } |
| 152 | if r.Body != nil && r.Body.Type != String && r.Body.Type != Bytes { |
| 153 | verr.Add(r, "Result type must be String or Bytes when ContentType is '%s'", r.ContentType) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | rt, isrt := e.MethodExpr.Result.Type.(*ResultTypeExpr) |
| 158 | resultAttributeType := func(name string) DataType { |
| 159 | if !IsObject(e.MethodExpr.Result.Type) { |
| 160 | return nil |
| 161 | } |
| 162 | if isrt { |
| 163 | if view, ok := e.MethodExpr.Result.Meta.Last(ViewMetaKey); ok { |
| 164 | v := rt.View(view) |
| 165 | if v == nil { |
| 166 | return nil |
| 167 | } |
| 168 | return v.AttributeExpr.Find(name).Type |
| 169 | } |
| 170 | for _, v := range rt.Views { |
| 171 | if !rt.ViewHasAttribute(v.Name, name) { |
| 172 | return nil |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | att := e.MethodExpr.Result.Find(name) |
| 177 | if att == nil || att.Type == nil { |
| 178 | // nil != nil |
| 179 | return nil |
| 180 | } |
| 181 | return att.Type |
| 182 | } |
| 183 | |
| 184 | var inview string |
| 185 | if isrt { |
| 186 | inview = " all views of" |
| 187 | } |
| 188 | |
| 189 | if !r.Headers.IsEmpty() { |
| 190 | verr.Merge(r.Headers.Validate("HTTP response headers", r)) |
| 191 | switch { |
nothing calls this directly
no test coverage detected