Skip to content

fix(go): properly generate inline enums #21623

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
Expand Up @@ -113,6 +113,9 @@ public GoClientCodegen() {
outputFolder = "generated-code/go";
embeddedTemplateDir = templateDir = "go";

// We need inline enums to be resolved to a separate model
inlineSchemaOption.put("RESOLVE_INLINE_ENUMS", "true");

apiTemplateFiles.put("api.mustache", ".go");
modelTemplateFiles.put("model.mustache", ".go");
apiTestTemplateFiles.put("api_test.mustache", ".go");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public void testMultipleRequiredPropertiesHasSameOneOfObject() throws IOExceptio
files.forEach(File::deleteOnExit);

Path docFile = Paths.get(output + "/docs/PetAPI.md");
TestUtils.assertFileContains(docFile, "openapiclient.pet{Cat: openapiclient.NewCat(\"Attr_example\")}, openapiclient.pet{Cat: openapiclient.NewCat(\"Attr_example\")}, openapiclient.pet{Cat: openapiclient.NewCat(\"Attr_example\")}");
TestUtils.assertFileContains(docFile, "openapiclient.pet{Cat: openapiclient.NewCat(openapiclient.cat_attr(\"CAT\"))}, openapiclient.pet{Cat: openapiclient.NewCat(openapiclient.cat_attr(\"CAT\"))}, openapiclient.pet{Cat: openapiclient.NewCat(openapiclient.cat_attr(\"CAT\"))}");
}

@Test
Expand Down Expand Up @@ -396,4 +396,24 @@ public void testAdditionalPropertiesWithoutGoMod() throws Exception {
Path goSumFile = Paths.get(output + "/go.sum");
TestUtils.assertFileNotExists(goSumFile);
}

@Test
public void testInlineEnums_issue9567() throws Exception {
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("go")
.setInputSpec("src/test/resources/bugs/issue_9567.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"))
.addAdditionalProperty(GoClientCodegen.WITH_GO_MOD, false);

DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
System.out.println(files);
files.forEach(File::deleteOnExit);

Path enumFile = Paths.get(output + "/model_pet_status.go");
TestUtils.assertFileExists(enumFile);
}
}
36 changes: 36 additions & 0 deletions modules/openapi-generator/src/test/resources/bugs/issue_9567.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
openapi: 3.0.1
info:
title: TEST
description: |-
## TEST
version: 1.0.0

servers:
- url: /v3
description: Major version of service

paths:
/agreements:
get:
operationId: readAPet
responses:
"200":
description: OK
content:
'*/*':
schema:
$ref: '#/components/schemas/Pet'
components:
schemas:
Pet:
title: a Pet
description: A pet for sale in the pet store
type: object
properties:
status:
type: string
description: pet status in the store
enum:
- available
- pending
- sold
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ docs/HeaderAPI.md
docs/NumberPropertiesOnly.md
docs/PathAPI.md
docs/Pet.md
docs/PetStatus.md
docs/Query.md
docs/QueryAPI.md
docs/QueryOutcomesInner.md
docs/StringEnumRef.md
docs/Tag.md
docs/TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter.md
docs/TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter.md
docs/TestsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPathEnumNonrefStringPathParameter.md
git_push.sh
go.mod
go.sum
Expand All @@ -36,10 +39,13 @@ model_data_query.go
model_default_value.go
model_number_properties_only.go
model_pet.go
model_pet_status.go
model_query.go
model_query_outcomes_inner.go
model_string_enum_ref.go
model_tag.go
model_test_query_style_deep_object_explode_true_object_all_of_query_object_parameter.go
model_test_query_style_form_explode_true_array_string_query_object_parameter.go
model_tests_path_string__path_string__integer__path_integer___enum_nonref_string_path___enum_ref_string_path__enum_nonref_string_path_parameter.go
response.go
utils.go
3 changes: 3 additions & 0 deletions samples/client/echo_api/go-external-refs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,14 @@ Class | Method | HTTP request | Description
- [DefaultValue](docs/DefaultValue.md)
- [NumberPropertiesOnly](docs/NumberPropertiesOnly.md)
- [Pet](docs/Pet.md)
- [PetStatus](docs/PetStatus.md)
- [Query](docs/Query.md)
- [QueryOutcomesInner](docs/QueryOutcomesInner.md)
- [StringEnumRef](docs/StringEnumRef.md)
- [Tag](docs/Tag.md)
- [TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter](docs/TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter.md)
- [TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter](docs/TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter.md)
- [TestsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPathEnumNonrefStringPathParameter](docs/TestsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPathEnumNonrefStringPathParameter.md)


## Documentation For Authorization
Expand Down
56 changes: 25 additions & 31 deletions samples/client/echo_api/go-external-refs/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ paths:
name: enum_nonref_string_path
required: true
schema:
enum:
- success
- failure
- unclassified
type: string
$ref: "#/components/schemas/tests_path_string__path_string__integer__path_integer___enum_nonref_string_path___enum_ref_string_path__enum_nonref_string_path_parameter"
style: simple
- explode: false
in: path
Expand Down Expand Up @@ -127,11 +123,7 @@ paths:
name: enum_nonref_string_header
required: false
schema:
enum:
- success
- failure
- unclassified
type: string
$ref: "#/components/schemas/tests_path_string__path_string__integer__path_integer___enum_nonref_string_path___enum_ref_string_path__enum_nonref_string_path_parameter"
style: form
- explode: true
in: header
Expand Down Expand Up @@ -160,11 +152,7 @@ paths:
name: enum_nonref_string_query
required: false
schema:
enum:
- success
- failure
- unclassified
type: string
$ref: "#/components/schemas/tests_path_string__path_string__integer__path_integer___enum_nonref_string_path___enum_ref_string_path__enum_nonref_string_path_parameter"
style: form
- explode: true
in: query
Expand Down Expand Up @@ -695,12 +683,7 @@ components:
xml:
wrapped: true
status:
description: pet status in the store
enum:
- available
- pending
- sold
type: string
$ref: "#/components/schemas/Pet_status"
required:
- name
- photoUrls
Expand Down Expand Up @@ -728,11 +711,7 @@ components:
- success
- failure
items:
enum:
- success
- failure
- unclassified
type: string
$ref: "#/components/schemas/tests_path_string__path_string__integer__path_integer___enum_nonref_string_path___enum_ref_string_path__enum_nonref_string_path_parameter"
type: array
array_string_default:
default:
Expand Down Expand Up @@ -784,11 +763,7 @@ components:
- SUCCESS
- FAILURE
items:
enum:
- SUCCESS
- FAILURE
- SKIPPED
type: string
$ref: "#/components/schemas/Query_outcomes_inner"
type: array
type: object
x-parent: true
Expand Down Expand Up @@ -877,6 +852,25 @@ components:
required:
- files
type: object
? tests_path_string__path_string__integer__path_integer___enum_nonref_string_path___enum_ref_string_path__enum_nonref_string_path_parameter
: enum:
- success
- failure
- unclassified
type: string
Pet_status:
description: pet status in the store
enum:
- available
- pending
- sold
type: string
Query_outcomes_inner:
enum:
- SUCCESS
- FAILURE
- SKIPPED
type: string
securitySchemes:
http_auth:
scheme: basic
Expand Down
4 changes: 2 additions & 2 deletions samples/client/echo_api/go-external-refs/api_header.go

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

4 changes: 2 additions & 2 deletions samples/client/echo_api/go-external-refs/api_path.go

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

4 changes: 2 additions & 2 deletions samples/client/echo_api/go-external-refs/api_query.go

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

8 changes: 4 additions & 4 deletions samples/client/echo_api/go-external-refs/docs/DefaultValue.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**ArrayStringEnumRefDefault** | Pointer to [**[]StringEnumRef**](StringEnumRef.md) | | [optional] [default to ["success","failure"]]
**ArrayStringEnumDefault** | Pointer to **[]string** | | [optional] [default to ["success","failure"]]
**ArrayStringEnumDefault** | Pointer to [**[]TestsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPathEnumNonrefStringPathParameter**](TestsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPathEnumNonrefStringPathParameter.md) | | [optional] [default to ["success","failure"]]
**ArrayStringDefault** | Pointer to **[]string** | | [optional] [default to ["failure","skipped"]]
**ArrayIntegerDefault** | Pointer to **[]int32** | | [optional] [default to [1,3]]
**ArrayString** | Pointer to **[]string** | | [optional]
Expand Down Expand Up @@ -59,20 +59,20 @@ HasArrayStringEnumRefDefault returns a boolean if a field has been set.

### GetArrayStringEnumDefault

`func (o *DefaultValue) GetArrayStringEnumDefault() []string`
`func (o *DefaultValue) GetArrayStringEnumDefault() []TestsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPathEnumNonrefStringPathParameter`

GetArrayStringEnumDefault returns the ArrayStringEnumDefault field if non-nil, zero value otherwise.

### GetArrayStringEnumDefaultOk

