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

[Golang] Introduce 'lambda.type-to-name' in models_anyof.mustache #18912

Merged
merged 3 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// {{classname}} {{{description}}}{{^description}}struct for {{{classname}}}{{/description}}
type {{classname}} struct {
{{#anyOf}}
{{{.}}} *{{{.}}}
{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} *{{{.}}}
{{/anyOf}}
}

Expand Down Expand Up @@ -45,17 +45,17 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
{{/mappedModels}}
{{/discriminator}}
{{#anyOf}}
// try to unmarshal JSON data into {{{.}}}
err = json.Unmarshal(data, &dst.{{{.}}});
// try to unmarshal JSON data into {{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}
err = json.Unmarshal(data, &dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}});
if err == nil {
json{{{.}}}, _ := json.Marshal(dst.{{{.}}})
if string(json{{{.}}}) == "{}" { // empty struct
dst.{{{.}}} = nil
json{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}, _ := json.Marshal(dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}})
if string(json{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}) == "{}" { // empty struct
dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} = nil
} else {
return nil // data stored in dst.{{{.}}}, return on the first match
return nil // data stored in dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}, return on the first match
}
} else {
dst.{{{.}}} = nil
dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} = nil
}

{{/anyOf}}
Expand All @@ -65,8 +65,8 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
// Marshal data from the first non-nil pointers in the struct to JSON
func (src *{{classname}}) MarshalJSON() ([]byte, error) {
{{#anyOf}}
if src.{{{.}}} != nil {
return json.Marshal(&src.{{{.}}})
if src.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} != nil {
return json.Marshal(&src.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}})
}

{{/anyOf}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,12 @@ components:
type: array
items:
type: string
AnyOfPrimitiveType:
anyOf:
- $ref: '#/components/schemas/OneOfPrimitiveTypeChild'
- type: integer
format: int32
- $ref: '#/components/schemas/OneOfArrayOfString'
# `dup-prop` is defined in both parent and child models
# but not identical: optional vs required
DuplicatedPropChild:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ docs/AdditionalPropertiesClass.md
docs/AllOfPrimitiveTypes.md
docs/Animal.md
docs/AnotherFakeAPI.md
docs/AnyOfPrimitiveType.md
docs/ApiResponse.md
docs/Apple.md
docs/AppleReq.md
Expand Down Expand Up @@ -91,6 +92,7 @@ model__special_model_name_.go
model_additional_properties_class.go
model_all_of_primitive_types.go
model_animal.go
model_any_of_primitive_type.go
model_api_response.go
model_apple.go
model_apple_req.go
Expand Down
1 change: 1 addition & 0 deletions samples/openapi3/client/petstore/go/go-petstore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ Class | Method | HTTP request | Description
- [AdditionalPropertiesClass](docs/AdditionalPropertiesClass.md)
- [AllOfPrimitiveTypes](docs/AllOfPrimitiveTypes.md)
- [Animal](docs/Animal.md)
- [AnyOfPrimitiveType](docs/AnyOfPrimitiveType.md)
- [ApiResponse](docs/ApiResponse.md)
- [Apple](docs/Apple.md)
- [AppleReq](docs/AppleReq.md)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2113,6 +2113,12 @@ components:
items:
type: string
type: array
AnyOfPrimitiveType:
anyOf:
- $ref: '#/components/schemas/OneOfPrimitiveTypeChild'
- format: int32
type: integer
- $ref: '#/components/schemas/OneOfArrayOfString'
DuplicatedPropChild:
allOf:
- $ref: '#/components/schemas/DuplicatedPropParent'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# AnyOfPrimitiveType

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Name** | Pointer to **string** | | [optional]

## Methods

### NewAnyOfPrimitiveType

`func NewAnyOfPrimitiveType() *AnyOfPrimitiveType`

NewAnyOfPrimitiveType instantiates a new AnyOfPrimitiveType object
This constructor will assign default values to properties that have it defined,
and makes sure properties required by API are set, but the set of arguments
will change when the set of required properties is changed

### NewAnyOfPrimitiveTypeWithDefaults

`func NewAnyOfPrimitiveTypeWithDefaults() *AnyOfPrimitiveType`

NewAnyOfPrimitiveTypeWithDefaults instantiates a new AnyOfPrimitiveType object
This constructor will only assign default values to properties that have it defined,
but it doesn't guarantee that properties required by API are set

### GetName

`func (o *AnyOfPrimitiveType) GetName() string`

GetName returns the Name field if non-nil, zero value otherwise.

### GetNameOk

`func (o *AnyOfPrimitiveType) GetNameOk() (*string, bool)`

GetNameOk returns a tuple with the Name field if it's non-nil, zero value otherwise
and a boolean to check if the value has been set.

### SetName

`func (o *AnyOfPrimitiveType) SetName(v string)`

SetName sets Name field to given value.

### HasName

`func (o *AnyOfPrimitiveType) HasName() bool`

HasName returns a boolean if a field has been set.


[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading