encoding/openapi: sort schema alphabetically

This makes the output more stable and minimizes diffs.

Change-Id: I9f73f8aa26d65c41f7ba09d0ba04da9746a9299b
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5403
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cmd/cue/cmd/testdata/script/def_openapi.txt b/cmd/cue/cmd/testdata/script/def_openapi.txt
index fa9cd51..a75de6e 100644
--- a/cmd/cue/cmd/testdata/script/def_openapi.txt
+++ b/cmd/cue/cmd/testdata/script/def_openapi.txt
@@ -27,6 +27,17 @@
     "paths": {},
     "components": {
         "schemas": {
+            "Bar": {
+                "type": "object",
+                "required": [
+                    "foo"
+                ],
+                "properties": {
+                    "foo": {
+                        "$ref": "#/components/schemas/Foo"
+                    }
+                }
+            },
             "Foo": {
                 "type": "object",
                 "required": [
@@ -43,17 +54,6 @@
                         "exclusiveMaximum": 10
                     }
                 }
-            },
-            "Bar": {
-                "type": "object",
-                "required": [
-                    "foo"
-                ],
-                "properties": {
-                    "foo": {
-                        "$ref": "#/components/schemas/Foo"
-                    }
-                }
             }
         }
     }
@@ -64,6 +64,13 @@
 paths: {}
 components:
     schemas:
+        Bar:
+            type: object
+            required:
+              - foo
+            properties:
+                foo:
+                    $ref: '#/components/schemas/Foo'
         Foo:
             type: object
             required:
@@ -76,18 +83,16 @@
                     type: integer
                     minimum: 0
                     exclusiveMaximum: 10
-        Bar:
-            type: object
-            required:
-              - foo
-            properties:
-                foo:
-                    $ref: '#/components/schemas/Foo'
 -- expect-cue-out --
 openapi: "3.0.0"
 info: {}
 paths: {}
 components: schemas: {
+	Bar: {
+		type: "object"
+		required: ["foo"]
+		properties: foo: $ref: "#/components/schemas/Foo"
+	}
 	Foo: {
 		type: "object"
 		required: ["a", "b"]
@@ -100,21 +105,16 @@
 			}
 		}
 	}
-	Bar: {
-		type: "object"
-		required: ["foo"]
-		properties: foo: $ref: "#/components/schemas/Foo"
-	}
 }
 -- expect-cue --
 package foo
 
+Bar :: {
+	foo: Foo
+	...
+}
 Foo :: {
 	a: int
 	b: >=0 & <10
 	...
 }
-Bar :: {
-	foo: Foo
-	...
-}
diff --git a/encoding/openapi/build.go b/encoding/openapi/build.go
index 2a90b8e..be55c39 100644
--- a/encoding/openapi/build.go
+++ b/encoding/openapi/build.go
@@ -136,6 +136,13 @@
 		}
 	}
 
+	a := c.schemas.Elts
+	sort.Slice(a, func(i, j int) bool {
+		x, _, _ := ast.LabelName(a[i].(*ast.Field).Label)
+		y, _, _ := ast.LabelName(a[j].(*ast.Field).Label)
+		return x < y
+	})
+
 	return (*ast.StructLit)(c.schemas), nil
 }
 
diff --git a/encoding/openapi/testdata/builtins.json b/encoding/openapi/testdata/builtins.json
index b5fec88..91db4e7 100644
--- a/encoding/openapi/testdata/builtins.json
+++ b/encoding/openapi/testdata/builtins.json
@@ -4,6 +4,10 @@
    "paths": {},
    "components": {
       "schemas": {
+         "Duration": {
+            "description": "This is not an OpenAPI type and has no format. In this case\nwe map to a type so that it can be documented properly (without\nrepeating it).",
+            "type": "string"
+         },
          "MyStruct": {
             "type": "object",
             "required": [
@@ -41,10 +45,6 @@
                   "type": "array"
                }
             }
-         },
-         "Duration": {
-            "description": "This is not an OpenAPI type and has no format. In this case\nwe map to a type so that it can be documented properly (without\nrepeating it).",
-            "type": "string"
          }
       }
    }
diff --git a/encoding/openapi/testdata/issue131.json b/encoding/openapi/testdata/issue131.json
index 1fc5e56..7257f5f 100644
--- a/encoding/openapi/testdata/issue131.json
+++ b/encoding/openapi/testdata/issue131.json
@@ -7,17 +7,6 @@
    "paths": {},
    "components": {
       "schemas": {
-         "Blocks": {
-            "type": "object",
-            "required": [
-               "block1"
-            ],
-            "properties": {
-               "block1": {
-                  "$ref": "#/components/schemas/Block"
-               }
-            }
-         },
          "Block": {
             "type": "object",
             "required": [
@@ -34,6 +23,17 @@
                   "exclusiveMaximum": 10
                }
             }
+         },
+         "Blocks": {
+            "type": "object",
+            "required": [
+               "block1"
+            ],
+            "properties": {
+               "block1": {
+                  "$ref": "#/components/schemas/Block"
+               }
+            }
          }
       }
    }
diff --git a/encoding/openapi/testdata/nested.json b/encoding/openapi/testdata/nested.json
index 07397c5..d390c64 100644
--- a/encoding/openapi/testdata/nested.json
+++ b/encoding/openapi/testdata/nested.json
@@ -4,9 +4,6 @@
    "paths": {},
    "components": {
       "schemas": {
-         "Struct.T": {
-            "type": "integer"
-         },
          "Struct": {
             "type": "object",
             "properties": {
@@ -17,6 +14,9 @@
                   "$ref": "#/components/schemas/Struct.T"
                }
             }
+         },
+         "Struct.T": {
+            "type": "integer"
          }
       }
    }
diff --git a/encoding/openapi/testdata/oneof-funcs.json b/encoding/openapi/testdata/oneof-funcs.json
index 816f5b3..19c7fe3 100644
--- a/encoding/openapi/testdata/oneof-funcs.json
+++ b/encoding/openapi/testdata/oneof-funcs.json
@@ -7,6 +7,187 @@
    "paths": {},
    "components": {
       "schemas": {
+         "EMBED": {
+            "description": "Randomly picked description from a set of size one.",
+            "type": "object",
+            "properties": {
+               "a": {
+                  "description": "Randomly picked description from a set of size one.",
+                  "type": "integer"
+               }
+            },
+            "oneOf": [
+               {
+                  "not": {
+                     "anyOf": [
+                        {
+                           "required": [
+                              "b"
+                           ],
+                           "properties": {
+                              "b": {
+                                 "description": "Randomly picked description from a set of size one.",
+                                 "type": "integer"
+                              }
+                           }
+                        },
+                        {
+                           "required": [
+                              "c"
+                           ],
+                           "properties": {
+                              "c": {
+                                 "description": "Randomly picked description from a set of size one.",
+                                 "type": "integer"
+                              }
+                           }
+                        }
+                     ]
+                  }
+               },
+               {
+                  "required": [
+                     "b"
+                  ],
+                  "properties": {
+                     "b": {
+                        "description": "Randomly picked description from a set of size one.",
+                        "type": "integer"
+                     }
+                  }
+               },
+               {
+                  "required": [
+                     "c"
+                  ],
+                  "properties": {
+                     "c": {
+                        "description": "Randomly picked description from a set of size one.",
+                        "type": "integer"
+                     }
+                  }
+               }
+            ]
+         },
+         "FOO": {
+            "description": "Randomly picked description from a set of size one.",
+            "type": "object",
+            "required": [
+               "count",
+               "include",
+               "exclude"
+            ],
+            "properties": {
+               "count": {
+                  "$ref": "#/components/schemas/MYINT"
+               },
+               "include": {
+                  "$ref": "#/components/schemas/T"
+               },
+               "exclude": {
+                  "description": "Randomly picked description from a set of size one.",
+                  "type": "array",
+                  "items": {
+                     "$ref": "#/components/schemas/T"
+                  }
+               }
+            }
+         },
+         "INCOMPATIBLE": {
+            "description": "Randomly picked description from a set of size one.",
+            "type": "object",
+            "oneOf": [
+               {
+                  "allOf": [
+                     {
+                        "required": [
+                           "shared"
+                        ],
+                        "properties": {
+                           "shared": {
+                              "description": "Randomly picked description from a set of size one.",
+                              "type": "integer"
+                           }
+                        }
+                     },
+                     {
+                        "not": {
+                           "anyOf": [
+                              {
+                                 "required": [
+                                    "shared",
+                                    "extra1"
+                                 ],
+                                 "properties": {
+                                    "shared": {
+                                       "description": "Randomly picked description from a set of size one.",
+                                       "type": "integer"
+                                    },
+                                    "extra1": {
+                                       "description": "Randomly picked description from a set of size one.",
+                                       "type": "integer"
+                                    }
+                                 }
+                              },
+                              {
+                                 "required": [
+                                    "shared",
+                                    "extra2"
+                                 ],
+                                 "properties": {
+                                    "shared": {
+                                       "description": "Randomly picked description from a set of size one.",
+                                       "type": "integer"
+                                    },
+                                    "extra2": {
+                                       "description": "Randomly picked description from a set of size one.",
+                                       "type": "integer"
+                                    }
+                                 }
+                              }
+                           ]
+                        }
+                     }
+                  ]
+               },
+               {
+                  "required": [
+                     "shared",
+                     "extra1"
+                  ],
+                  "properties": {
+                     "shared": {
+                        "description": "Randomly picked description from a set of size one.",
+                        "type": "integer"
+                     },
+                     "extra1": {
+                        "description": "Randomly picked description from a set of size one.",
+                        "type": "integer"
+                     }
+                  }
+               },
+               {
+                  "required": [
+                     "shared",
+                     "extra2"
+                  ],
+                  "properties": {
+                     "shared": {
+                        "description": "Randomly picked description from a set of size one.",
+                        "type": "integer"
+                     },
+                     "extra2": {
+                        "description": "Randomly picked description from a set of size one.",
+                        "type": "integer"
+                     }
+                  }
+               }
+            ]
+         },
+         "MYINT": {
+            "description": "Randomly picked description from a set of size one.",
+            "type": "integer"
+         },
          "T": {
             "description": "Randomly picked description from a set of size one.",
             "type": "object",
@@ -141,125 +322,6 @@
                }
             ]
          },
-         "MYINT": {
-            "description": "Randomly picked description from a set of size one.",
-            "type": "integer"
-         },
-         "FOO": {
-            "description": "Randomly picked description from a set of size one.",
-            "type": "object",
-            "required": [
-               "count",
-               "include",
-               "exclude"
-            ],
-            "properties": {
-               "count": {
-                  "$ref": "#/components/schemas/MYINT"
-               },
-               "include": {
-                  "$ref": "#/components/schemas/T"
-               },
-               "exclude": {
-                  "description": "Randomly picked description from a set of size one.",
-                  "type": "array",
-                  "items": {
-                     "$ref": "#/components/schemas/T"
-                  }
-               }
-            }
-         },
-         "INCOMPATIBLE": {
-            "description": "Randomly picked description from a set of size one.",
-            "type": "object",
-            "oneOf": [
-               {
-                  "allOf": [
-                     {
-                        "required": [
-                           "shared"
-                        ],
-                        "properties": {
-                           "shared": {
-                              "description": "Randomly picked description from a set of size one.",
-                              "type": "integer"
-                           }
-                        }
-                     },
-                     {
-                        "not": {
-                           "anyOf": [
-                              {
-                                 "required": [
-                                    "shared",
-                                    "extra1"
-                                 ],
-                                 "properties": {
-                                    "shared": {
-                                       "description": "Randomly picked description from a set of size one.",
-                                       "type": "integer"
-                                    },
-                                    "extra1": {
-                                       "description": "Randomly picked description from a set of size one.",
-                                       "type": "integer"
-                                    }
-                                 }
-                              },
-                              {
-                                 "required": [
-                                    "shared",
-                                    "extra2"
-                                 ],
-                                 "properties": {
-                                    "shared": {
-                                       "description": "Randomly picked description from a set of size one.",
-                                       "type": "integer"
-                                    },
-                                    "extra2": {
-                                       "description": "Randomly picked description from a set of size one.",
-                                       "type": "integer"
-                                    }
-                                 }
-                              }
-                           ]
-                        }
-                     }
-                  ]
-               },
-               {
-                  "required": [
-                     "shared",
-                     "extra1"
-                  ],
-                  "properties": {
-                     "shared": {
-                        "description": "Randomly picked description from a set of size one.",
-                        "type": "integer"
-                     },
-                     "extra1": {
-                        "description": "Randomly picked description from a set of size one.",
-                        "type": "integer"
-                     }
-                  }
-               },
-               {
-                  "required": [
-                     "shared",
-                     "extra2"
-                  ],
-                  "properties": {
-                     "shared": {
-                        "description": "Randomly picked description from a set of size one.",
-                        "type": "integer"
-                     },
-                     "extra2": {
-                        "description": "Randomly picked description from a set of size one.",
-                        "type": "integer"
-                     }
-                  }
-               }
-            ]
-         },
          "WITHMAP": {
             "description": "Randomly picked description from a set of size one.",
             "type": "object",
@@ -372,68 +434,6 @@
                   }
                }
             ]
-         },
-         "EMBED": {
-            "description": "Randomly picked description from a set of size one.",
-            "type": "object",
-            "properties": {
-               "a": {
-                  "description": "Randomly picked description from a set of size one.",
-                  "type": "integer"
-               }
-            },
-            "oneOf": [
-               {
-                  "not": {
-                     "anyOf": [
-                        {
-                           "required": [
-                              "b"
-                           ],
-                           "properties": {
-                              "b": {
-                                 "description": "Randomly picked description from a set of size one.",
-                                 "type": "integer"
-                              }
-                           }
-                        },
-                        {
-                           "required": [
-                              "c"
-                           ],
-                           "properties": {
-                              "c": {
-                                 "description": "Randomly picked description from a set of size one.",
-                                 "type": "integer"
-                              }
-                           }
-                        }
-                     ]
-                  }
-               },
-               {
-                  "required": [
-                     "b"
-                  ],
-                  "properties": {
-                     "b": {
-                        "description": "Randomly picked description from a set of size one.",
-                        "type": "integer"
-                     }
-                  }
-               },
-               {
-                  "required": [
-                     "c"
-                  ],
-                  "properties": {
-                     "c": {
-                        "description": "Randomly picked description from a set of size one.",
-                        "type": "integer"
-                     }
-                  }
-               }
-            ]
          }
       }
    }
diff --git a/encoding/openapi/testdata/oneof-resolve.json b/encoding/openapi/testdata/oneof-resolve.json
index c37b83a..038f2ac 100644
--- a/encoding/openapi/testdata/oneof-resolve.json
+++ b/encoding/openapi/testdata/oneof-resolve.json
@@ -7,102 +7,48 @@
    "paths": {},
    "components": {
       "schemas": {
-         "T": {
+         "Embed": {
             "type": "object",
             "properties": {
-               "shared": {
+               "a": {
                   "type": "integer"
                },
-               "shared2": {
+               "b": {
                   "type": "integer"
                },
-               "count": {
+               "c": {
                   "type": "integer"
-               },
-               "amount": {
-                  "type": "integer"
-               },
-               "exact": {
-                  "type": "string",
-                  "format": "string"
-               },
-               "regex": {
-                  "type": "string",
-                  "format": "string"
                }
             },
-            "allOf": [
+            "oneOf": [
                {
-                  "required": [
-                     "shared"
-                  ],
-                  "oneOf": [
-                     {
-                        "not": {
-                           "anyOf": [
-                              {
-                                 "required": [
-                                    "exact"
-                                 ]
-                              },
-                              {
-                                 "required": [
-                                    "regex"
-                                 ]
-                              }
+                  "not": {
+                     "anyOf": [
+                        {
+                           "required": [
+                              "b"
+                           ]
+                        },
+                        {
+                           "required": [
+                              "c"
                            ]
                         }
-                     },
-                     {
-                        "required": [
-                           "exact"
-                        ]
-                     },
-                     {
-                        "required": [
-                           "regex"
-                        ]
-                     }
+                     ]
+                  }
+               },
+               {
+                  "required": [
+                     "b"
                   ]
                },
                {
-                  "oneOf": [
-                     {
-                        "not": {
-                           "anyOf": [
-                              {
-                                 "required": [
-                                    "count"
-                                 ]
-                              },
-                              {
-                                 "required": [
-                                    "amount"
-                                 ]
-                              }
-                           ]
-                        }
-                     },
-                     {
-                        "required": [
-                           "count"
-                        ]
-                     },
-                     {
-                        "required": [
-                           "amount"
-                        ]
-                     }
-                  ],
                   "required": [
-                     "shared2"
+                     "c"
                   ]
                }
             ]
          },
-         "MyInt": {
-            "type": "integer"
-         },
          "Foo": {
             "type": "object",
             "required": [
@@ -360,6 +306,102 @@
                }
             ]
          },
+         "MyInt": {
+            "type": "integer"
+         },
+         "T": {
+            "type": "object",
+            "properties": {
+               "shared": {
+                  "type": "integer"
+               },
+               "shared2": {
+                  "type": "integer"
+               },
+               "count": {
+                  "type": "integer"
+               },
+               "amount": {
+                  "type": "integer"
+               },
+               "exact": {
+                  "type": "string",
+                  "format": "string"
+               },
+               "regex": {
+                  "type": "string",
+                  "format": "string"
+               }
+            },
+            "allOf": [
+               {
+                  "required": [
+                     "shared"
+                  ],
+                  "oneOf": [
+                     {
+                        "not": {
+                           "anyOf": [
+                              {
+                                 "required": [
+                                    "exact"
+                                 ]
+                              },
+                              {
+                                 "required": [
+                                    "regex"
+                                 ]
+                              }
+                           ]
+                        }
+                     },
+                     {
+                        "required": [
+                           "exact"
+                        ]
+                     },
+                     {
+                        "required": [
+                           "regex"
+                        ]
+                     }
+                  ]
+               },
+               {
+                  "oneOf": [
+                     {
+                        "not": {
+                           "anyOf": [
+                              {
+                                 "required": [
+                                    "count"
+                                 ]
+                              },
+                              {
+                                 "required": [
+                                    "amount"
+                                 ]
+                              }
+                           ]
+                        }
+                     },
+                     {
+                        "required": [
+                           "count"
+                        ]
+                     },
+                     {
+                        "required": [
+                           "amount"
+                        ]
+                     }
+                  ],
+                  "required": [
+                     "shared2"
+                  ]
+               }
+            ]
+         },
          "WithMap": {
             "type": "object",
             "properties": {
@@ -424,48 +466,6 @@
                   ]
                }
             ]
-         },
-         "Embed": {
-            "type": "object",
-            "properties": {
-               "a": {
-                  "type": "integer"
-               },
-               "b": {
-                  "type": "integer"
-               },
-               "c": {
-                  "type": "integer"
-               }
-            },
-            "oneOf": [
-               {
-                  "not": {
-                     "anyOf": [
-                        {
-                           "required": [
-                              "b"
-                           ]
-                        },
-                        {
-                           "required": [
-                              "c"
-                           ]
-                        }
-                     ]
-                  }
-               },
-               {
-                  "required": [
-                     "b"
-                  ]
-               },
-               {
-                  "required": [
-                     "c"
-                  ]
-               }
-            ]
          }
       }
    }
diff --git a/encoding/openapi/testdata/oneof.json b/encoding/openapi/testdata/oneof.json
index f7a13f0..48ce218 100644
--- a/encoding/openapi/testdata/oneof.json
+++ b/encoding/openapi/testdata/oneof.json
@@ -4,6 +4,168 @@
    "paths": {},
    "components": {
       "schemas": {
+         "Embed": {
+            "type": "object",
+            "properties": {
+               "a": {
+                  "type": "integer"
+               }
+            },
+            "oneOf": [
+               {
+                  "not": {
+                     "anyOf": [
+                        {
+                           "required": [
+                              "b"
+                           ],
+                           "properties": {
+                              "b": {
+                                 "type": "integer"
+                              }
+                           }
+                        },
+                        {
+                           "required": [
+                              "c"
+                           ],
+                           "properties": {
+                              "c": {
+                                 "type": "integer"
+                              }
+                           }
+                        }
+                     ]
+                  }
+               },
+               {
+                  "required": [
+                     "b"
+                  ],
+                  "properties": {
+                     "b": {
+                        "type": "integer"
+                     }
+                  }
+               },
+               {
+                  "required": [
+                     "c"
+                  ],
+                  "properties": {
+                     "c": {
+                        "type": "integer"
+                     }
+                  }
+               }
+            ]
+         },
+         "Foo": {
+            "type": "object",
+            "required": [
+               "count",
+               "include",
+               "exclude"
+            ],
+            "properties": {
+               "count": {
+                  "$ref": "#/components/schemas/MyInt"
+               },
+               "include": {
+                  "$ref": "#/components/schemas/T"
+               },
+               "exclude": {
+                  "type": "array",
+                  "items": {
+                     "$ref": "#/components/schemas/T"
+                  }
+               }
+            }
+         },
+         "Incompatible": {
+            "type": "object",
+            "oneOf": [
+               {
+                  "allOf": [
+                     {
+                        "required": [
+                           "shared"
+                        ],
+                        "properties": {
+                           "shared": {
+                              "type": "integer"
+                           }
+                        }
+                     },
+                     {
+                        "not": {
+                           "anyOf": [
+                              {
+                                 "required": [
+                                    "shared",
+                                    "extra1"
+                                 ],
+                                 "properties": {
+                                    "shared": {
+                                       "type": "integer"
+                                    },
+                                    "extra1": {
+                                       "type": "integer"
+                                    }
+                                 }
+                              },
+                              {
+                                 "required": [
+                                    "shared",
+                                    "extra2"
+                                 ],
+                                 "properties": {
+                                    "shared": {
+                                       "type": "integer"
+                                    },
+                                    "extra2": {
+                                       "type": "integer"
+                                    }
+                                 }
+                              }
+                           ]
+                        }
+                     }
+                  ]
+               },
+               {
+                  "required": [
+                     "shared",
+                     "extra1"
+                  ],
+                  "properties": {
+                     "shared": {
+                        "type": "integer"
+                     },
+                     "extra1": {
+                        "type": "integer"
+                     }
+                  }
+               },
+               {
+                  "required": [
+                     "shared",
+                     "extra2"
+                  ],
+                  "properties": {
+                     "shared": {
+                        "type": "integer"
+                     },
+                     "extra2": {
+                        "type": "integer"
+                     }
+                  }
+               }
+            ]
+         },
+         "MyInt": {
+            "type": "integer"
+         },
          "T": {
             "type": "object",
             "properties": {
@@ -127,112 +289,6 @@
                }
             ]
          },
-         "MyInt": {
-            "type": "integer"
-         },
-         "Foo": {
-            "type": "object",
-            "required": [
-               "count",
-               "include",
-               "exclude"
-            ],
-            "properties": {
-               "count": {
-                  "$ref": "#/components/schemas/MyInt"
-               },
-               "include": {
-                  "$ref": "#/components/schemas/T"
-               },
-               "exclude": {
-                  "type": "array",
-                  "items": {
-                     "$ref": "#/components/schemas/T"
-                  }
-               }
-            }
-         },
-         "Incompatible": {
-            "type": "object",
-            "oneOf": [
-               {
-                  "allOf": [
-                     {
-                        "required": [
-                           "shared"
-                        ],
-                        "properties": {
-                           "shared": {
-                              "type": "integer"
-                           }
-                        }
-                     },
-                     {
-                        "not": {
-                           "anyOf": [
-                              {
-                                 "required": [
-                                    "shared",
-                                    "extra1"
-                                 ],
-                                 "properties": {
-                                    "shared": {
-                                       "type": "integer"
-                                    },
-                                    "extra1": {
-                                       "type": "integer"
-                                    }
-                                 }
-                              },
-                              {
-                                 "required": [
-                                    "shared",
-                                    "extra2"
-                                 ],
-                                 "properties": {
-                                    "shared": {
-                                       "type": "integer"
-                                    },
-                                    "extra2": {
-                                       "type": "integer"
-                                    }
-                                 }
-                              }
-                           ]
-                        }
-                     }
-                  ]
-               },
-               {
-                  "required": [
-                     "shared",
-                     "extra1"
-                  ],
-                  "properties": {
-                     "shared": {
-                        "type": "integer"
-                     },
-                     "extra1": {
-                        "type": "integer"
-                     }
-                  }
-               },
-               {
-                  "required": [
-                     "shared",
-                     "extra2"
-                  ],
-                  "properties": {
-                     "shared": {
-                        "type": "integer"
-                     },
-                     "extra2": {
-                        "type": "integer"
-                     }
-                  }
-               }
-            ]
-         },
          "WithMap": {
             "type": "object",
             "oneOf": [
@@ -332,62 +388,6 @@
                   }
                }
             ]
-         },
-         "Embed": {
-            "type": "object",
-            "properties": {
-               "a": {
-                  "type": "integer"
-               }
-            },
-            "oneOf": [
-               {
-                  "not": {
-                     "anyOf": [
-                        {
-                           "required": [
-                              "b"
-                           ],
-                           "properties": {
-                              "b": {
-                                 "type": "integer"
-                              }
-                           }
-                        },
-                        {
-                           "required": [
-                              "c"
-                           ],
-                           "properties": {
-                              "c": {
-                                 "type": "integer"
-                              }
-                           }
-                        }
-                     ]
-                  }
-               },
-               {
-                  "required": [
-                     "b"
-                  ],
-                  "properties": {
-                     "b": {
-                        "type": "integer"
-                     }
-                  }
-               },
-               {
-                  "required": [
-                     "c"
-                  ],
-                  "properties": {
-                     "c": {
-                        "type": "integer"
-                     }
-                  }
-               }
-            ]
          }
       }
    }
diff --git a/encoding/openapi/testdata/openapi-norefs.json b/encoding/openapi/testdata/openapi-norefs.json
index dc246b1..d39a589 100644
--- a/encoding/openapi/testdata/openapi-norefs.json
+++ b/encoding/openapi/testdata/openapi-norefs.json
@@ -7,6 +7,81 @@
    "paths": {},
    "components": {
       "schemas": {
+         "DefaultStruct": {
+            "type": "object",
+            "properties": {
+               "port": {},
+               "obj": {
+                  "type": "array",
+                  "items": {
+                     "type": "integer"
+                  }
+               }
+            },
+            "default": {
+               "port": 1
+            },
+            "oneOf": [
+               {
+                  "required": [
+                     "port",
+                     "obj"
+                  ]
+               },
+               {
+                  "required": [
+                     "port"
+                  ]
+               }
+            ]
+         },
+         "Enum": {
+            "type": "string",
+            "enum": [
+               "foo",
+               "bar",
+               "baz"
+            ]
+         },
+         "Int32": {
+            "type": "integer",
+            "format": "int32"
+         },
+         "List": {
+            "type": "array",
+            "items": {
+               "type": "number"
+            },
+            "default": [
+               1,
+               2,
+               3
+            ]
+         },
+         "Msg2": {
+            "type": "object",
+            "properties": {
+               "b": {
+                  "type": "number"
+               },
+               "a": {
+                  "type": "string",
+                  "format": "string"
+               }
+            },
+            "oneOf": [
+               {
+                  "required": [
+                     "b"
+                  ]
+               },
+               {
+                  "required": [
+                     "a"
+                  ]
+               }
+            ]
+         },
          "MyMessage": {
             "description": "MyMessage is my message.",
             "type": "object",
@@ -88,10 +163,6 @@
                }
             }
          },
-         "Int32": {
-            "type": "integer",
-            "format": "int32"
-         },
          "YourMessage": {
             "type": "object",
             "properties": {
@@ -182,77 +253,6 @@
                   ]
                }
             ]
-         },
-         "Msg2": {
-            "type": "object",
-            "properties": {
-               "b": {
-                  "type": "number"
-               },
-               "a": {
-                  "type": "string",
-                  "format": "string"
-               }
-            },
-            "oneOf": [
-               {
-                  "required": [
-                     "b"
-                  ]
-               },
-               {
-                  "required": [
-                     "a"
-                  ]
-               }
-            ]
-         },
-         "Enum": {
-            "type": "string",
-            "enum": [
-               "foo",
-               "bar",
-               "baz"
-            ]
-         },
-         "List": {
-            "type": "array",
-            "items": {
-               "type": "number"
-            },
-            "default": [
-               1,
-               2,
-               3
-            ]
-         },
-         "DefaultStruct": {
-            "type": "object",
-            "properties": {
-               "port": {},
-               "obj": {
-                  "type": "array",
-                  "items": {
-                     "type": "integer"
-                  }
-               }
-            },
-            "default": {
-               "port": 1
-            },
-            "oneOf": [
-               {
-                  "required": [
-                     "port",
-                     "obj"
-                  ]
-               },
-               {
-                  "required": [
-                     "port"
-                  ]
-               }
-            ]
          }
       }
    }
