Skip to content

Commit

Permalink
transform-swagger: fix GoogleRPCStatus case (#138)
Browse files Browse the repository at this point in the history
When updating cloud-packer-service to grpc-gateway v2, we broke the
generated SDK for the service because the error type for all our API
calls was changed from #/definitions/grpc.gateway.runtime.Error to
\#/definitions/google.rpc.Status.

This change then conflicted with the transform-swagger code, as it would
invoke `toCamel' on it, rendering the generated type as
`*cloud.GoogleRpcStatus', where the real type is
`*cloud.GoogleRPCStatus'.

To circumvent this problem, we hard-code this special case in the
transform command, so we end-up with the expected type in the generated
Go code.
  • Loading branch information
lbajolet-hashicorp authored Nov 28, 2022
1 parent b828063 commit c81b933
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/138.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:BUG
Added exception for google.rpc.status when generating code since it was mistakenly rendered as `GoogleRpcStatus` instead of `GoogleRPCStatus`
```
18 changes: 15 additions & 3 deletions cmd/transform-swagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ func loadSharedDefinitions(sharedPath, svcPath string) (map[string]bool, error)
return sharedDefs, nil
}

// sharedDefNameReplaces is used for replacing with specific values those that
// are incompatible with `strcase.ToCamel`
//
// For example, google.rpc.Status, when fed into ToCamel, yields GoogleRpcStatus,
// which is not the name of the structure, but GoogleRPCStatus
var sharedDefNameReplaces = map[string]string{
"google.rpc.Status": "GoogleRPCStatus",
}

// addSharedExtensions loops over each shared type definition in a service spec and adds the type that it should reuse.
// Without adding the type reuse extension, separate copies of each shared type definition are generated alongside the service-specific type definitions.
func addSharedExtension(apiSpec *spec.Swagger, sharedDefs map[string]bool) (*spec.Swagger, error) {
Expand All @@ -154,9 +163,12 @@ func addSharedExtension(apiSpec *spec.Swagger, sharedDefs map[string]bool) (*spe
continue
}

// The shared definition name gets transformed into a camelcased Go type.
// example: hashicorp.cloud.common.PaginationRequest -> HashicorpCloudCommonPaginationRequest
genTypeName := strcase.ToCamel(sharedDefName)
genTypeName := sharedDefNameReplaces[sharedDefName]
if genTypeName == "" {
// Unless for some exceptions, the shared definition name gets transformed into a camelcased Go type.
// example: hashicorp.cloud.common.PaginationRequest -> HashicorpCloudCommonPaginationRequest
genTypeName = strcase.ToCamel(sharedDefName)
}

// This struct contains all the data, like which package to import, needed for the SDK client generator to ensure the service reuses
// the generated shared type, rather than a service-specific duplicate of that shared type.
Expand Down

0 comments on commit c81b933

Please sign in to comment.