fix: ensure models that have variables that contain a complexType of time.Time import the time module (#17452)

* fix: handle variables that have items that contain a complexType of `time.Time` properly

Signed-off-by: Paul Horton <phorton@sonatype.com>

* chore: trigger new CI runs

Signed-off-by: Paul Horton <phorton@sonatype.com>

* test: added test change an regenerated samples

Signed-off-by: Paul Horton <phorton@sonatype.com>

* resolved merge issues with upstream main

Signed-off-by: Paul Horton <phorton@sonatype.com>

* regenerated samples

Signed-off-by: Paul Horton <phorton@sonatype.com>

---------

Signed-off-by: Paul Horton <phorton@sonatype.com>
This commit is contained in:
Paul Horton
2024-01-28 03:36:05 +00:00
committed by GitHub
parent 9952776e9c
commit 5055ebade9
7 changed files with 1297 additions and 28 deletions

View File

@@ -748,10 +748,11 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
}
for (CodegenProperty cp : codegenProperties) {
if (!addedTimeImport && ("time.Time".equals(cp.dataType) || (cp.items != null && "time.Time".equals(cp.items.dataType)))) {
if (!addedTimeImport && ("time.Time".equals(cp.dataType) || (cp.items != null && "time.Time".equals(cp.items.complexType)))) {
imports.add(createMapping("import", "time"));
addedTimeImport = true;
}
if (!addedOSImport && ("*os.File".equals(cp.dataType) ||
(cp.items != null && "*os.File".equals(cp.items.dataType)))) {
imports.add(createMapping("import", "os"));

View File

@@ -1335,11 +1335,22 @@ components:
type: http
scheme: signature
schemas:
MapWithDateTime:
type: object
additionalProperties:
items:
format: date-time
type: string
type: array
Foo:
type: object
required:
- bar
properties:
bar:
$ref: '#/components/schemas/Bar'
map:
$ref: '#/components/schemas/MapWithDateTime'
Bar:
type: string
default: bar

View File

@@ -7,7 +7,7 @@ go 1.13
require (
github.com/OpenAPITools/openapi-generator/samples/client/petstore/go/go-petstore v0.0.0-00010101000000-000000000000
github.com/go-openapi/swag v0.22.3
github.com/stretchr/testify v1.8.1
golang.org/x/net v0.7.0 // indirect
golang.org/x/oauth2 v0.2.0
github.com/stretchr/testify v1.8.4
golang.org/x/net v0.20.0 // indirect
golang.org/x/oauth2 v0.16.0
)

File diff suppressed because it is too large Load Diff

View File

@@ -1245,13 +1245,33 @@ components:
description: Pet object that needs to be added to the store
required: true
schemas:
MapWithDateTime:
additionalProperties:
items:
format: date-time
type: string
type: array
type: object
Foo:
example:
bar: bar
map:
key:
- 2000-01-23T04:56:07.000+00:00
- 2000-01-23T04:56:07.000+00:00
properties:
bar:
default: bar
type: string
map:
additionalProperties:
items:
format: date-time
type: string
type: array
type: object
required:
- bar
type: object
Bar:
default: bar
@@ -2133,6 +2153,10 @@ components:
example:
string:
bar: bar
map:
key:
- 2000-01-23T04:56:07.000+00:00
- 2000-01-23T04:56:07.000+00:00
properties:
string:
$ref: '#/components/schemas/Foo'

View File

@@ -4,13 +4,14 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Bar** | Pointer to **string** | | [optional] [default to "bar"]
**Bar** | **string** | | [default to "bar"]
**Map** | Pointer to [**map[string][]time.Time**](array.md) | | [optional]
## Methods
### NewFoo
`func NewFoo() *Foo`
`func NewFoo(bar string, ) *Foo`
NewFoo instantiates a new Foo object
This constructor will assign default values to properties that have it defined,
@@ -44,11 +45,31 @@ and a boolean to check if the value has been set.
SetBar sets Bar field to given value.
### HasBar
`func (o *Foo) HasBar() bool`
### GetMap
HasBar returns a boolean if a field has been set.
`func (o *Foo) GetMap() map[string][]time.Time`
GetMap returns the Map field if non-nil, zero value otherwise.
### GetMapOk
`func (o *Foo) GetMapOk() (*map[string][]time.Time, bool)`
GetMapOk returns a tuple with the Map field if it's non-nil, zero value otherwise
and a boolean to check if the value has been set.
### SetMap
`func (o *Foo) SetMap(v map[string][]time.Time)`
SetMap sets Map field to given value.
### HasMap
`func (o *Foo) HasMap() bool`
HasMap 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)

View File

@@ -12,6 +12,8 @@ package petstore
import (
"encoding/json"
"time"
"fmt"
)
// checks if the Foo type satisfies the MappedNullable interface at compile time
@@ -19,7 +21,8 @@ var _ MappedNullable = &Foo{}
// Foo struct for Foo
type Foo struct {
Bar *string `json:"bar,omitempty"`
Bar string `json:"bar"`
Map *map[string][]time.Time `json:"map,omitempty"`
AdditionalProperties map[string]interface{}
}
@@ -29,10 +32,9 @@ type _Foo Foo
// 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
func NewFoo() *Foo {
func NewFoo(bar string) *Foo {
this := Foo{}
var bar string = "bar"
this.Bar = &bar
this.Bar = bar
return &this
}
@@ -42,40 +44,64 @@ func NewFoo() *Foo {
func NewFooWithDefaults() *Foo {
this := Foo{}
var bar string = "bar"
this.Bar = &bar
this.Bar = bar
return &this
}
// GetBar returns the Bar field value if set, zero value otherwise.
// GetBar returns the Bar field value
func (o *Foo) GetBar() string {
if o == nil || IsNil(o.Bar) {
if o == nil {
var ret string
return ret
}
return *o.Bar
return o.Bar
}
// GetBarOk returns a tuple with the Bar field value if set, nil otherwise
// GetBarOk returns a tuple with the Bar field value
// and a boolean to check if the value has been set.
func (o *Foo) GetBarOk() (*string, bool) {
if o == nil || IsNil(o.Bar) {
if o == nil {
return nil, false
}
return o.Bar, true
return &o.Bar, true
}
// HasBar returns a boolean if a field has been set.
func (o *Foo) HasBar() bool {
if o != nil && !IsNil(o.Bar) {
// SetBar sets field value
func (o *Foo) SetBar(v string) {
o.Bar = v
}
// GetMap returns the Map field value if set, zero value otherwise.
func (o *Foo) GetMap() map[string][]time.Time {
if o == nil || IsNil(o.Map) {
var ret map[string][]time.Time
return ret
}
return *o.Map
}
// GetMapOk returns a tuple with the Map field value if set, nil otherwise
// and a boolean to check if the value has been set.
func (o *Foo) GetMapOk() (*map[string][]time.Time, bool) {
if o == nil || IsNil(o.Map) {
return nil, false
}
return o.Map, true
}
// HasMap returns a boolean if a field has been set.
func (o *Foo) HasMap() bool {
if o != nil && !IsNil(o.Map) {
return true
}
return false
}
// SetBar gets a reference to the given string and assigns it to the Bar field.
func (o *Foo) SetBar(v string) {
o.Bar = &v
// SetMap gets a reference to the given map[string][]time.Time and assigns it to the Map field.
func (o *Foo) SetMap(v map[string][]time.Time) {
o.Map = &v
}
func (o Foo) MarshalJSON() ([]byte, error) {
@@ -88,8 +114,9 @@ func (o Foo) MarshalJSON() ([]byte, error) {
func (o Foo) ToMap() (map[string]interface{}, error) {
toSerialize := map[string]interface{}{}
if !IsNil(o.Bar) {
toSerialize["bar"] = o.Bar
toSerialize["bar"] = o.Bar
if !IsNil(o.Map) {
toSerialize["map"] = o.Map
}
for key, value := range o.AdditionalProperties {
@@ -100,6 +127,27 @@ func (o Foo) ToMap() (map[string]interface{}, error) {
}
func (o *Foo) UnmarshalJSON(data []byte) (err error) {
// This validates that all required properties are included in the JSON object
// by unmarshalling the object into a generic map with string keys and checking
// that every required field exists as a key in the generic map.
requiredProperties := []string{
"bar",
}
allProperties := make(map[string]interface{})
err = json.Unmarshal(data, &allProperties)
if err != nil {
return err;
}
for _, requiredProperty := range(requiredProperties) {
if _, exists := allProperties[requiredProperty]; !exists {
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}
varFoo := _Foo{}
err = json.Unmarshal(data, &varFoo)
@@ -114,6 +162,7 @@ func (o *Foo) UnmarshalJSON(data []byte) (err error) {
if err = json.Unmarshal(data, &additionalProperties); err == nil {
delete(additionalProperties, "bar")
delete(additionalProperties, "map")
o.AdditionalProperties = additionalProperties
}