diff --git a/encoding/openapi/testdata/openapi.json b/encoding/openapi/testdata/openapi.json
index 923fd0d..ef7d41c 100644
--- a/encoding/openapi/testdata/openapi.json
+++ b/encoding/openapi/testdata/openapi.json
@@ -4,6 +4,79 @@
    "paths": {},
    "components": {
       "schemas": {
+         "DefaultStruct": {
+            "type": "object",
+            "default": {
+               "port": 1
+            },
+            "oneOf": [
+               {
+                  "$ref": "#/components/schemas/Port"
+               },
+               {
+                  "required": [
+                     "port"
+                  ],
+                  "properties": {
+                     "port": {
+                        "type": "integer",
+                        "enum": [
+                           1
+                        ]
+                     }
+                  }
+               }
+            ]
+         },
+         "Enum": {
+            "type": "string",
+            "enum": [
+               "foo",
+               "bar",
+               "baz"
+            ]
+         },
+         "Int32": {
+            "type": "integer",
+            "format": "int32"
+         },
+         "List": {
+            "type": "array",
+            "items": {
+               "type": "number"
+            },
+            "default": [
+               1,
+               2,
+               3
+            ]
+         },
+         "Msg2": {
+            "type": "object",
+            "oneOf": [
+               {
+                  "required": [
+                     "b"
+                  ],
+                  "properties": {
+                     "b": {
+                        "type": "number"
+                     }
+                  }
+               },
+               {
+                  "required": [
+                     "a"
+                  ],
+                  "properties": {
+                     "a": {
+                        "type": "string",
+                        "format": "string"
+                     }
+                  }
+               }
+            ]
+         },
          "MyMessage": {
             "description": "MyMessage is my message.",
             "type": "object",
@@ -82,10 +155,6 @@
                }
             }
          },
