Skip to content

Commit c3ab445

Browse files
authored
Add more information to non-array with empty property exception. (#493)
1 parent 4a011ff commit c3ab445

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/compiler/Restler.Compiler/SwaggerVisitors.fs

+19-4
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,24 @@ module SwaggerVisitors =
296296
/// Returns the specified property when the object contains it.
297297
/// Note: if the example object does not contain the property,
298298
let extractPropertyFromObject propertyName (objectType:NJsonSchema.JsonObjectType)
299-
(exampleObj: JToken option) =
299+
(exampleObj: JToken option)
300+
(parents: NJsonSchema.JsonSchema list option) =
300301
if (String.IsNullOrWhiteSpace propertyName && objectType <> NJsonSchema.JsonObjectType.Array) then
301-
failwith (sprintf "non-array should always have a property name. Property type: %A" objectType)
302+
let message = sprintf "non-array should always have a property name. Property type: %A" objectType
303+
let propertyInfo =
304+
let parentPropertyNames =
305+
match parents with
306+
| None -> ""
307+
| Some parents ->
308+
parents
309+
|> Seq.map (fun p -> match p with
310+
| :? NJsonSchema.JsonSchemaProperty as jp ->
311+
jp.Name
312+
| _ -> "")
313+
|> Seq.filter (fun x -> not (String.IsNullOrEmpty x))
314+
|> String.concat "."
315+
sprintf "Property name: %A, parent properties: %s" propertyName parentPropertyNames
316+
failwith (sprintf "%s %s" message propertyInfo)
302317

303318
let pv, includeProperty =
304319
match exampleObj with
@@ -327,7 +342,7 @@ module SwaggerVisitors =
327342
pv, includeProperty
328343

329344
let extractPropertyFromArray (exampleObj: JToken option) =
330-
extractPropertyFromObject "" NJsonSchema.JsonObjectType.Array exampleObj
345+
extractPropertyFromObject "" NJsonSchema.JsonObjectType.Array exampleObj None
331346

332347
let formatJTokenProperty primitiveType (v:JToken) =
333348
// Use Formatting.None to avoid unintended string formatting, for example
@@ -616,7 +631,7 @@ module SwaggerVisitors =
616631
|> Seq.choose (fun item ->
617632
let name = item.Key
618633
// Just extract the property as a string.
619-
let exValue, includeProperty = GenerateGrammarElements.extractPropertyFromObject name NJsonSchema.JsonObjectType.String exampleValue
634+
let exValue, includeProperty = GenerateGrammarElements.extractPropertyFromObject name NJsonSchema.JsonObjectType.String exampleValue (Some parents)
620635
if not includeProperty then None
621636
else
622637
Some (processProperty (name, item.Value)

0 commit comments

Comments
 (0)