diff --git a/src/Templates/ModelTemplate.cshtml b/src/Templates/ModelTemplate.cshtml
index 05c2c3e2f..cf315dab9 100644
--- a/src/Templates/ModelTemplate.cshtml
+++ b/src/Templates/ModelTemplate.cshtml
@@ -141,7 +141,7 @@ else
}
-@if (Model.BaseIsPolymorphic || Model.IsPolymorphic || Model.AllProperties.Any(p => p.ModelType is DictionaryTypeGo))
+@if (Model.BaseIsPolymorphic || Model.IsPolymorphic || Model.HasFlattenedFields || Model.AllProperties.Any(p => p.ModelType is DictionaryTypeGo))
{
@EmptyLine
@@ -319,7 +319,7 @@ else
{
@:@(Model.Name.FixedValue.ToShortName()).@(p.Name) = @(CodeNamerGo.Instance.GetVariableName(p.Name))
}
- else
+ else
{
@:@(Model.Name.FixedValue.ToShortName()).@(p.Name) = &@(CodeNamerGo.Instance.GetVariableName(p.Name))
}
diff --git a/test/src/tests/generated/lro/models.go b/test/src/tests/generated/lro/models.go
index 662009d23..b5247567c 100644
--- a/test/src/tests/generated/lro/models.go
+++ b/test/src/tests/generated/lro/models.go
@@ -3869,6 +3869,18 @@ type SubProduct struct {
ID *string `json:"id,omitempty"`
}
+// MarshalJSON is the custom marshaler for SubProduct.
+func (sp SubProduct) MarshalJSON() ([]byte, error) {
+ objectMap := make(map[string]interface{})
+ if sp.SubProductProperties != nil {
+ objectMap["properties"] = sp.SubProductProperties
+ }
+ if sp.ID != nil {
+ objectMap["id"] = sp.ID
+ }
+ return json.Marshal(objectMap)
+}
+
// UnmarshalJSON is the custom unmarshaler for SubProduct struct.
func (sp *SubProduct) UnmarshalJSON(body []byte) error {
var m map[string]*json.RawMessage
diff --git a/test/src/tests/generated/model-flattening/models.go b/test/src/tests/generated/model-flattening/models.go
index 84f66481c..82cfda637 100644
--- a/test/src/tests/generated/model-flattening/models.go
+++ b/test/src/tests/generated/model-flattening/models.go
@@ -59,6 +59,21 @@ type Error struct {
*Error `json:"parentError,omitempty"`
}
+// MarshalJSON is the custom marshaler for Error.
+func (e Error) MarshalJSON() ([]byte, error) {
+ objectMap := make(map[string]interface{})
+ if e.Status != nil {
+ objectMap["status"] = e.Status
+ }
+ if e.Message != nil {
+ objectMap["message"] = e.Message
+ }
+ if e.Error != nil {
+ objectMap["parentError"] = e.Error
+ }
+ return json.Marshal(objectMap)
+}
+
// UnmarshalJSON is the custom unmarshaler for Error struct.
func (e *Error) UnmarshalJSON(body []byte) error {
var m map[string]*json.RawMessage
@@ -248,6 +263,15 @@ type ProductWrapper struct {
*WrappedProduct `json:"property,omitempty"`
}
+// MarshalJSON is the custom marshaler for ProductWrapper.
+func (pw ProductWrapper) MarshalJSON() ([]byte, error) {
+ objectMap := make(map[string]interface{})
+ if pw.WrappedProduct != nil {
+ objectMap["property"] = pw.WrappedProduct
+ }
+ return json.Marshal(objectMap)
+}
+
// UnmarshalJSON is the custom unmarshaler for ProductWrapper struct.
func (pw *ProductWrapper) UnmarshalJSON(body []byte) error {
var m map[string]*json.RawMessage
@@ -354,6 +378,21 @@ type SimpleProduct struct {
Description *string `json:"base_product_description,omitempty"`
}
+// MarshalJSON is the custom marshaler for SimpleProduct.
+func (sp SimpleProduct) MarshalJSON() ([]byte, error) {
+ objectMap := make(map[string]interface{})
+ if sp.SimpleProductProperties != nil {
+ objectMap["details"] = sp.SimpleProductProperties
+ }
+ if sp.ProductID != nil {
+ objectMap["base_product_id"] = sp.ProductID
+ }
+ if sp.Description != nil {
+ objectMap["base_product_description"] = sp.Description
+ }
+ return json.Marshal(objectMap)
+}
+
// UnmarshalJSON is the custom unmarshaler for SimpleProduct struct.
func (sp *SimpleProduct) UnmarshalJSON(body []byte) error {
var m map[string]*json.RawMessage
@@ -405,6 +444,21 @@ type SimpleProductProperties struct {
*ProductURL `json:"max_product_image,omitempty"`
}
+// MarshalJSON is the custom marshaler for SimpleProductProperties.
+func (spp SimpleProductProperties) MarshalJSON() ([]byte, error) {
+ objectMap := make(map[string]interface{})
+ if spp.MaxProductDisplayName != nil {
+ objectMap["max_product_display_name"] = spp.MaxProductDisplayName
+ }
+ if spp.Capacity != nil {
+ objectMap["max_product_capacity"] = spp.Capacity
+ }
+ if spp.ProductURL != nil {
+ objectMap["max_product_image"] = spp.ProductURL
+ }
+ return json.Marshal(objectMap)
+}
+
// UnmarshalJSON is the custom unmarshaler for SimpleProductProperties struct.
func (spp *SimpleProductProperties) UnmarshalJSON(body []byte) error {
var m map[string]*json.RawMessage