Skip to content

Commit

Permalink
[Go] Fix an issue causing int array reference translated to an invali…
Browse files Browse the repository at this point in the history
…d type []Integer (#19013)

* [Go] Fix an issue causing int array ref be converted into an invalid type, `[]Integer`

* Add test case: wrapped and referenced integer of an array to Go client samples

* add check for ref to property in go abstract

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

Co-authored-by: Zhiwei <[email protected]>

---------

Co-authored-by: Zhiwei Liang <[email protected]>
Co-authored-by: Zhiwei <[email protected]>
  • Loading branch information
3 people committed Jun 25, 2024
1 parent ba9ac74 commit 6aa825b
Show file tree
Hide file tree
Showing 11 changed files with 708 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,27 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
private final Logger LOGGER = LoggerFactory.getLogger(AbstractGoCodegen.class);
private static final String NUMERIC_ENUM_PREFIX = "_";

@Setter protected boolean withGoCodegenComment = false;
@Setter protected boolean withAWSV4Signature = false;
@Setter protected boolean withXml = false;
@Setter protected boolean enumClassPrefix = false;
@Setter protected boolean structPrefix = false;
@Setter protected boolean generateInterfaces = false;
@Setter protected boolean withGoMod = false;
@Setter protected boolean generateMarshalJSON = true;
@Setter protected boolean generateUnmarshalJSON = true;

@Setter protected String packageName = "openapi";
@Setter
protected boolean withGoCodegenComment = false;
@Setter
protected boolean withAWSV4Signature = false;
@Setter
protected boolean withXml = false;
@Setter
protected boolean enumClassPrefix = false;
@Setter
protected boolean structPrefix = false;
@Setter
protected boolean generateInterfaces = false;
@Setter
protected boolean withGoMod = false;
@Setter
protected boolean generateMarshalJSON = true;
@Setter
protected boolean generateUnmarshalJSON = true;

@Setter
protected String packageName = "openapi";
protected Set<String> numberTypes;

public AbstractGoCodegen() {
Expand Down Expand Up @@ -424,17 +434,26 @@ public String getSchemaType(Schema p) {
String ref = p.get$ref();
String type;

// schema is a ref to property's schema e.g. #/components/schemas/Pet/properties/id
if (ModelUtils.isRefToSchemaWithProperties(ref)) {
Schema propertySchema = ModelUtils.getSchemaFromRefToSchemaWithProperties(openAPI, ref);
openAPIType = super.getSchemaType(propertySchema);
ref = propertySchema.get$ref();
}

if (ref != null && !ref.isEmpty()) {
type = toModelName(openAPIType);
} else if ("object".equals(openAPIType) && ModelUtils.isAnyType(p)) {
// Arbitrary type. Note this is not the same thing as free-form object.
type = "interface{}";
} else if (typeMapping.containsKey(openAPIType)) {
type = typeMapping.get(openAPIType);
if (languageSpecificPrimitives.contains(type))
if (languageSpecificPrimitives.contains(type)) {
return (type);
} else
}
} else {
type = openAPIType;
}
return type;
}

Expand Down Expand Up @@ -753,7 +772,7 @@ public ModelsMap postProcessModels(ModelsMap objs) {
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"));
Expand All @@ -762,15 +781,15 @@ public ModelsMap postProcessModels(ModelsMap objs) {

if (cp.pattern != null) {
cp.vendorExtensions.put("x-go-custom-tag", "validate:\"regexp=" +
cp.pattern.replace("\\","\\\\").replaceAll("^/|/$","") +
"\"");
cp.pattern.replace("\\", "\\\\").replaceAll("^/|/$", "") +
"\"");
}
}
if (this instanceof GoClientCodegen && model.isEnum) {
imports.add(createMapping("import", "fmt"));
}

if(model.oneOf != null && !model.oneOf.isEmpty() && !addedValidator && generateUnmarshalJSON) {
if (model.oneOf != null && !model.oneOf.isEmpty() && !addedValidator && generateUnmarshalJSON) {
imports.add(createMapping("import", "gopkg.in/validator.v2"));
addedValidator = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,29 @@ paths:
responses:
200:
description: OK
/fake/wrapped-integer-ref:
post:
operationId: someOpsRequiringRefInt
requestBody:
required: true
content:
application/json:
schema:
type: object
required:
- ids
properties:
MyIDs:
type: array
items:
$ref: '#/components/schemas/IDsWrapper/properties/id'
responses:
'200':
description: success
content:
application/json:
schema:
type: object
servers:
- url: 'http://{server}.swagger.io:{port}/v2'
description: petstore server
Expand Down Expand Up @@ -2191,3 +2214,8 @@ components:
type: object
- type: object
- type: "null"
IDsWrapper:
type: object
properties:
id:
type: integer
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ docs/FruitReq.md
docs/GmFruit.md
docs/HasOnlyReadOnly.md
docs/HealthCheckResult.md
docs/IDsWrapper.md
docs/IncidentData.md
docs/List.md
docs/Mammal.md
Expand Down Expand Up @@ -75,6 +76,7 @@ docs/PropertyNameMapping.md
docs/ReadOnlyFirst.md
docs/ReadOnlyWithDefault.md
docs/Return.md
docs/SomeOpsRequiringRefIntRequest.md
docs/SpecialModelName.md
docs/StoreAPI.md
docs/Tag.md
Expand Down Expand Up @@ -121,6 +123,7 @@ model_fruit_req.go
model_gm_fruit.go
model_has_only_read_only.go
model_health_check_result.go
model_ids_wrapper.go
model_incident_data.go
model_list.go
model_mammal.go
Expand All @@ -146,6 +149,7 @@ model_property_name_mapping.go
model_read_only_first.go
model_read_only_with_default.go
model_return.go
model_some_ops_requiring_ref_int_request.go
model_tag.go
model_test_inline_freeform_additional_properties_request.go
model_user.go
Expand Down
3 changes: 3 additions & 0 deletions samples/openapi3/client/petstore/go/go-petstore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------
*AnotherFakeAPI* | [**Call123TestSpecialTags**](docs/AnotherFakeAPI.md#call123testspecialtags) | **Patch** /another-fake/dummy | To test special tags
*DefaultAPI* | [**FooGet**](docs/DefaultAPI.md#fooget) | **Get** /foo |
*DefaultAPI* | [**SomeOpsRequiringRefInt**](docs/DefaultAPI.md#someopsrequiringrefint) | **Post** /fake/wrapped-integer-ref |
*FakeAPI* | [**FakeHealthGet**](docs/FakeAPI.md#fakehealthget) | **Get** /fake/health | Health check endpoint
*FakeAPI* | [**FakeOuterBooleanSerialize**](docs/FakeAPI.md#fakeouterbooleanserialize) | **Post** /fake/outer/boolean |
*FakeAPI* | [**FakeOuterCompositeSerialize**](docs/FakeAPI.md#fakeoutercompositeserialize) | **Post** /fake/outer/composite |
Expand Down Expand Up @@ -160,6 +161,7 @@ Class | Method | HTTP request | Description
- [GmFruit](docs/GmFruit.md)
- [HasOnlyReadOnly](docs/HasOnlyReadOnly.md)
- [HealthCheckResult](docs/HealthCheckResult.md)
- [IDsWrapper](docs/IDsWrapper.md)
- [IncidentData](docs/IncidentData.md)
- [List](docs/List.md)
- [Mammal](docs/Mammal.md)
Expand All @@ -186,6 +188,7 @@ Class | Method | HTTP request | Description
- [ReadOnlyFirst](docs/ReadOnlyFirst.md)
- [ReadOnlyWithDefault](docs/ReadOnlyWithDefault.md)
- [Return](docs/Return.md)
- [SomeOpsRequiringRefIntRequest](docs/SomeOpsRequiringRefIntRequest.md)
- [SpecialModelName](docs/SpecialModelName.md)
- [Tag](docs/Tag.md)
- [TestInlineFreeformAdditionalPropertiesRequest](docs/TestInlineFreeformAdditionalPropertiesRequest.md)
Expand Down
30 changes: 30 additions & 0 deletions samples/openapi3/client/petstore/go/go-petstore/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,22 @@ paths:
summary: parameter name mapping test
tags:
- fake
/fake/wrapped-integer-ref:
post:
operationId: someOpsRequiringRefInt
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/someOpsRequiringRefInt_request'
required: true
responses:
"200":
content:
application/json:
schema:
type: object
description: success
components:
requestBodies:
UserArray:
Expand Down Expand Up @@ -2177,6 +2193,11 @@ components:
type: object
type: array
- type: object
IDsWrapper:
properties:
id:
type: integer
type: object
_foo_get_default_response:
example:
string:
Expand Down Expand Up @@ -2330,6 +2351,15 @@ components:
required:
- requiredFile
type: object
someOpsRequiringRefInt_request:
properties:
MyIDs:
items:
$ref: '#/components/schemas/IDsWrapper/properties/id'
type: array
required:
- ids
type: object
securitySchemes:
petstore_auth:
flows:
Expand Down
120 changes: 120 additions & 0 deletions samples/openapi3/client/petstore/go/go-petstore/api_default.go

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

Loading

0 comments on commit 6aa825b

Please sign in to comment.