From 1a4784399ba850ab2c92fb0133d72b2f17c3cedf Mon Sep 17 00:00:00 2001 From: Joel Hendrix Date: Tue, 25 Jun 2024 07:21:01 -0700 Subject: [PATCH] Propagate raw JSON as bytes setting to array element schema (#1394) * Propagate raw JSON as bytes setting to array element schema This ensures that we generate the type as [][]byte. The original intention was to generate this a []byte but that never actually happened and we can't change it now. * bump package version for pending release --- packages/autorest.go/.scripts/regeneration.js | 2 + packages/autorest.go/package.json | 2 +- .../autorest.go/src/transform/transform.ts | 4 +- .../test/misc/azarrayofrawjson/go.mod | 11 ++++ .../test/misc/azarrayofrawjson/go.sum | 12 ++++ .../test/misc/azarrayofrawjson/zz_models.go | 10 ++++ .../misc/azarrayofrawjson/zz_models_serde.go | 60 +++++++++++++++++++ .../test/swagger/arrayOfRawJSON.json | 32 ++++++++++ 8 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 packages/autorest.go/test/misc/azarrayofrawjson/go.mod create mode 100644 packages/autorest.go/test/misc/azarrayofrawjson/go.sum create mode 100644 packages/autorest.go/test/misc/azarrayofrawjson/zz_models.go create mode 100644 packages/autorest.go/test/misc/azarrayofrawjson/zz_models_serde.go create mode 100644 packages/autorest.go/test/swagger/arrayOfRawJSON.json diff --git a/packages/autorest.go/.scripts/regeneration.js b/packages/autorest.go/.scripts/regeneration.js index d7b273361..8e4b6100e 100644 --- a/packages/autorest.go/.scripts/regeneration.js +++ b/packages/autorest.go/.scripts/regeneration.js @@ -137,6 +137,8 @@ generateFromReadme("armmachinelearning", machineLearning, 'package-2022-02-01-pr generate("azalias", 'packages/autorest.go/test/swagger/alias.json', 'test/maps/azalias', '--security=AzureKey --module="azalias" --module-version=0.1.0 --openapi-type="data-plane" --generate-fakes --inject-spans --slice-elements-byval --disallow-unknown-fields --single-client'); +generate("azarrayofrawjson", 'packages/autorest.go/test/swagger/arrayOfRawJSON.json', 'test/misc/azarrayofrawjson', '--security=AzureKey --module="azarrayofrawjson" --module-version=0.1.0 --openapi-type="data-plane" --rawjson-as-bytes=true'); + function should_generate(name) { if (filter !== undefined) { let re = new RegExp(filter); diff --git a/packages/autorest.go/package.json b/packages/autorest.go/package.json index b5d688712..d6723474a 100644 --- a/packages/autorest.go/package.json +++ b/packages/autorest.go/package.json @@ -1,6 +1,6 @@ { "name": "@autorest/go", - "version": "4.0.0-preview.66", + "version": "4.0.0-preview.67", "description": "AutoRest Go Generator", "main": "dist/autorest.go/src/main.js", "type": "module", diff --git a/packages/autorest.go/src/transform/transform.ts b/packages/autorest.go/src/transform/transform.ts index 3c66a3a5b..b8bbfaec5 100644 --- a/packages/autorest.go/src/transform/transform.ts +++ b/packages/autorest.go/src/transform/transform.ts @@ -265,7 +265,9 @@ function schemaTypeToGoType(codeModel: m4.CodeModel, schema: m4.Schema, type: 'P const arrayElem = arraySchema.elementType; if (rawJSONAsBytes && (arrayElem.type === m4.SchemaType.Any || arrayElem.type === m4.SchemaType.AnyObject)) { schema.language.go!.rawJSONAsBytes = rawJSONAsBytes; - return '[]byte'; + // propagate the setting to the element type + arrayElem.language.go!.rawJSONAsBytes = rawJSONAsBytes; + return '[][]byte'; } arraySchema.language.go!.elementIsPtr = !helpers.isTypePassedByValue(arrayElem) && !codeModel.language.go!.sliceElementsByValue; // passing nil for array elements in headers, paths, and query params diff --git a/packages/autorest.go/test/misc/azarrayofrawjson/go.mod b/packages/autorest.go/test/misc/azarrayofrawjson/go.mod new file mode 100644 index 000000000..fb40d6b30 --- /dev/null +++ b/packages/autorest.go/test/misc/azarrayofrawjson/go.mod @@ -0,0 +1,11 @@ +module azarrayofrawjson + +go 1.18 + +require github.com/Azure/azure-sdk-for-go/sdk/azcore v1.12.0 + +require ( + github.com/Azure/azure-sdk-for-go/sdk/internal v1.9.0 // indirect + golang.org/x/net v0.26.0 // indirect + golang.org/x/text v0.16.0 // indirect +) diff --git a/packages/autorest.go/test/misc/azarrayofrawjson/go.sum b/packages/autorest.go/test/misc/azarrayofrawjson/go.sum new file mode 100644 index 000000000..6b8c4a4ad --- /dev/null +++ b/packages/autorest.go/test/misc/azarrayofrawjson/go.sum @@ -0,0 +1,12 @@ +github.com/Azure/azure-sdk-for-go/sdk/azcore v1.12.0 h1:1nGuui+4POelzDwI7RG56yfQJHCnKvwfMoU7VsEp+Zg= +github.com/Azure/azure-sdk-for-go/sdk/azcore v1.12.0/go.mod h1:99EvauvlcJ1U06amZiksfYz/3aFGyIhWGHVyiZXtBAI= +github.com/Azure/azure-sdk-for-go/sdk/internal v1.9.0 h1:H+U3Gk9zY56G3u872L82bk4thcsy2Gghb9ExT4Zvm1o= +github.com/Azure/azure-sdk-for-go/sdk/internal v1.9.0/go.mod h1:mgrmMSgaLp9hmax62XQTd0N4aAqSE5E0DulSpVYK7vc= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= +golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= +golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/packages/autorest.go/test/misc/azarrayofrawjson/zz_models.go b/packages/autorest.go/test/misc/azarrayofrawjson/zz_models.go new file mode 100644 index 000000000..93cdc0e89 --- /dev/null +++ b/packages/autorest.go/test/misc/azarrayofrawjson/zz_models.go @@ -0,0 +1,10 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. +// Code generated by Microsoft (R) AutoRest Code Generator. DO NOT EDIT. +// Changes may cause incorrect behavior and will be lost if the code is regenerated. + +package azarrayofrawjson + +type Example struct { + Rawslice [][]byte +} diff --git a/packages/autorest.go/test/misc/azarrayofrawjson/zz_models_serde.go b/packages/autorest.go/test/misc/azarrayofrawjson/zz_models_serde.go new file mode 100644 index 000000000..347554bbf --- /dev/null +++ b/packages/autorest.go/test/misc/azarrayofrawjson/zz_models_serde.go @@ -0,0 +1,60 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. +// Code generated by Microsoft (R) AutoRest Code Generator. DO NOT EDIT. +// Changes may cause incorrect behavior and will be lost if the code is regenerated. + +package azarrayofrawjson + +import ( + "encoding/json" + "fmt" + "github.com/Azure/azure-sdk-for-go/sdk/azcore" + "reflect" +) + +// MarshalJSON implements the json.Marshaller interface for type Example. +func (e Example) MarshalJSON() ([]byte, error) { + objectMap := make(map[string]any) + populate(objectMap, "rawslice", e.Rawslice) + return json.Marshal(objectMap) +} + +// UnmarshalJSON implements the json.Unmarshaller interface for type Example. +func (e *Example) UnmarshalJSON(data []byte) error { + var rawMsg map[string]json.RawMessage + if err := json.Unmarshal(data, &rawMsg); err != nil { + return fmt.Errorf("unmarshalling type %T: %v", e, err) + } + for key, val := range rawMsg { + var err error + switch key { + case "rawslice": + err = unpopulate(val, "Rawslice", &e.Rawslice) + delete(rawMsg, key) + } + if err != nil { + return fmt.Errorf("unmarshalling type %T: %v", e, err) + } + } + return nil +} + +func populate(m map[string]any, k string, v any) { + if v == nil { + return + } else if azcore.IsNullValue(v) { + m[k] = nil + } else if !reflect.ValueOf(v).IsNil() { + m[k] = v + } +} + +func unpopulate(data json.RawMessage, fn string, v any) error { + if data == nil || string(data) == "null" { + return nil + } + if err := json.Unmarshal(data, v); err != nil { + return fmt.Errorf("struct field %s: %v", fn, err) + } + return nil +} diff --git a/packages/autorest.go/test/swagger/arrayOfRawJSON.json b/packages/autorest.go/test/swagger/arrayOfRawJSON.json new file mode 100644 index 000000000..65a9460a1 --- /dev/null +++ b/packages/autorest.go/test/swagger/arrayOfRawJSON.json @@ -0,0 +1,32 @@ +{ + "swagger": "2.0", + "info": { + "title": "Array of raw JSON", + "version": "2.0", + "description": "Test for array of any as raw JSON" + }, + "schemes": [ + "https" + ], + "host": "contoso.com", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "paths": {}, + "definitions": { + "Example": { + "type": "object", + "properties": { + "rawslice": { + "type": "array", + "items": { + "type": "object" + } + } + } + } + } +}