Skip to content

Commit 6e78021

Browse files
authored
[Go] Client Models using AllOf, OneOf, or AnyOf Correctly Include time and os imports (Attempt 2) (#14459)
* Currently, if a Model is an allOf the time and os imports are not correctly added to the generated file. This was introduced recently with a fix to not include those imports when the model is a composedSchema #13833. The logic in that fix was just slightly off as an allOf should be treated the same as a standard model. If a model is an AllOf or does not have any composed schemas at all, the sub-models are in-lined defined in the struct. In this case, the standard logic of including the time and os imports apply. If a model is a OneOf or AnyOf, the sub-models are included as pointers to the defined model. In this case, do not include those items in the logic of including time and os imports. * Update example to include a time in an allOf * Add back the accidentally removed nil check
1 parent 6cd7989 commit 6e78021

File tree

9 files changed

+444
-18
lines changed

9 files changed

+444
-18
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -634,9 +634,6 @@ public ModelsMap postProcessModels(ModelsMap objs) {
634634

635635
List<CodegenProperty> inheritedProperties = new ArrayList<>();
636636
if (model.getComposedSchemas() != null) {
637-
if (model.getComposedSchemas().getAllOf() != null) {
638-
inheritedProperties.addAll(model.getComposedSchemas().getAllOf());
639-
}
640637
if (model.getComposedSchemas().getAnyOf() != null) {
641638
inheritedProperties.addAll(model.getComposedSchemas().getAnyOf());
642639
}
@@ -646,22 +643,18 @@ public ModelsMap postProcessModels(ModelsMap objs) {
646643
}
647644

648645
List<CodegenProperty> codegenProperties = new ArrayList<>();
649-
if(model.getIsModel() || model.getComposedSchemas() == null) {
650-
// If the model is a model, use model.vars as it only
651-
// contains properties the generated struct will own itself.
652-
// If model is no model and it has no composed schemas use
653-
// model.vars.
646+
if(model.getComposedSchemas() == null || (model.getComposedSchemas() != null && model.getComposedSchemas().getAllOf() != null)) {
647+
// If the model is an allOf or does not have any composed schemas, then we can use the model's properties.
654648
codegenProperties.addAll(model.vars);
655649
} else {
656650
// If the model is no model, but is a
657-
// allOf, anyOf or oneOf, add all first level options
658-
// from allOf, anyOf or oneOf.
651+
// anyOf or oneOf, add all first level options
652+
// from anyOf or oneOf.
659653
codegenProperties.addAll(inheritedProperties);
660654
}
661655

662656
for (CodegenProperty cp : codegenProperties) {
663-
if (!addedTimeImport && ("time.Time".equals(cp.dataType) ||
664-
(cp.items != null && "time.Time".equals(cp.items.dataType)))) {
657+
if (!addedTimeImport && ("time.Time".equals(cp.dataType) || (cp.items != null && "time.Time".equals(cp.items.dataType)))) {
665658
imports.add(createMapping("import", "time"));
666659
addedTimeImport = true;
667660
}
@@ -671,16 +664,10 @@ public ModelsMap postProcessModels(ModelsMap objs) {
671664
addedOSImport = true;
672665
}
673666
}
674-
675667
if (this instanceof GoClientCodegen && model.isEnum) {
676668
imports.add(createMapping("import", "fmt"));
677669
}
678670

679-
// if oneOf contains "time.Time" type
680-
if (!addedTimeImport && model.oneOf != null && model.oneOf.contains("time.Time")) {
681-
imports.add(createMapping("import", "time"));
682-
}
683-
684671
// if oneOf contains "null" type
685672
if (model.oneOf != null && !model.oneOf.isEmpty() && model.oneOf.contains("nil")) {
686673
model.isNullable = true;

modules/openapi-generator/src/test/resources/3_0/go/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,3 +2051,10 @@ components:
20512051
- type: string
20522052
- format: date-time
20532053
type: string
2054+
AllOfPrimitiveTypes:
2055+
allOf:
2056+
- type: object
2057+
properties:
2058+
test:
2059+
type: string
2060+
format: date-time

samples/openapi3/client/petstore/go/go-petstore/.openapi-generator/FILES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ api_user.go
1212
client.go
1313
configuration.go
1414
docs/AdditionalPropertiesClass.md
15+
docs/AllOfPrimitiveTypes.md
16+
docs/AllOfPrimitiveTypesAllOf.md
1517
docs/Animal.md
1618
docs/AnotherFakeApi.md
1719
docs/ApiResponse.md
@@ -88,6 +90,8 @@ model_200_response.go
8890
model__foo_get_default_response.go
8991
model__special_model_name_.go
9092
model_additional_properties_class.go
93+
model_all_of_primitive_types.go
94+
model_all_of_primitive_types_all_of.go
9195
model_animal.go
9296
model_api_response.go
9397
model_apple.go

samples/openapi3/client/petstore/go/go-petstore/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ Class | Method | HTTP request | Description
123123
## Documentation For Models
124124

125125
- [AdditionalPropertiesClass](docs/AdditionalPropertiesClass.md)
126+
- [AllOfPrimitiveTypes](docs/AllOfPrimitiveTypes.md)
127+
- [AllOfPrimitiveTypesAllOf](docs/AllOfPrimitiveTypesAllOf.md)
126128
- [Animal](docs/Animal.md)
127129
- [ApiResponse](docs/ApiResponse.md)
128130
- [Apple](docs/Apple.md)

samples/openapi3/client/petstore/go/go-petstore/api/openapi.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,9 @@ components:
20142014
- type: string
20152015
- format: date-time
20162016
type: string
2017+
AllOfPrimitiveTypes:
2018+
allOf:
2019+
- $ref: '#/components/schemas/AllOfPrimitiveTypes_allOf'
20172020
_foo_get_default_response:
20182021
example:
20192022
string:
@@ -2176,6 +2179,13 @@ components:
21762179
type: string
21772180
type: object
21782181
example: null
2182+
AllOfPrimitiveTypes_allOf:
2183+
properties:
2184+
test:
2185+
format: date-time
2186+
type: string
2187+
type: object
2188+
example: null
21792189
securitySchemes:
21802190
petstore_auth:
21812191
flows:
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# AllOfPrimitiveTypes
2+
3+
## Properties
4+
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**Test** | Pointer to **time.Time** | | [optional]
8+
9+
## Methods
10+
11+
### NewAllOfPrimitiveTypes
12+
13+
`func NewAllOfPrimitiveTypes() *AllOfPrimitiveTypes`
14+
15+
NewAllOfPrimitiveTypes instantiates a new AllOfPrimitiveTypes object
16+
This constructor will assign default values to properties that have it defined,
17+
and makes sure properties required by API are set, but the set of arguments
18+
will change when the set of required properties is changed
19+
20+
### NewAllOfPrimitiveTypesWithDefaults
21+
22+
`func NewAllOfPrimitiveTypesWithDefaults() *AllOfPrimitiveTypes`
23+
24+
NewAllOfPrimitiveTypesWithDefaults instantiates a new AllOfPrimitiveTypes object
25+
This constructor will only assign default values to properties that have it defined,
26+
but it doesn't guarantee that properties required by API are set
27+
28+
### GetTest
29+
30+
`func (o *AllOfPrimitiveTypes) GetTest() time.Time`
31+
32+
GetTest returns the Test field if non-nil, zero value otherwise.
33+
34+
### GetTestOk
35+
36+
`func (o *AllOfPrimitiveTypes) GetTestOk() (*time.Time, bool)`
37+
38+
GetTestOk returns a tuple with the Test field if it's non-nil, zero value otherwise
39+
and a boolean to check if the value has been set.
40+
41+
### SetTest
42+
43+
`func (o *AllOfPrimitiveTypes) SetTest(v time.Time)`
44+
45+
SetTest sets Test field to given value.
46+
47+
### HasTest
48+
49+
`func (o *AllOfPrimitiveTypes) HasTest() bool`
50+
51+
HasTest returns a boolean if a field has been set.
52+
53+
54+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
55+
56+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# AllOfPrimitiveTypesAllOf
2+
3+
## Properties
4+
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**Test** | Pointer to **time.Time** | | [optional]
8+
9+
## Methods
10+
11+
### NewAllOfPrimitiveTypesAllOf
12+
13+
`func NewAllOfPrimitiveTypesAllOf() *AllOfPrimitiveTypesAllOf`
14+
15+
NewAllOfPrimitiveTypesAllOf instantiates a new AllOfPrimitiveTypesAllOf object
16+
This constructor will assign default values to properties that have it defined,
17+
and makes sure properties required by API are set, but the set of arguments
18+
will change when the set of required properties is changed
19+
20+
### NewAllOfPrimitiveTypesAllOfWithDefaults
21+
22+
`func NewAllOfPrimitiveTypesAllOfWithDefaults() *AllOfPrimitiveTypesAllOf`
23+
24+
NewAllOfPrimitiveTypesAllOfWithDefaults instantiates a new AllOfPrimitiveTypesAllOf object
25+
This constructor will only assign default values to properties that have it defined,
26+
but it doesn't guarantee that properties required by API are set
27+
28+
### GetTest
29+
30+
`func (o *AllOfPrimitiveTypesAllOf) GetTest() time.Time`
31+
32+
GetTest returns the Test field if non-nil, zero value otherwise.
33+
34+
### GetTestOk
35+
36+
`func (o *AllOfPrimitiveTypesAllOf) GetTestOk() (*time.Time, bool)`
37+
38+
GetTestOk returns a tuple with the Test field if it's non-nil, zero value otherwise
39+
and a boolean to check if the value has been set.
40+
41+
### SetTest
42+
43+
`func (o *AllOfPrimitiveTypesAllOf) SetTest(v time.Time)`
44+
45+
SetTest sets Test field to given value.
46+
47+
### HasTest
48+
49+
`func (o *AllOfPrimitiveTypesAllOf) HasTest() bool`
50+
51+
HasTest returns a boolean if a field has been set.
52+
53+
54+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
55+
56+

samples/openapi3/client/petstore/go/go-petstore/model_all_of_primitive_types.go

Lines changed: 152 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)