Skip to content

Commit e4904c1

Browse files
updating documentation and tests for registering versions
Signed-off-by: Alex Lovell-Troy <alex@lovelltroy.org>
1 parent 31425bb commit e4904c1

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

cmd/fabrica/generate.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ func newGenerateCommand() *cobra.Command {
6464
Long: `Generate server handlers, storage adapters, client code, and OpenAPI specs
6565
from your resource definitions.
6666
67+
For versioned APIs (apis.yaml), this also generates
68+
pkg/apiversion/registry_generated.go, which is required for apiVersion
69+
validation in the server middleware.
70+
6771
Examples:
6872
fabrica generate # Generate all
6973
fabrica generate --handlers # Just handlers

docs/guides/versioning.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,12 @@ See [Migration Guide](https://docs.example.io/migration) for step-by-step instru
641641

642642
### Error: "apiVersion not supported"
643643

644-
**Cause**: Client requested a version not in the `apis.yaml` versions list.
644+
**Cause**: Client requested a version not in the `apis.yaml` versions list, or
645+
the version registry was not generated/imported in the server.
645646

646647
**Solution**: Add the version to `apis.yaml` or update the client to use a supported version.
648+
If the registry is missing, run `fabrica generate` and ensure
649+
`_ "<module>/pkg/apiversion"` is imported in `cmd/server/main.go`.
647650

648651
```yaml
649652
# apis.yaml

docs/reference/cli.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ Generated Files:
360360
- Files ending in `_generated.go` are completely overwritten
361361
- Your resource definitions (`*_types.go`) are never modified
362362
- Run from project root directory
363+
- Versioned APIs require `pkg/apiversion/registry_generated.go` for apiVersion validation
363364

364365
---
365366

test/integration/versioning_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package integration
66

77
import (
8+
"net/http"
89
"os"
910
"path/filepath"
1011
"testing"
@@ -189,6 +190,31 @@ func (s *VersioningSuite) TestJSONCompatibility() {
189190
s.Require().NoError(err, "generated models should contain JSON marshaling tags")
190191
}
191192

193+
// TestUnsupportedAPIVersionRejected verifies invalid apiVersion returns 406 when registry is present
194+
func (s *VersioningSuite) TestUnsupportedAPIVersionRejected() {
195+
project := s.createProject("apiversion-unsupported", "github.com/test/unsupported", "file")
196+
197+
err := project.Initialize(s.fabricaBinary)
198+
s.Require().NoError(err, "project initialization should succeed")
199+
200+
err = project.AddResource(s.fabricaBinary, "Device")
201+
s.Require().NoError(err, "adding resource should succeed")
202+
203+
err = project.Generate(s.fabricaBinary)
204+
s.Require().NoError(err, "code generation should succeed")
205+
206+
project.AssertFileExists("pkg/apiversion/registry_generated.go")
207+
208+
err = project.StartServerRuntime()
209+
s.Require().NoError(err, "server should start")
210+
211+
body := `{"apiVersion":"example.com/v2asdasda","kind":"Device","metadata":{"name":"device-100"},"spec":{}}`
212+
resp, respBody, err := project.HTTPCall(http.MethodPost, "/devices", body, nil)
213+
s.Require().NoError(err, "request should succeed")
214+
s.Require().Equal(http.StatusNotAcceptable, resp.StatusCode)
215+
s.Require().Contains(string(respBody), "Unsupported version")
216+
}
217+
192218
// TestRun is the entry point for the versioning test suite
193219
func TestVersioningSuite(t *testing.T) {
194220
suite.Run(t, new(VersioningSuite))

0 commit comments

Comments
 (0)