Skip to content

Commit

Permalink
Handle upstream breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
praneetloke committed Mar 10, 2024
1 parent b53df73 commit bf964bc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,5 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/frand v1.4.2 // indirect
)

replace github.com/getkin/kin-openapi => ../../getkin/kin-openapi
11 changes: 2 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/gedex/inflector v0.0.0-20170307190818-16278e9db813 h1:Uc+IZ7gYqAf/rSGFplbWBSHaGolEQlNLgMgSE3ccnIQ=
github.com/gedex/inflector v0.0.0-20170307190818-16278e9db813/go.mod h1:P+oSoE9yhSRvsmYyZsshflcR6ePWYLql6UU1amW13IM=
github.com/getkin/kin-openapi v0.123.0 h1:zIik0mRwFNLyvtXK274Q6ut+dPh6nlxBp0x7mNrPhs8=
github.com/getkin/kin-openapi v0.123.0/go.mod h1:wb1aSZA/iWmorQP9KTAS/phLj/t17B5jT7+fS8ed9NM=
github.com/gliderlabs/ssh v0.3.5 h1:OcaySEmAQJgyYcArR+gGGTHCyE7nvhEMTlYY+Dp8CpY=
github.com/gliderlabs/ssh v0.3.5/go.mod h1:8XB4KraRrX39qHhT6yxPsHedjA08I/uBVwj4xC+/+z4=
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI=
Expand Down Expand Up @@ -372,12 +370,8 @@ github.com/pulumi/appdash v0.0.0-20231130102222-75f619a67231 h1:vkHw5I/plNdTr435
github.com/pulumi/appdash v0.0.0-20231130102222-75f619a67231/go.mod h1:murToZ2N9hNJzewjHBgfFdXhZKjY3z5cYC1VXk+lbFE=
github.com/pulumi/esc v0.8.2 h1:+PZg+qAWW9SYrRCHex36QNueAWdxz9b7hi/q/Zb31V0=
github.com/pulumi/esc v0.8.2/go.mod h1:v5VAPxYDa9DRwvubbzKt4ZYf5y0esWC2ccSp/AT923I=
github.com/pulumi/pulumi/pkg/v3 v3.106.0 h1:5mQGmad/qS/nESIswUKey6xyFZ2ZJ7iMTwZzoCMN+zI=
github.com/pulumi/pulumi/pkg/v3 v3.106.0/go.mod h1:sZMGEPMp2sLbB0PEyrPWDvAX+W2AwOptAKnKOHyX01k=
github.com/pulumi/pulumi/pkg/v3 v3.109.0 h1:Nww0a/qil7sDBPsott3d3p1YWOQ3QGNYLXTtzzcMz3Q=
github.com/pulumi/pulumi/pkg/v3 v3.109.0/go.mod h1:rU/82+ujNGv9oFx4LJKIIQjYhrl6LV3kOgK6XQgGuEM=
github.com/pulumi/pulumi/sdk/v3 v3.106.0 h1:Og3sPKC3SJ2xyQ0dF5si6C126SwcR6rm4lupHh83ELk=
github.com/pulumi/pulumi/sdk/v3 v3.106.0/go.mod h1:Ml3rpGfyZlI4zQCG7LN2XDSmH4XUNYdyBwJ3yEr/OpI=
github.com/pulumi/pulumi/sdk/v3 v3.109.0 h1:bcejf5DNcx2I66X1M2tYFKzZGvefPD44sGzOMr0UB00=
github.com/pulumi/pulumi/sdk/v3 v3.109.0/go.mod h1:5A6GHUwAJlRY1SSLZh84aDIbsBShcrfcmHzI50ecSBg=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
Expand Down Expand Up @@ -410,8 +404,9 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand All @@ -421,8 +416,6 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/texttheater/golang-levenshtein v1.0.1 h1:+cRNoVrfiwufQPhoMzB6N0Yf/Mqajr6t1lOv8GyGE2U=
Expand Down
42 changes: 21 additions & 21 deletions pkg/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (o *OpenAPIContext) GatherResourcesFromAPI(csharpNamespaces map[string]stri

// Use the type and operationID as a hint to determine if this GET endpoint returns a single resource
// or a list of resources.
if resourceType.Type != openapi3.TypeArray && !strings.Contains(strings.ToLower(pathItem.Get.OperationID), "list") {
if !resourceType.Type.Is(openapi3.TypeArray) && !strings.Contains(strings.ToLower(pathItem.Get.OperationID), "list") {
// If there is a discriminator then we should set this operation
// as the read endpoint for each of the types in the mapping.
if resourceType.Discriminator != nil {
Expand Down Expand Up @@ -196,7 +196,7 @@ func (o *OpenAPIContext) GatherResourcesFromAPI(csharpNamespaces map[string]stri
}

// Add the API operation as a list* function.
if resourceType.Type == openapi3.TypeArray || strings.Contains(strings.ToLower(pathItem.Get.OperationID), "list") {
if resourceType.Type.Is(openapi3.TypeArray) || strings.Contains(strings.ToLower(pathItem.Get.OperationID), "list") {
funcName := "list" + getResourceTitleFromOperationID(pathItem.Get.OperationID, http.MethodGet, o.OperationIdsHaveTypeSpecNamespace)
funcTypeToken := o.Pkg.Name + ":" + module + ":" + funcName
funcSpec, err := o.genListFunc(*pathItem, *jsonReq.Schema, module, funcName)
Expand Down Expand Up @@ -791,7 +791,7 @@ func (o *OpenAPIContext) gatherResourceProperties(resourceName string, requestBo
var types []pschema.TypeSpec
newlyAddedTypes := codegen.NewStringSet()
for _, schemaRef := range requestBodySchema.AllOf {
if schemaRef == nil || (schemaRef.Value.Type != "object" && len(schemaRef.Value.AllOf) == 0) {
if schemaRef == nil || (!schemaRef.Value.Type.Is(openapi3.TypeObject) && len(schemaRef.Value.AllOf) == 0) {
continue
}

Expand Down Expand Up @@ -868,7 +868,7 @@ func (ctx *resourceContext) genPropertySpec(propName string, p openapi3.SchemaRe
Description: p.Value.Description,
}

if p.Value.Default != nil && p.Value.Type != openapi3.TypeArray {
if p.Value.Default != nil && !p.Value.Type.Is(openapi3.TypeArray) {
propertySpec.Default = p.Value.Default
}

Expand Down Expand Up @@ -907,7 +907,7 @@ func (ctx *resourceContext) genPropertySpec(propName string, p openapi3.SchemaRe
func (ctx *resourceContext) propertyTypeSpec(parentName string, propSchema openapi3.SchemaRef) (*pschema.TypeSpec, bool, error) {
// References to other type definitions as long as the type is not an array.
// Arrays and enums will be handled later in this method.
if propSchema.Ref != "" && propSchema.Value.Type != openapi3.TypeArray && len(propSchema.Value.Enum) == 0 {
if propSchema.Ref != "" && !propSchema.Value.Type.Is(openapi3.TypeArray) && len(propSchema.Value.Enum) == 0 {
schemaName := strings.TrimPrefix(propSchema.Ref, componentsSchemaRefPrefix)
typName := ToPascalCase(schemaName)
tok := fmt.Sprintf("%s:%s:%s", ctx.pkg.Name, ctx.mod, typName)
Expand All @@ -925,11 +925,11 @@ func (ctx *resourceContext) propertyTypeSpec(parentName string, propSchema opena
// return a TypeSpec for that type.
// Properties can refer to reusable schema types
// which are actually just simple types.
if typeSchema.Value.Type != openapi3.TypeObject &&
if !typeSchema.Value.Type.Is(openapi3.TypeObject) &&
len(typeSchema.Value.Properties) == 0 &&
len(typeSchema.Value.AllOf) == 0 {
return &pschema.TypeSpec{
Type: typeSchema.Value.Type,
Type: typeSchema.Value.Type.Slice()[0],
}, false, nil
}

Expand Down Expand Up @@ -1049,18 +1049,18 @@ func (ctx *resourceContext) propertyTypeSpec(parentName string, propSchema opena
}

// All other types.
switch propSchema.Value.Type {
case openapi3.TypeInteger:
switch {
case propSchema.Value.Type.Is(openapi3.TypeInteger):
return &pschema.TypeSpec{Type: "integer"}, false, nil
case openapi3.TypeString:
case propSchema.Value.Type.Is(openapi3.TypeString):
return &pschema.TypeSpec{Type: "string"}, false, nil
case openapi3.TypeBoolean:
case propSchema.Value.Type.Is(openapi3.TypeBoolean):
return &pschema.TypeSpec{Type: "boolean"}, false, nil
case openapi3.TypeNumber:
case propSchema.Value.Type.Is(openapi3.TypeNumber):
return &pschema.TypeSpec{Type: "number"}, false, nil
case openapi3.TypeObject:
case propSchema.Value.Type.Is(openapi3.TypeObject):
return &pschema.TypeSpec{Ref: "pulumi.json#/Any"}, false, nil
case openapi3.TypeArray:
case propSchema.Value.Type.Is(openapi3.TypeArray):
elementType, _, err := ctx.propertyTypeSpec(parentName+"Item", *propSchema.Value.Items)
if err != nil {
return nil, false, errors.Wrapf(err, "generating array item type (parentName: %s)", parentName)
Expand Down Expand Up @@ -1140,7 +1140,7 @@ func (ctx *resourceContext) genProperties(parentName string, typeSchema openapi3
// Don't set default values for array-type properties
// since Pulumi doesn't support it and also it isn't
// very helpful anyway for arrays.
if value.Value.Default != nil && value.Value.Type != openapi3.TypeArray {
if value.Value.Default != nil && !value.Value.Type.Is(openapi3.TypeArray) {
propertySpec.Default = value.Value.Default
}

Expand Down Expand Up @@ -1172,7 +1172,7 @@ func (ctx *resourceContext) genPropertiesFromAllOf(parentName string, allOf open
newlyAddedTypes := codegen.NewStringSet()

for _, schemaRef := range allOf {
if schemaRef.Ref == "" && schemaRef.Value.Type != "object" {
if schemaRef.Ref == "" && !schemaRef.Value.Type.Is(openapi3.TypeObject) {
glog.Warningf("Prop type %s uses allOf schema but one of the schema refs is invalid", parentName)
continue
}
Expand Down Expand Up @@ -1260,7 +1260,7 @@ func getIntegerEnumValues(rawEnumValues []interface{}) ([]pschema.EnumValueSpec,

// genEnumType generates the enum type for a given schema.
func (ctx *resourceContext) genEnumType(enumName string, propSchema openapi3.Schema) (*pschema.TypeSpec, error) {
if len(propSchema.Type) == 0 {
if len(propSchema.Type.Slice()) == 0 {
return nil, nil
}

Expand All @@ -1270,16 +1270,16 @@ func (ctx *resourceContext) genEnumType(enumName string, propSchema openapi3.Sch
enumSpec := &pschema.ComplexTypeSpec{
ObjectTypeSpec: pschema.ObjectTypeSpec{
Description: propSchema.Description,
Type: propSchema.Type,
Type: propSchema.Type.Slice()[0],
},
}

var names codegen.StringSet

switch propSchema.Type {
case openapi3.TypeString:
switch {
case propSchema.Type.Is(openapi3.TypeString):
enumSpec.Enum, names = getStringEnumValues(propSchema.Enum)
case openapi3.TypeInteger:
case propSchema.Type.Is(openapi3.TypeInteger):
enumSpec.Enum, names = getIntegerEnumValues(propSchema.Enum)
default:
return nil, errors.Errorf("cannot handle enum values of type %s", propSchema.Type)
Expand Down

0 comments on commit bf964bc

Please sign in to comment.