|
4 | 4 | package bundler |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "errors" |
| 8 | + "github.com/pb33f/libopenapi/orderedmap" |
7 | 9 | "testing" |
8 | 10 |
|
9 | 11 | "github.com/pb33f/libopenapi" |
@@ -95,3 +97,80 @@ func TestProcessRef_UnknownLocation_ThreeStep(t *testing.T) { |
95 | 97 | assert.NoError(t, err) |
96 | 98 | assert.Len(t, config.inlineRequired, 1) |
97 | 99 | } |
| 100 | + |
| 101 | +// ---- RenameRef Tests ---- |
| 102 | + |
| 103 | +// A component key that contains a dot (“asdf.zxcv”) must *not* be shortened to |
| 104 | +// “asdf” when we re-wire references. |
| 105 | +func TestRenameRef_KeepsDotInComponentName(t *testing.T) { |
| 106 | + spec := []byte(` |
| 107 | +openapi: 3.1.0 |
| 108 | +info: |
| 109 | + title: Test |
| 110 | + version: 0.0.0 |
| 111 | +components: |
| 112 | + schemas: |
| 113 | + "asdf.zxcv": |
| 114 | + type: object |
| 115 | + Foo: |
| 116 | + allOf: |
| 117 | + - $ref: '#/components/schemas/asdf.zxcv' |
| 118 | +`) |
| 119 | + |
| 120 | + doc, err := libopenapi.NewDocument(spec) |
| 121 | + assert.NoError(t, err) |
| 122 | + |
| 123 | + v3doc, errs := doc.BuildV3Model() |
| 124 | + assert.NoError(t, errors.Join(errs...)) |
| 125 | + |
| 126 | + idx := v3doc.Model.Index |
| 127 | + |
| 128 | + processed := orderedmap.New[string, *processRef]() |
| 129 | + |
| 130 | + got := renameRef(idx, "#/components/schemas/asdf.zxcv", processed) |
| 131 | + |
| 132 | + assert.Equal(t, |
| 133 | + "#/components/schemas/asdf.zxcv", |
| 134 | + got, |
| 135 | + "renameRef must not strip the .zxcv part from the component key") |
| 136 | +} |
| 137 | + |
| 138 | +// A reference that really *is* a filename + JSON-pointer must still have the |
| 139 | +// extension stripped |
| 140 | +func TestRenameRef_FilePointer_Extensions(t *testing.T) { |
| 141 | + exts := []string{".yaml", ".yml", ".json"} |
| 142 | + |
| 143 | + for _, ext := range exts { |
| 144 | + def := "schemas/pet" + ext + "#/components/schemas/Pet" |
| 145 | + got := renameRef(nil, def, orderedmap.New[string, *processRef]()) |
| 146 | + assert.Equal(t, "#/components/schemas/Pet", got, |
| 147 | + "extension %s should not affect the pointer rewrite", ext) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +// If a component name has already been changed during composition, |
| 152 | +// renameRef must return that new name. |
| 153 | +func TestRenameRef_RespectsAlreadyRenamedComponent(t *testing.T) { |
| 154 | + ps := orderedmap.New[string, *processRef]() |
| 155 | + ps.Set("#/components/schemas/asdf.zxcv", |
| 156 | + &processRef{name: "asdf__1", location: []string{}}) |
| 157 | + |
| 158 | + got := renameRef(nil, |
| 159 | + "#/components/schemas/asdf.zxcv", |
| 160 | + ps) |
| 161 | + |
| 162 | + assert.Equal(t, |
| 163 | + "#/components/schemas/asdf__1", |
| 164 | + got, |
| 165 | + "renameRef should use the name stored in processedNodes") |
| 166 | +} |
| 167 | + |
| 168 | +func TestRenameRef_RootFileImport(t *testing.T) { |
| 169 | + processed := orderedmap.New[string, *processRef]() |
| 170 | + processed.Set("schemas/pet.yaml", |
| 171 | + &processRef{location: []string{"components", "schemas", "Pet"}}) |
| 172 | + |
| 173 | + got := renameRef(nil, "schemas/pet.yaml", processed) |
| 174 | + |
| 175 | + assert.Equal(t, "#/components/schemas/Pet", got) |
| 176 | +} |
0 commit comments