Skip to content

Commit

Permalink
Fix portal dashboardName validation pattern in metadata (#3568)
Browse files Browse the repository at this point in the history
There's a mistake in the Azure spec where the max length of a Portal
dashboardName is (correctly) 64, but the regex validation pattern only
allows 24 characters:
Azure/azure-rest-api-specs#30458.

Resolves #3560 

This change corrects the validation regex on our side. After the change:

```
> make local_generate

> cat bin/metadata-compact.json | jq > ../m.json

> rg -A 8 dashboardName ../m.json | rg '(dashboardName|pattern)'
      "path": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Portal/dashboards/{dashboardName}",
          "name": "dashboardName",
      "path": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Portal/dashboards/{dashboardName}",
          "name": "dashboardName",
            "pattern": "^[a-zA-Z0-9-]{3,64}$",
      "path": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Portal/dashboards/{dashboardName}",
          "name": "dashboardName",
            "pattern": "^[a-zA-Z0-9-]{3,64}$",
      "path": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Portal/dashboards/{dashboardName}",
          "name": "dashboardName",
            "pattern": "^[a-zA-Z0-9-]{3,64}$",
      "path": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Portal/dashboards/{dashboardName}",
          "name": "dashboardName",
      "path": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Portal/dashboards/{dashboardName}",
          "name": "dashboardName",
            "pattern": "^[a-zA-Z0-9-]{3,64}$"
      "path": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Portal/dashboards/{dashboardName}",
          "name": "dashboardName",
            "pattern": "^[a-zA-Z0-9-]{3,64}$"
      "path": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Portal/dashboards/{dashboardName}",
          "name": "dashboardName",
            "pattern": "^[a-zA-Z0-9-]{3,64}$"
```
  • Loading branch information
thomas11 authored Sep 11, 2024
1 parent f63e8ec commit ad3f4b9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
11 changes: 10 additions & 1 deletion provider/pkg/gen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,15 @@ func (m *moduleGenerator) escapeCSharpNames(typeName string, resourceResponse *p
}
}

func normalizeParamPattern(param *openapi.Parameter) string {
// #3560: the regex has the wrong max length in the spec.
// See portal/resource-manager/Microsoft.Portal/preview/2022-12-01-preview/portal.json#L185-L192
if param.Name == "dashboardName" && *param.MaxLength > 24 && param.Pattern == "^[a-zA-Z0-9-]{3,24}$" {
return fmt.Sprintf("^[a-zA-Z0-9-]{3,%d}$", *param.MaxLength)
}
return param.Pattern
}

func (m *moduleGenerator) genMethodParameters(parameters []spec.Parameter, ctx *openapi.ReferenceContext,
namer *resources.AutoNamer, bodySchema *openapi.Schema) (*parameterBag, error) {
result := newParameterBag()
Expand All @@ -1327,7 +1336,7 @@ func (m *moduleGenerator) genMethodParameters(parameters []spec.Parameter, ctx *
Type: param.Type,
MinLength: param.MinLength,
MaxLength: param.MaxLength,
Pattern: param.Pattern,
Pattern: normalizeParamPattern(param),
},
}

Expand Down
52 changes: 52 additions & 0 deletions provider/pkg/gen/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,55 @@ func TestFindResourcesBodyRefs(t *testing.T) {
expected := []string{"#/definitions/Subnet"}
assert.Equal(t, expected, actual)
}

func TestNormalizePattern(t *testing.T) {
openapiParam := func(pattern string, maxLength int64) *openapi.Parameter {
return &openapi.Parameter{
Parameter: &spec.Parameter{
CommonValidations: spec.CommonValidations{
Pattern: pattern,
MaxLength: &maxLength,
},
ParamProps: spec.ParamProps{
Name: "dashboardName",
},
},
}
}

t.Run("not a dashboard name", func(t *testing.T) {
p := openapiParam("^[a-zA-Z0-9-]{3,24}$", 90)
p.Name = "foo"
assert.Equal(t, "^[a-zA-Z0-9-]{3,24}$", normalizeParamPattern(p))
})

t.Run("empty", func(t *testing.T) {
p := openapiParam("", 0)
assert.Equal(t, "", normalizeParamPattern(p))
})

t.Run("empty with length", func(t *testing.T) {
p := openapiParam("", 90)
assert.Equal(t, "", normalizeParamPattern(p))
})

t.Run("pattern without length", func(t *testing.T) {
p := openapiParam("^[a-zA-Z0-9-]{3,24}$", 0)
assert.Equal(t, "^[a-zA-Z0-9-]{3,24}$", normalizeParamPattern(p))
})

t.Run("pattern with correct length", func(t *testing.T) {
p := openapiParam("^[a-zA-Z0-9-]{3,24}$", 24)
assert.Equal(t, "^[a-zA-Z0-9-]{3,24}$", normalizeParamPattern(p))
})

t.Run("pattern with shorter length", func(t *testing.T) {
p := openapiParam("^[a-zA-Z0-9-]{3,24}$", 23)
assert.Equal(t, "^[a-zA-Z0-9-]{3,24}$", normalizeParamPattern(p))
})

t.Run("pattern with longer length", func(t *testing.T) {
p := openapiParam("^[a-zA-Z0-9-]{3,24}$", 60)
assert.Equal(t, "^[a-zA-Z0-9-]{3,60}$", normalizeParamPattern(p))
})
}

0 comments on commit ad3f4b9

Please sign in to comment.