Skip to content

Commit

Permalink
Automate openapi server generation (#308)
Browse files Browse the repository at this point in the history
* Fix type_assert.patch

* Improve openapi-server generation

* Automate openapi server generation if something changed
  • Loading branch information
lampajr authored Feb 15, 2024
1 parent dbcacad commit 90f7ddc
Show file tree
Hide file tree
Showing 8 changed files with 511 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

exclude: '^patches/'
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
Expand Down
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ COPY cmd/ cmd/
COPY api/ api/
COPY internal/ internal/
COPY pkg/ pkg/
COPY scripts/ scripts/
COPY patches/ patches/
COPY templates/ templates/

# Build
USER root
Expand Down
18 changes: 9 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,18 @@ gen/converter: gen/grpc internal/converter/generated/converter.go
# validate the openapi schema
.PHONY: openapi/validate
openapi/validate: bin/openapi-generator-cli
openapi-generator-cli validate -i api/openapi/model-registry.yaml
@openapi-generator-cli validate -i api/openapi/model-registry.yaml

# generate the openapi server implementation
# note: run manually only when model-registry.yaml api changes, for model changes gen/openapi is run automatically
.PHONY: gen/openapi-server
gen/openapi-server: bin/openapi-generator-cli openapi/validate
openapi-generator-cli generate \
-i api/openapi/model-registry.yaml -g go-server -o internal/server/openapi --package-name openapi --global-property models \
--ignore-file-override ./.openapi-generator-ignore --additional-properties=outputAsLibrary=true,enumClassPrefix=true,router=chi,sourceFolder=,onlyInterfaces=true,isGoSubmodule=true,enumClassPrefix=true,useOneOfDiscriminatorLookup=true \
--template-dir ./templates/go-server
./scripts/gen_type_asserts.sh
gofmt -w internal/server/openapi
@if git diff --cached --name-only | grep -q "api/openapi/model-registry.yaml" || \
git diff --name-only | grep -q "api/openapi/model-registry.yaml" || \
[ -n "${FORCE_SERVER_GENERATION}" ]; then \
ROOT_FOLDER="." ./scripts/gen_openapi_server.sh; \
else \
echo "INFO api/openapi/model-registry.yaml is not staged or modified, will not re-generate server"; \
fi

# generate the openapi schema model and client
.PHONY: gen/openapi
Expand Down Expand Up @@ -156,7 +156,7 @@ build/odh: vet
go build

.PHONY: gen
gen: deps gen/grpc gen/openapi gen/converter
gen: deps gen/grpc gen/openapi gen/openapi-server gen/converter
go generate ./...

.PHONY: lint
Expand Down
5 changes: 3 additions & 2 deletions internal/server/openapi/api_model_registry_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* REST API for Model Registry to create and manage ML model metadata
*
* API version: 1.0.0
* API version: v1alpha1
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/

Expand All @@ -15,6 +15,7 @@ import (
"strings"

"github.com/go-chi/chi/v5"

model "github.com/opendatahub-io/model-registry/pkg/openapi"
)

Expand Down Expand Up @@ -692,7 +693,7 @@ func (c *ModelRegistryServiceAPIController) GetModelVersion(w http.ResponseWrite
EncodeJSONResponse(result.Body, &result.Code, w)
}

// GetModelVersionArtifacts - List All ModelVersion's artifacts
// GetModelVersionArtifacts - List all artifacts associated with the `ModelVersion`
func (c *ModelRegistryServiceAPIController) GetModelVersionArtifacts(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
modelversionIdParam := chi.URLParam(r, "modelversionId")
Expand Down
19 changes: 19 additions & 0 deletions internal/server/openapi/type_asserts.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,25 @@ func AssertBaseResourceUpdateConstraints(obj model.BaseResourceUpdate) error {
return nil
}

// AssertDocArtifactRequired checks if the required fields are not zero-ed
func AssertDocArtifactRequired(obj model.DocArtifact) error {
elements := map[string]interface{}{
"artifactType": obj.ArtifactType,
}
for name, el := range elements {
if isZero := IsZeroValue(el); isZero {
return &RequiredError{Field: name}
}
}

return nil
}

// AssertDocArtifactConstraints checks if the values respects the defined constraints
func AssertDocArtifactConstraints(obj model.DocArtifact) error {
return nil
}

// AssertErrorRequired checks if the required fields are not zero-ed
func AssertErrorRequired(obj model.Error) error {
elements := map[string]interface{}{
Expand Down
5 changes: 3 additions & 2 deletions patches/type_asserts.patch
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
diff --git a/internal/server/openapi/type_asserts.go b/internal/server/openapi/type_asserts.go
index 4318f15..2aba64a 100644
index 6e8ecb1..6d08bbb 100644
--- a/internal/server/openapi/type_asserts.go
+++ b/internal/server/openapi/type_asserts.go
@@ -18,15 +18,15 @@ import (

// AssertArtifactRequired checks if the required fields are not zero-ed
func AssertArtifactRequired(obj model.Artifact) error {
- elements := map[string]interface{}{
Expand All @@ -26,3 +26,4 @@ index 4318f15..2aba64a 100644
+ // }
return nil
}

22 changes: 22 additions & 0 deletions scripts/gen_openapi_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

set -e

echo "Generating the OpenAPI server"

ROOT_FOLDER="${ROOT_FOLDER:-..}"

openapi-generator-cli generate \
-i $ROOT_FOLDER/api/openapi/model-registry.yaml -g go-server -o $ROOT_FOLDER/internal/server/openapi --package-name openapi --global-property models,apis \
--ignore-file-override $ROOT_FOLDER/.openapi-generator-ignore --additional-properties=outputAsLibrary=true,enumClassPrefix=true,router=chi,sourceFolder=,onlyInterfaces=true,isGoSubmodule=true,enumClassPrefix=true,useOneOfDiscriminatorLookup=true \
--template-dir $ROOT_FOLDER/templates/go-server

sed -i 's/, orderByParam/, model.OrderByField(orderByParam)/g' $ROOT_FOLDER/internal/server/openapi/api_model_registry_service.go
sed -i 's/, sortOrderParam/, model.SortOrder(sortOrderParam)/g' $ROOT_FOLDER/internal/server/openapi/api_model_registry_service.go

echo "Assembling type_assert Go file"
./scripts/gen_type_asserts.sh

gofmt -w $ROOT_FOLDER/internal/server/openapi

echo "OpenAPI server generation completed"
Loading

0 comments on commit 90f7ddc

Please sign in to comment.