go-openapi / spec

openapi specification object model

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Expander: fails with circular ref based on "id"

fredbi opened this issue · comments

json-schema semantics with "id" fail to be expanded properly when references based on "id" define cycle.

This is not much of a problem for swagger specs ("id" is not supported), but this affects go-openapi validate, when it comes to run the json-schema test suite. The jsonschema test suite embeds a testcase with such verification (jsonschema_suite/refRemote.json).

The trick is to change the order of ID detection:

+       // expand definitions first, possibly getting some new IDs along the way
+       for k := range target.Definitions {
+               t, err := expandSchema(target.Definitions[k], parentRefs, resolver, basePath)
+               if shouldStopOnError(err, resolver.options) {
+                       return &target, err
+               }
+               if t != nil {
+                       target.Definitions[k] = *t
+               }
+       }
+
       t, err := expandItems(target, parentRefs, resolver, basePath)
       if shouldStopOnError(err, resolver.options) {
               return &target, err
@@ -889,15 +902,6 @@ func expandSchema(target Schema, parentRefs []string, resolver *schemaLoader, ba
                       *target.AdditionalItems.Schema = *t
               }
       }
-       for k := range target.Definitions {
-               t, err := expandSchema(target.Definitions[k], parentRefs, resolver, basePath)
-               if shouldStopOnError(err, resolver.options) {
-                       return &target, err
-               }
-               if t != nil {
-                       target.Definitions[k] = *t
-               }
-       }
       return &target, nil
}