-         "Int32": {
-            "type": "integer",
-            "format": "int32"
-         },
          "YourMessage": {
             "type": "object",
             "properties": {
@@ -194,75 +263,6 @@
                   ]
                }
             ]
-         },
-         "Msg2": {
-            "type": "object",
-            "oneOf": [
-               {
-                  "required": [
-                     "b"
-                  ],
-                  "properties": {
-                     "b": {
-                        "type": "number"
-                     }
-                  }
-               },
-               {
-                  "required": [
-                     "a"
-                  ],
-                  "properties": {
-                     "a": {
-                        "type": "string",
-                        "format": "string"
-                     }
-                  }
-               }
-            ]
-         },
-         "Enum": {
-            "type": "string",
-            "enum": [
-               "foo",
-               "bar",
-               "baz"
-            ]
-         },
-         "List": {
-            "type": "array",
-            "items": {
-               "type": "number"
-            },
-            "default": [
-               1,
-               2,
-               3
-            ]
-         },
-         "DefaultStruct": {
-            "type": "object",
-            "default": {
-               "port": 1
-            },
-            "oneOf": [
-               {
-                  "$ref": "#/components/schemas/Port"
-               },
-               {
-                  "required": [
-                     "port"
-                  ],
-                  "properties": {
-                     "port": {
-                        "type": "integer",
-                        "enum": [
-                           1
-                        ]
-                     }
-                  }
-               }
-            ]
          }
       }
    }
