Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that we do not marshal read-only fields into request body in any level #438

Closed
ArcturusZhang opened this issue Jul 14, 2020 · 2 comments · Fixed by #446
Closed

Ensure that we do not marshal read-only fields into request body in any level #438

ArcturusZhang opened this issue Jul 14, 2020 · 2 comments · Fixed by #446

Comments

@ArcturusZhang
Copy link
Member

ArcturusZhang commented Jul 14, 2020

In the current go SDK, we do not have custom marshaler for every struct, and some of them may have read-only fields. Without a custom marshaler, the read-only fields will be marshaled into the request body.

For instance, the VirtualMachineProperties does not have a custom marshaler, despite the VMID field is read-only, it will still be in the request body if assigned.

Sample code of reproduce:

func createVM(authorizer autorest.Authorizer) {
	client := compute.NewVirtualMachinesClient(subsID)
	client.Authorizer = authorizer
	vm := compute.VirtualMachine{
		Location: to.StringPtr("eastus"),
		VirtualMachineProperties: &compute.VirtualMachineProperties{
			VMID: to.StringPtr("TEST"),
		},
	}
	future, err := client.CreateOrUpdate(ctx, "test-rg", "test-vm", vm)
	if err != nil {
		panic(err)
	}
	if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
		panic(err)
	}
}

Log:

(2020-07-14T16:13:04.0542686+08:00) INFO: REQUEST: PUT https://management.azure.com/subscriptions/**REDACTED**/resourceGroups/test-rg/providers/
Microsoft.Compute/virtualMachines/test-vm?api-version=2019-12-01
Content-Type: application/json; charset=utf-8
User-Agent: Go/go1.13.6 (amd64-windows) go-autorest/v14.0.0 Azure-SDK-For-Go/1.0.0 compute/2019-12-01
Authorization: **REDACTED**
{"location":"eastus","properties":{"vmId":"TEST"}}
@jhendrixMSFT
Copy link
Member

It looks like the condition in the template doesn't explicitly check if a model contains R/O properties.

https://github.com/Azure/autorest.go/blob/master/src/Templates/ModelTemplate.cshtml#L142

Can you investigate further?

@ArcturusZhang
Copy link
Member Author

@jhendrixMSFT I will take a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants