Skip to content

Commit

Permalink
Ensure discriminated resource names are prefixed with the parent reso…
Browse files Browse the repository at this point in the history
…urce name
  • Loading branch information
praneetloke committed May 1, 2024
1 parent ba60f97 commit ec9964e
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions pkg/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,23 @@ 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
setReadOperationMapping(funcTypeToken)
}
} 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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ec9964e

Please sign in to comment.