`func (o *DefaultValue) GetArrayStringEnumDefaultOk() (*[]string, bool)`
`func (o *DefaultValue) GetArrayStringEnumDefaultOk() (*[]TestsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPathEnumNonrefStringPathParameter, bool)`

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

### SetArrayStringEnumDefault

`func (o *DefaultValue) SetArrayStringEnumDefault(v []string)`
`func (o *DefaultValue) SetArrayStringEnumDefault(v []TestsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPathEnumNonrefStringPathParameter)`

SetArrayStringEnumDefault sets ArrayStringEnumDefault field to given value.

Expand Down
4 changes: 2 additions & 2 deletions samples/client/echo_api/go-external-refs/docs/HeaderAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
integerHeader := int32(56) // int32 | (optional)
booleanHeader := true // bool | (optional)
stringHeader := "stringHeader_example" // string | (optional)
enumNonrefStringHeader := "enumNonrefStringHeader_example" // string | (optional)
enumNonrefStringHeader := openapiclient.tests_path_string__path_string__integer__path_integer___enum_nonref_string_path___enum_ref_string_path__enum_nonref_string_path_parameter("success") // TestsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPathEnumNonrefStringPathParameter | (optional)
enumRefStringHeader := openapiclient.StringEnumRef("success") // StringEnumRef | (optional)

configuration := openapiclient.NewConfiguration()
Expand Down Expand Up @@ -61,7 +61,7 @@ Name | Type | Description | Notes
**integerHeader** | **int32** | |
**booleanHeader** | **bool** | |
**stringHeader** | **string** | |
**enumNonrefStringHeader** | **string** | |
**enumNonrefStringHeader** | [**TestsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPathEnumNonrefStringPathParameter**](TestsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPathEnumNonrefStringPathParameter.md) | |
**enumRefStringHeader** | [**StringEnumRef**](StringEnumRef.md) | |

### Return type
Expand Down
4 changes: 2 additions & 2 deletions samples/client/echo_api/go-external-refs/docs/PathAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
func main() {
pathString := "pathString_example" // string |
pathInteger := int32(56) // int32 |
enumNonrefStringPath := "enumNonrefStringPath_example" // string |
enumNonrefStringPath := openapiclient.tests_path_string__path_string__integer__path_integer___enum_nonref_string_path___enum_ref_string_path__enum_nonref_string_path_parameter("success") // TestsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPathEnumNonrefStringPathParameter |
enumRefStringPath := openapiclient.StringEnumRef("success") // StringEnumRef |

configuration := openapiclient.NewConfiguration()
Expand All @@ -54,7 +54,7 @@ Name | Type | Description | Notes
**ctx** | **context.Context** | context for authentication, logging, cancellation, deadlines, tracing, etc.
**pathString** | **string** | |
**pathInteger** | **int32** | |
**enumNonrefStringPath** | **string** | |
**enumNonrefStringPath** | [**TestsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPathEnumNonrefStringPathParameter**](.md) | |
**enumRefStringPath** | [**StringEnumRef**](.md) | |

### Other Parameters
Expand Down
8 changes: 4 additions & 4 deletions samples/client/echo_api/go-external-refs/docs/Pet.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Name | Type | Description | Notes
**Category** | Pointer to [**Category**](Category.md) | | [optional]
**PhotoUrls** | **[]string** | |
**Tags** | Pointer to [**[]Tag**](Tag.md) | | [optional]
**Status** | Pointer to **string** | pet status in the store | [optional]
**Status** | Pointer to [**PetStatus**](PetStatus.md) | | [optional]

## Methods

Expand Down Expand Up @@ -147,20 +147,20 @@ HasTags returns a boolean if a field has been set.

### GetStatus

`func (o *Pet) GetStatus() string`
`func (o *Pet) GetStatus() PetStatus`

GetStatus returns the Status field if non-nil, zero value otherwise.

### GetStatusOk

`func (o *Pet) GetStatusOk() (*string, bool)`
`func (o *Pet) GetStatusOk() (*PetStatus, bool)`

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

### SetStatus

`func (o *Pet) SetStatus(v string)`
`func (o *Pet) SetStatus(v PetStatus)`

SetStatus sets Status field to given value.

Expand Down
15 changes: 15 additions & 0 deletions samples/client/echo_api/go-external-refs/docs/PetStatus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# PetStatus

## Enum


* `AVAILABLE` (value: `"available"`)

* `PENDING` (value: `"pending"`)

* `SOLD` (value: `"sold"`)


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


Loading
Loading