Skip to content

Commit

Permalink
fix: Update openapi-server codegen to re-create manual changes (#822)
Browse files Browse the repository at this point in the history
The code in the repo has deviated from what openapi-generator-cli
produces. This overrides the templates so that the generated code is
equivalent.

Signed-off-by: Paul Boyd <[email protected]>
  • Loading branch information
pboyd authored Feb 28, 2025
1 parent 3fe4460 commit 1ebadef
Show file tree
Hide file tree
Showing 19 changed files with 650 additions and 48 deletions.
2 changes: 0 additions & 2 deletions .openapi-generator-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,3 @@ internal/server/openapi/.openapi-generator-ignore
internal/server/openapi/api_model_registry_service_service.go
internal/server/openapi/README.md
internal/server/openapi/main.go
internal/server/openapi/error.go
internal/server/openapi/helpers.go
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ RUN yum module -y enable nodejs:18
# install npm and java for openapi-generator-cli
RUN yum install -y nodejs npm java-11 python3

# Copy the go source
COPY ["Makefile", "main.go", ".openapi-generator-ignore", "openapitools.json", "./"]
# Download tools
COPY Makefile .
COPY scripts/install_protoc.sh scripts/
RUN make deps

# Copy rest of the source
COPY ["main.go", ".openapi-generator-ignore", "openapitools.json", "./"]
COPY cmd/ cmd/
COPY api/ api/
COPY internal/ internal/
Expand All @@ -30,9 +33,6 @@ COPY pkg/ pkg/
COPY patches/ patches/
COPY templates/ templates/

# Download tools
RUN make deps

# Build
USER root

Expand Down
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,16 @@ clean/csi:
rm -Rf ./mr-storage-initializer

.PHONY: clean-pkg-openapi
while IFS= read -r file; do rm -f "pkg/openapi/$file"; done < pkg/openapi/.openapi-generator/FILES
clean-pkg-openapi:
while IFS= read -r file; do rm -f "pkg/openapi/$$file"; done < pkg/openapi/.openapi-generator/FILES

.PHONY: clean-internal-server-openapi
clean-internal-server-openapi:
while IFS= read -r file; do rm -f "internal/server/openapi/$$file"; done < internal/server/openapi/.openapi-generator/FILES

.PHONY: clean
clean: clean-pkg-openapi clean/csi
rm -Rf ./model-registry internal/ml_metadata/proto/*.go internal/converter/generated/*.go internal/server/openapi/api_model_registry_service.go
clean: clean-pkg-openapi clean-internal-server-openapi clean/csi
rm -Rf ./model-registry internal/ml_metadata/proto/*.go internal/converter/generated/*.go

.PHONY: clean/odh
clean/odh:
Expand Down
56 changes: 56 additions & 0 deletions internal/server/openapi/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,60 @@ error.go
helpers.go
impl.go
logger.go
model_artifact.go
model_artifact_create.go
model_artifact_list.go
model_artifact_state.go
model_artifact_update.go
model_base_artifact.go
model_base_artifact_create.go
model_base_artifact_update.go
model_base_execution.go
model_base_execution_create.go
model_base_execution_update.go
model_base_resource.go
model_base_resource_create.go
model_base_resource_list.go
model_base_resource_update.go
model_doc_artifact.go
model_doc_artifact_create.go
model_doc_artifact_update.go
model_error.go
model_execution_state.go
model_inference_service.go
model_inference_service_create.go
model_inference_service_list.go
model_inference_service_state.go
model_inference_service_update.go
model_metadata_bool_value.go
model_metadata_double_value.go
model_metadata_int_value.go
model_metadata_proto_value.go
model_metadata_string_value.go
model_metadata_struct_value.go
model_metadata_value.go
model_model_artifact.go
model_model_artifact_create.go
model_model_artifact_list.go
model_model_artifact_update.go
model_model_version.go
model_model_version_create.go
model_model_version_list.go
model_model_version_state.go
model_model_version_update.go
model_order_by_field.go
model_registered_model.go
model_registered_model_create.go
model_registered_model_list.go
model_registered_model_state.go
model_registered_model_update.go
model_serve_model.go
model_serve_model_create.go
model_serve_model_list.go
model_serve_model_update.go
model_serving_environment.go
model_serving_environment_create.go
model_serving_environment_list.go
model_serving_environment_update.go
model_sort_order.go
routers.go
2 changes: 1 addition & 1 deletion internal/server/openapi/.openapi-generator/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.0.1
7.0.1
22 changes: 14 additions & 8 deletions internal/server/openapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
*
* REST API for Model Registry to create and manage ML model metadata
*
* API version: 1.0.0
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
* API version: v1alpha3
*/

// Generated by: OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.

package openapi

import (
Expand All @@ -20,20 +21,23 @@ import (
// The ModelRegistryServiceAPIRouter implementation should parse necessary information from the http request,
// pass the data to a ModelRegistryServiceAPIServicer to perform the required actions, then write the service results to the http response.
type ModelRegistryServiceAPIRouter interface {
CreateArtifact(http.ResponseWriter, *http.Request)
CreateEnvironmentInferenceService(http.ResponseWriter, *http.Request)
CreateInferenceService(http.ResponseWriter, *http.Request)
CreateInferenceServiceServe(http.ResponseWriter, *http.Request)
CreateModelArtifact(http.ResponseWriter, *http.Request)
CreateModelVersion(http.ResponseWriter, *http.Request)
CreateModelVersionArtifact(http.ResponseWriter, *http.Request)
CreateRegisteredModel(http.ResponseWriter, *http.Request)
CreateRegisteredModelVersion(http.ResponseWriter, *http.Request)
CreateServingEnvironment(http.ResponseWriter, *http.Request)
FindArtifact(http.ResponseWriter, *http.Request)
FindInferenceService(http.ResponseWriter, *http.Request)
FindModelArtifact(http.ResponseWriter, *http.Request)
FindModelVersion(http.ResponseWriter, *http.Request)
FindRegisteredModel(http.ResponseWriter, *http.Request)
FindServingEnvironment(http.ResponseWriter, *http.Request)
GetArtifact(http.ResponseWriter, *http.Request)
GetArtifacts(http.ResponseWriter, *http.Request)
GetEnvironmentInferenceServices(http.ResponseWriter, *http.Request)
GetInferenceService(http.ResponseWriter, *http.Request)
GetInferenceServiceModel(http.ResponseWriter, *http.Request)
Expand All @@ -50,41 +54,43 @@ type ModelRegistryServiceAPIRouter interface {
GetRegisteredModels(http.ResponseWriter, *http.Request)
GetServingEnvironment(http.ResponseWriter, *http.Request)
GetServingEnvironments(http.ResponseWriter, *http.Request)
UpdateArtifact(http.ResponseWriter, *http.Request)
UpdateInferenceService(http.ResponseWriter, *http.Request)
UpdateModelArtifact(http.ResponseWriter, *http.Request)
UpdateModelVersion(http.ResponseWriter, *http.Request)
UpdateRegisteredModel(http.ResponseWriter, *http.Request)
UpdateServingEnvironment(http.ResponseWriter, *http.Request)
UpsertModelVersionArtifact(http.ResponseWriter, *http.Request)
}

// ModelRegistryServiceAPIServicer defines the api actions for the ModelRegistryServiceAPI service
// This interface intended to stay up to date with the openapi yaml used to generate it,
// while the service implementation can be ignored with the .openapi-generator-ignore file
// and updated with the logic required for the API.
type ModelRegistryServiceAPIServicer interface {
CreateArtifact(context.Context, model.ArtifactCreate) (ImplResponse, error)
CreateEnvironmentInferenceService(context.Context, string, model.InferenceServiceCreate) (ImplResponse, error)
CreateInferenceService(context.Context, model.InferenceServiceCreate) (ImplResponse, error)
CreateInferenceServiceServe(context.Context, string, model.ServeModelCreate) (ImplResponse, error)
CreateArtifact(context.Context, model.ArtifactCreate) (ImplResponse, error)
CreateModelArtifact(context.Context, model.ModelArtifactCreate) (ImplResponse, error)
CreateModelVersion(context.Context, model.ModelVersionCreate) (ImplResponse, error)
CreateRegisteredModel(context.Context, model.RegisteredModelCreate) (ImplResponse, error)
CreateRegisteredModelVersion(context.Context, string, model.ModelVersion) (ImplResponse, error)
CreateServingEnvironment(context.Context, model.ServingEnvironmentCreate) (ImplResponse, error)
FindInferenceService(context.Context, string, string, string) (ImplResponse, error)
FindArtifact(context.Context, string, string, string) (ImplResponse, error)
FindInferenceService(context.Context, string, string, string) (ImplResponse, error)
FindModelArtifact(context.Context, string, string, string) (ImplResponse, error)
FindModelVersion(context.Context, string, string, string) (ImplResponse, error)
FindRegisteredModel(context.Context, string, string) (ImplResponse, error)
FindServingEnvironment(context.Context, string, string) (ImplResponse, error)
GetArtifact(context.Context, string) (ImplResponse, error)
GetArtifacts(context.Context, string, model.OrderByField, model.SortOrder, string) (ImplResponse, error)
GetEnvironmentInferenceServices(context.Context, string, string, string, string, model.OrderByField, model.SortOrder, string) (ImplResponse, error)
GetInferenceService(context.Context, string) (ImplResponse, error)
GetInferenceServiceModel(context.Context, string) (ImplResponse, error)
GetInferenceServiceServes(context.Context, string, string, string, string, model.OrderByField, model.SortOrder, string) (ImplResponse, error)
GetInferenceServiceVersion(context.Context, string) (ImplResponse, error)
GetInferenceServices(context.Context, string, model.OrderByField, model.SortOrder, string) (ImplResponse, error)
GetArtifact(context.Context, string) (ImplResponse, error)
GetArtifacts(context.Context, string, model.OrderByField, model.SortOrder, string) (ImplResponse, error)
GetModelArtifact(context.Context, string) (ImplResponse, error)
GetModelArtifacts(context.Context, string, model.OrderByField, model.SortOrder, string) (ImplResponse, error)
GetModelVersion(context.Context, string) (ImplResponse, error)
Expand All @@ -95,8 +101,8 @@ type ModelRegistryServiceAPIServicer interface {
GetRegisteredModels(context.Context, string, model.OrderByField, model.SortOrder, string) (ImplResponse, error)
GetServingEnvironment(context.Context, string) (ImplResponse, error)
GetServingEnvironments(context.Context, string, model.OrderByField, model.SortOrder, string) (ImplResponse, error)
UpdateInferenceService(context.Context, string, model.InferenceServiceUpdate) (ImplResponse, error)
UpdateArtifact(context.Context, string, model.ArtifactUpdate) (ImplResponse, error)
UpdateInferenceService(context.Context, string, model.InferenceServiceUpdate) (ImplResponse, error)
UpdateModelArtifact(context.Context, string, model.ModelArtifactUpdate) (ImplResponse, error)
UpdateModelVersion(context.Context, string, model.ModelVersionUpdate) (ImplResponse, error)
UpdateRegisteredModel(context.Context, string, model.RegisteredModelUpdate) (ImplResponse, error)
Expand Down
3 changes: 2 additions & 1 deletion internal/server/openapi/api_model_registry_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
* REST API for Model Registry to create and manage ML model metadata
*
* API version: v1alpha3
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/

// Generated by: OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.

package openapi

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

// Generated by: OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.

package openapi

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

// Generated by: OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.

package openapi

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

// Generated by: OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.

package openapi

// ImplResponse defines an implementation response with error code and the associated body
type ImplResponse struct {
Body interface{}
Code int
Body interface{}
}
5 changes: 3 additions & 2 deletions internal/server/openapi/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
*
* REST API for Model Registry to create and manage ML model metadata
*
* API version: 1.0.0
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
* API version: v1alpha3
*/

// Generated by: OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.

package openapi

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

// Generated by: OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.

package openapi

import (
Expand Down
26 changes: 15 additions & 11 deletions scripts/gen_openapi_server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ set -e
echo "Generating the OpenAPI server"

PROJECT_ROOT=$(realpath "$(dirname "$0")"/..)
OPENAPI_GENERATOR=${OPENAPI_GENERATOR:-"$PROJECT_ROOT"/bin/openapi-generator-cli}

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

if [[ $(uname) == "Darwin" ]]; then
# introduce -i parameter for Mac OSX sed compatibility
sed -i '' 's/, orderByParam/, model.OrderByField(orderByParam)/g' "$PROJECT_ROOT"/internal/server/openapi/api_model_registry_service.go
sed -i '' 's/, sortOrderParam/, model.SortOrder(sortOrderParam)/g' "$PROJECT_ROOT"/internal/server/openapi/api_model_registry_service.go
else
sed -i 's/, orderByParam/, model.OrderByField(orderByParam)/g' "$PROJECT_ROOT"/internal/server/openapi/api_model_registry_service.go
sed -i 's/, sortOrderParam/, model.SortOrder(sortOrderParam)/g' "$PROJECT_ROOT"/internal/server/openapi/api_model_registry_service.go
fi
function sed_inplace() {
if [[ $(uname) == "Darwin" ]]; then
# introduce -i parameter for Mac OSX sed compatibility
sed -i '' "$@"
else
sed -i "$@"
fi
}

sed_inplace 's/, orderByParam/, model.OrderByField(orderByParam)/g' "$PROJECT_ROOT"/internal/server/openapi/api_model_registry_service.go
sed_inplace 's/, sortOrderParam/, model.SortOrder(sortOrderParam)/g' "$PROJECT_ROOT"/internal/server/openapi/api_model_registry_service.go

echo "Assembling type_assert Go file"
./scripts/gen_type_asserts.sh
Expand Down
34 changes: 34 additions & 0 deletions templates/go-server/api.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{{>partial_header}}
package {{packageName}}

import (
"context"
"net/http"{{#apiInfo}}{{#apis}}{{#imports}}
"{{import}}"{{/imports}}{{/apis}}{{/apiInfo}}

model "github.com/kubeflow/model-registry/pkg/openapi"
)


{{#apiInfo}}{{#apis}}
// {{classname}}Router defines the required methods for binding the api requests to a responses for the {{classname}}
// The {{classname}}Router implementation should parse necessary information from the http request,
// pass the data to a {{classname}}Servicer to perform the required actions, then write the service results to the http response.
type {{classname}}Router interface { {{#operations}}{{#operation}}
{{#isDeprecated}}
// Deprecated
{{/isDeprecated}}
{{operationId}}(http.ResponseWriter, *http.Request){{/operation}}{{/operations}}
}{{/apis}}{{/apiInfo}}{{#apiInfo}}{{#apis}}


// {{classname}}Servicer defines the api actions for the {{classname}} service
// This interface intended to stay up to date with the openapi yaml used to generate it,
// while the service implementation can be ignored with the .openapi-generator-ignore file
// and updated with the logic required for the API.
type {{classname}}Servicer interface { {{#operations}}{{#operation}}
{{#isDeprecated}}
// Deprecated
{{/isDeprecated}}
{{operationId}}(context.Context{{#allParams}}, {{^isPrimitiveType}}model.{{/isPrimitiveType}}{{dataType}}{{/allParams}}) (ImplResponse, error){{/operation}}{{/operations}}
}{{/apis}}{{/apiInfo}}
Loading

0 comments on commit 1ebadef

Please sign in to comment.