diff --git a/encoding/openapi/testdata/structural.json b/encoding/openapi/testdata/structural.json
index dab5eb7..3e1945c 100644
--- a/encoding/openapi/testdata/structural.json
+++ b/encoding/openapi/testdata/structural.json
@@ -7,6 +7,104 @@
    "paths": {},
    "components": {
       "schemas": {
+         "AttrValue": {
+            "description": "The attribute value.",
+            "type": "object",
+            "properties": {
+               "stringValue": {
+                  "description": "Used for values of type STRING, DNS_NAME, EMAIL_ADDRESS, and URI",
+                  "type": "string",
+                  "format": "string"
+               },
+               "int64Value": {
+                  "description": "Used for values of type INT64",
+                  "type": "integer",
+                  "format": "int64"
+               },
+               "doubleValue": {
+                  "description": "Used for values of type DOUBLE",
+                  "type": "number",
+                  "format": "double"
+               },
+               "boolValue": {
+                  "description": "Used for values of type BOOL",
+                  "type": "boolean"
+               },
+               "bytesValue": {
+                  "description": "Used for values of type BYTES",
+                  "type": "string",
+                  "format": "binary"
+               },
+               "timestampValue": {
+                  "description": "Used for values of type TIMESTAMP",
+                  "type": "string",
+                  "format": "dateTime"
+               },
+               "durationValue": {
+                  "description": "Used for values of type DURATION",
+                  "type": "string"
+               },
+               "stringMapValue": {
+                  "description": "Used for values of type STRING_MAP",
+                  "type": "object",
+                  "required": [
+                     "entries"
+                  ],
+                  "properties": {
+                     "entries": {
+                        "description": "Holds a set of name/value pairs.",
+                        "type": "object",
+                        "additionalProperties": {
+                           "type": "string",
+                           "format": "string"
+                        }
+                     }
+                  }
+               }
+            },
+            "oneOf": [
+               {
+                  "required": [
+                     "stringValue"
+                  ]
+               },
+               {
+                  "required": [
+                     "int64Value"
+                  ]
+               },
+               {
+                  "required": [
+                     "doubleValue"
+                  ]
+               },
+               {
+                  "required": [
+                     "boolValue"
+                  ]
+               },
+               {
+                  "required": [
+                     "bytesValue"
+                  ]
+               },
+               {
+                  "required": [
+                     "timestampValue"
+                  ]
+               },
+               {
+                  "required": [
+                     "durationValue"
+                  ]
+               },
+               {
+                  "required": [
+                     "stringMapValue"
+                  ]
+               }
+            ]
+         },
          "Attributes": {
             "type": "object",
             "required": [
@@ -116,104 +214,6 @@
                }
             }
          },
-         "AttrValue": {
-            "description": "The attribute value.",
-            "type": "object",
-            "properties": {
-               "stringValue": {
-                  "description": "Used for values of type STRING, DNS_NAME, EMAIL_ADDRESS, and URI",
-                  "type": "string",
-                  "format": "string"
-               },
-               "int64Value": {
-                  "description": "Used for values of type INT64",
-                  "type": "integer",
-                  "format": "int64"
-               },
-               "doubleValue": {
-                  "description": "Used for values of type DOUBLE",
-                  "type": "number",
-                  "format": "double"
-               },
-               "boolValue": {
-                  "description": "Used for values of type BOOL",
-                  "type": "boolean"
-               },
-               "bytesValue": {
-                  "description": "Used for values of type BYTES",
-                  "type": "string",
-                  "format": "binary"
-               },
-               "timestampValue": {
-                  "description": "Used for values of type TIMESTAMP",
-                  "type": "string",
-                  "format": "dateTime"
-               },
-               "durationValue": {
-                  "description": "Used for values of type DURATION",
-                  "type": "string"
-               },
-               "stringMapValue": {
-                  "description": "Used for values of type STRING_MAP",
-                  "type": "object",
-                  "required": [
-                     "entries"
-                  ],
-                  "properties": {
-                     "entries": {
-                        "description": "Holds a set of name/value pairs.",
-                        "type": "object",
-                        "additionalProperties": {
-                           "type": "string",
-                           "format": "string"
-                        }
-                     }
-                  }
-               }
-            },
-            "oneOf": [
-               {
-                  "required": [
-                     "stringValue"
-                  ]
-               },
-               {
-                  "required": [
-                     "int64Value"
-                  ]
-               },
-               {
-                  "required": [
-                     "doubleValue"
-                  ]
-               },
-               {
-                  "required": [
-                     "boolValue"
-                  ]
-               },
-               {
-                  "required": [
-                     "bytesValue"
-                  ]
-               },
-               {
-                  "required": [
-                     "timestampValue"
-                  ]
-               },
-               {
-                  "required": [
-                     "durationValue"
-                  ]
-               },
-               {
-                  "required": [
-                     "stringMapValue"
-                  ]
-               }
-            ]
-         },
          "Attributes_StringMap": {
             "type": "object",
             "required": [