From ec9964e7e561f17621ced383bc726aa11425b9b6 Mon Sep 17 00:00:00 2001 From: Praneet Loke <1466314+praneetloke@users.noreply.github.com> Date: Wed, 1 May 2024 10:27:38 -0700 Subject: [PATCH] Ensure discriminated resource names are prefixed with the parent resource name --- pkg/openapi.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/pkg/openapi.go b/pkg/openapi.go index ef3efdf..e0f7bba 100644 --- a/pkg/openapi.go +++ b/pkg/openapi.go @@ -168,11 +168,12 @@ func (o *OpenAPIContext) GatherResourcesFromAPI(csharpNamespaces map[string]stri for _, ref := range resourceType.Discriminator.Mapping { schemaName := strings.TrimPrefix(ref, componentsSchemaRefPrefix) dResource := o.Doc.Components.Schemas[schemaName] + parentResourceName := getResourceTitleFromOperationID(pathItem.Get.OperationID, http.MethodGet, o.OperationIdsHaveTypeSpecNamespace) title := getResourceTitleFromRequestSchema(schemaName, dResource) - typeToken := fmt.Sprintf("%s:%s:%s", o.Pkg.Name, module, title) + typeToken := fmt.Sprintf("%s:%s:%s", o.Pkg.Name, module, parentResourceName+title) setReadOperationMapping(typeToken) - funcName := "get" + dResource.Value.Title + funcName := "get" + title funcTypeToken := o.Pkg.Name + ":" + module + ":" + funcName getterFuncSpec := o.genGetFunc(*pathItem, *dResource, module, funcName) o.Pkg.Functions[funcTypeToken] = getterFuncSpec @@ -180,6 +181,10 @@ func (o *OpenAPIContext) GatherResourcesFromAPI(csharpNamespaces map[string]stri } } else { resourceName := getResourceTitleFromOperationID(pathItem.Get.OperationID, http.MethodGet, o.OperationIdsHaveTypeSpecNamespace) + // HACK! Use the singular version of the resource name + // where the current operation is fetching a single + // item instead of a list. + resourceName = strings.TrimSuffix(resourceName, "s") // The resource needs to be read from the cloud provider API, // so we should map this "read" endpoint for this resource. @@ -258,8 +263,9 @@ func (o *OpenAPIContext) GatherResourcesFromAPI(csharpNamespaces map[string]stri for _, n := range schemaNames { dResource := o.Doc.Components.Schemas[n] + parentResourceName := getResourceTitleFromOperationID(pathItem.Patch.OperationID, http.MethodPatch, o.OperationIdsHaveTypeSpecNamespace) resourceName := getResourceTitleFromRequestSchema(n, dResource) - typeToken := fmt.Sprintf("%s:%s:%s", o.Pkg.Name, module, resourceName) + typeToken := fmt.Sprintf("%s:%s:%s", o.Pkg.Name, module, parentResourceName+resourceName) setUpdateOperationMapping(typeToken) } } else { @@ -296,8 +302,9 @@ func (o *OpenAPIContext) GatherResourcesFromAPI(csharpNamespaces map[string]stri for _, ref := range resourceType.Discriminator.Mapping { schemaName := strings.TrimPrefix(ref, componentsSchemaRefPrefix) dResource := o.Doc.Components.Schemas[schemaName] + parentResourceName := getResourceTitleFromOperationID(pathItem.Put.OperationID, http.MethodPut, o.OperationIdsHaveTypeSpecNamespace) resourceName := getResourceTitleFromRequestSchema(schemaName, dResource) - typeToken := fmt.Sprintf("%s:%s:%s", o.Pkg.Name, module, resourceName) + typeToken := fmt.Sprintf("%s:%s:%s", o.Pkg.Name, module, parentResourceName+resourceName) setPutOperationMapping(typeToken) } } else { @@ -345,8 +352,9 @@ func (o *OpenAPIContext) GatherResourcesFromAPI(csharpNamespaces map[string]stri for _, ref := range resourceType.Discriminator.Mapping { schemaName := strings.TrimPrefix(ref, componentsSchemaRefPrefix) dResource := o.Doc.Components.Schemas[schemaName] + parentResourceName := getResourceTitleFromOperationID(pathItem.Delete.OperationID, http.MethodDelete, o.OperationIdsHaveTypeSpecNamespace) resourceName := getResourceTitleFromRequestSchema(schemaName, dResource) - typeToken := fmt.Sprintf("%s:%s:%s", o.Pkg.Name, module, resourceName) + typeToken := fmt.Sprintf("%s:%s:%s", o.Pkg.Name, module, parentResourceName+resourceName) setDeleteOperationMapping(typeToken) } } else {