Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
685a7af
fix go compilation error for properties of type map and array
sebastien-rosset Jan 22, 2020
31527ca
fix go compilation error for properties of type date and datetime
sebastien-rosset Jan 22, 2020
9370937
add missing shell script to bin/utils/ensure-up-to-date
sebastien-rosset Jan 22, 2020
d665d7c
add missing shell script to bin/utils/ensure-up-to-date
sebastien-rosset Jan 22, 2020
48526da
add missing shell script to bin/utils/ensure-up-to-date
sebastien-rosset Jan 22, 2020
262dc8a
fix issue with 'date' type
sebastien-rosset Jan 22, 2020
a3bb335
fix time import problem
sebastien-rosset Jan 22, 2020
735f84c
Add missing pom.xml files for golang
sebastien-rosset Jan 22, 2020
53226d3
Add missing unit test files for golang
sebastien-rosset Jan 22, 2020
be0b85c
Add missing unit test files for golang. Must use class name prefix fo…
sebastien-rosset Jan 22, 2020
295bbb7
Fix unit tests for go-experimental in OAS3
sebastien-rosset Jan 22, 2020
e2cf635
Fix unit tests for go-experimental in OAS3
sebastien-rosset Jan 22, 2020
1769234
Add code comments in codegen
sebastien-rosset Jan 22, 2020
1ed28d8
Fix compilation errors of generated go code
sebastien-rosset Jan 22, 2020
705a8c3
Fix compilation errors of generated go code
sebastien-rosset Jan 22, 2020
f115d8c
remove antihax from go-experimental, it is no longer used
sebastien-rosset Jan 22, 2020
220e0cf
Merge remote-tracking branch 'origin' into go-syntax-error
sebastien-rosset Jan 23, 2020
2ac6769
copy python testfile for ut purpose
sebastien-rosset Jan 23, 2020
d2e34ee
add error checkout in unit tests
sebastien-rosset Jan 23, 2020
0049c79
add unit tests
sebastien-rosset Jan 23, 2020
60e6709
add code comments
sebastien-rosset Jan 23, 2020
4cd66c2
move test foo.png file to correct location
sebastien-rosset Jan 23, 2020
1f0676c
run samples scripts
sebastien-rosset Jan 23, 2020
ef25e55
run samples scripts
sebastien-rosset Jan 23, 2020
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
4 changes: 3 additions & 1 deletion bin/openapi3/go-experimental-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ rm -rf $STUB_DIR

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -t modules/openapi-generator/src/main/resources/$GENERATOR -i $SPEC -g $GENERATOR -o $STUB_DIR -DpackageName=petstore $@"
ags="generate -t modules/openapi-generator/src/main/resources/$GENERATOR -i $SPEC -g $GENERATOR -o $STUB_DIR"
ags="$ags --additional-properties enumClassPrefix=true,packageName=petstore"
ags="$ags $@"

java $JAVA_OPTS -jar $executable $ags
4 changes: 3 additions & 1 deletion bin/openapi3/go-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ rm -rf $STUB_DIR

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -t modules/openapi-generator/src/main/resources/go -i $SPEC -g $GENERATOR -o $STUB_DIR --additional-properties packageName=petstore $@"
ags="generate -t modules/openapi-generator/src/main/resources/go -i $SPEC -g $GENERATOR -o $STUB_DIR"
ags="$ags --additional-properties enumClassPrefix=true,packageName=petstore"
ags="$ags $@"

java $JAVA_OPTS -jar $executable $ags
1 change: 1 addition & 0 deletions bin/utils/ensure-up-to-date
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ declare -a samples=(
"${root}/bin/csharp-netcore-petstore-all.sh"
"${root}/bin/elixir-petstore.sh"
"${root}/bin/openapi3/go-petstore.sh"
"${root}/bin/openapi3/go-experimental-petstore.sh"
"${root}/bin/go-experimental-petstore.sh"
"${root}/bin/go-petstore.sh"
"${root}/bin/go-petstore-withxml.sh"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@

@JsonIgnoreProperties({"parentModel", "interfaceModels"})
public class CodegenModel implements IJsonSchemaValidationProperties {
// The parent model name from the schemas. The parent is determined by inspecting the allOf, anyOf and
// oneOf attributes in the OAS. First codegen inspects 'allOf', then 'anyOf', then 'oneOf'.
// If there are multiple object references in the attribute ('allOf', 'anyOf', 'oneOf'), and one of the
// object is a discriminator, that object is set as the parent. If no discriminator is specified,
// codegen returns the first one in the list, i.e. there is no obvious parent in the OpenAPI specification.
// When possible, the mustache templates should use 'allParents' to handle multiple parents.
public String parent, parentSchema;
public List<String> interfaces;
// The list of parent model name from the schemas. In order of preference, the parent is obtained
// from the 'allOf' attribute, then 'anyOf', and finally 'oneOf'.
public List<String> allParents;

// References to parent and interface CodegenModels. Only set when code generator supports inheritance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
public boolean isBinary;
public boolean isFile;
public boolean isBoolean;
public boolean isDate;
public boolean isDateTime;
public boolean isDate; // full-date notation as defined by RFC 3339, section 5.6, for example, 2017-07-21
public boolean isDateTime; // the date-time notation as defined by RFC 3339, section 5.6, for example, 2017-07-21T17:32:28Z
public boolean isUuid;
public boolean isUri;
public boolean isEmail;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public void processOpts() {

@Override
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
objs = super.postProcessModels(objs);

List<Map<String, Object>> models = (List<Map<String, Object>>) objs.get("models");
for (Map<String, Object> m : models) {
Expand All @@ -83,16 +82,27 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
}

for (CodegenProperty param : model.vars) {
if (!param.isNullable) {
if (!param.isNullable || param.isMapContainer || param.isListContainer) {
continue;
}

param.dataType = "Nullable" + Character.toUpperCase(param.dataType.charAt(0))
if (param.isDateTime) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using NullableTime for the OAS "time" type instead of plain "string" type? At least specifically for go-experimental, because this would be a breaking change for "go". Golang has the time.Time.Truncate() function, which could help to perform arithmetic with dates.

// Note this could have been done by adding the following line in processOpts(),
// however, we only want to represent the DateTime object as NullableTime if
// it's marked as nullable in the spec.
// typeMapping.put("DateTime", "NullableTime");
param.dataType = "NullableTime";

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not invoke the following code in go-experimental processOpts() instead of modifying the dataType here? What is the reason for changing the type in postProcessModels()?

typeMapping.put("DateTime", "NullableTime");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed with using typeMapping to directly map the type instead of using the code above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So interestingly, adding typeMapping.put("DateTime", "NullableTime") does not produce the same output for the samples. I wonder if that was done on purpose. When the dataType is set in postProcessModels(), it does not have the same effect as when typeMapping is invoked in processOpts(). Is that done on purpose?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only want to represent the DateTime object as NullableTime if it's marked as nullable in the spec. I don't think we should use it in all cases.

@sebastien-rosset sebastien-rosset Jan 23, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, at https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/go-experimental/api.mustache#L26

type api{{operationId}}Request struct {
	ctx _context.Context
	apiService *{{classname}}Service{{#allParams}}
	{{paramName}} {{^isPathParam}}*{{/isPathParam}}{{{dataType}}}{{/allParams}}
}

With "typeMapping", this will produce NullableTime.
With the dataType changed in processOpts(), this will produce *time.Time.

I propose that we merge this PR as is, and then work on enhancing/fixing the typeMapping in a separate PR.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree this PR is ok as-is, let's keep more changes for followup PRs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only want to represent the DateTime object as NullableTime if it's marked as nullable in the spec. I don't think we should use it in all cases.

Thanks for the explanation. I have added code comments to explain.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree this PR is ok as-is, let's keep more changes for followup PRs.

Thanks. Can you approve the PR then? Approving in the text does not help much. BTW, for some reason I am unable to add you as a reviewer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. Sorry, I keep forgetting doing the official review.

} else {
param.dataType = "Nullable" + Character.toUpperCase(param.dataType.charAt(0))
+ param.dataType.substring(1);
}
}
}
}

// The superclass determines the list of required golang imports. The actual list of imports
// depends on which types are used, which is done in the code above. So super.postProcessModels
// must be invoked at the end of this method.
objs = super.postProcessModels(objs);
return objs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,8 @@ public static List<Schema> getInterfaces(ComposedSchema composed) {
}

/**
* Get the the parent model name from the schemas (allOf, anyOf, oneOf)
* Get the parent model name from the schemas (allOf, anyOf, oneOf).
* If there are multiple parents, return the first one.
*
* @param composedSchema schema (alias or direct reference)
* @param allSchemas all schemas
Expand Down Expand Up @@ -965,6 +966,14 @@ public static String getParentName(ComposedSchema composedSchema, Map<String, Sc
return null;
}

/**
* Get the list of parent model names from the schemas (allOf, anyOf, oneOf).
*
* @param composedSchema schema (alias or direct reference)
* @param allSchemas all schemas
* @param includeAncestors if true, include the indirect ancestors in the return value. If false, return the direct parents.
* @return the name of the parent model
*/
public static List<String> getAllParentsName(ComposedSchema composedSchema, Map<String, Schema> allSchemas, boolean includeAncestors) {
List<Schema> interfaces = getInterfaces(composedSchema);
List<String> names = new ArrayList<String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ Install the following dependencies:
go get github.com/stretchr/testify/assert
go get golang.org/x/oauth2
go get golang.org/x/net/context
go get github.com/antihax/optional
```

Put the package under your project folder and add the following in import:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ module {{gitHost}}/{{gitUserId}}/{{gitRepoId}}{{#isGoSubmodule}}/{{packageName}}
go 1.13

require (
github.com/antihax/optional v1.0.0
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
)
16 changes: 9 additions & 7 deletions modules/openapi-generator/src/main/resources/go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,16 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#hasParams
{{#queryParams}}
{{#required}}
{{#isCollectionFormatMulti}}
t:={{paramName}}
if reflect.TypeOf(t).Kind() == reflect.Slice {
s := reflect.ValueOf(t)
for i := 0; i < s.Len(); i++ {
localVarQueryParams.Add("{{baseName}}", parameterToString(s.Index(i), "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
{
t:={{paramName}}
if reflect.TypeOf(t).Kind() == reflect.Slice {
s := reflect.ValueOf(t)
for i := 0; i < s.Len(); i++ {
localVarQueryParams.Add("{{baseName}}", parameterToString(s.Index(i), "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
}
} else {
localVarQueryParams.Add("{{baseName}}", parameterToString(t, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
}
} else {
localVarQueryParams.Add("{{baseName}}", parameterToString(t, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
}
{{/isCollectionFormatMulti}}
{{^isCollectionFormatMulti}}
Expand Down
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,8 @@
<!-- test non-java projects -->
<module>samples/client/petstore/go</module>
<module>samples/client/petstore/go-experimental</module>
<module>samples/openapi3/client/petstore/go-experimental</module>
Comment thread
sebastien-rosset marked this conversation as resolved.
<module>samples/openapi3/client/petstore/go</module>
<!-- test java-related projects -->
<!--<module>samples/client/petstore/scala-akka</module>-->
<module>samples/client/petstore/scala-httpclient</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Install the following dependencies:
go get github.com/stretchr/testify/assert
go get golang.org/x/oauth2
go get golang.org/x/net/context
go get github.com/antihax/optional
```

Put the package under your project folder and add the following in import:
Expand Down
1 change: 0 additions & 1 deletion samples/client/petstore/go-experimental/go-petstore/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ module github.com/GIT_USER_ID/GIT_REPO_ID
go 1.13

require (
github.com/antihax/optional v1.0.0
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
)
10 changes: 8 additions & 2 deletions samples/client/petstore/go-experimental/pet_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ func TestFindPetsByStatus(t *testing.T) {
}

func TestUploadFile(t *testing.T) {
file, _ := os.Open("../python/testfiles/foo.png")
file, err1 := os.Open("testfiles/foo.png")
if err1 != nil {
Comment thread
sebastien-rosset marked this conversation as resolved.
t.Fatalf("Error opening file: %v", err1)
}

_, r, err := client.PetApi.UploadFile(context.Background(), 12830).AdditionalMetadata("golang").File(file).Execute()

Expand All @@ -154,7 +157,10 @@ func TestUploadFile(t *testing.T) {

func TestUploadFileRequired(t *testing.T) {
return // remove when server supports this endpoint
file, _ := os.Open("../python/testfiles/foo.png")
file, err1 := os.Open("testfiles/foo.png")
if err1 != nil {
t.Fatalf("Error opening file: %v", err1)
}

_, r, err := client.PetApi.UploadFileWithRequiredFile(context.Background(), 12830).RequiredFile(file).AdditionalMetadata("golang").Execute()

Expand Down
14 changes: 0 additions & 14 deletions samples/client/petstore/go-experimental/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,6 @@
</arguments>
</configuration>
</execution>
<execution>
<id>go-get-optional</id>
<phase>pre-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>go</executable>
<arguments>
<argument>get</argument>
<argument>github.com/antihax/optional</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>go-test</id>
<phase>integration-test</phase>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 9 additions & 7 deletions samples/client/petstore/go/go-petstore-withXml/api_fake.go

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

16 changes: 9 additions & 7 deletions samples/client/petstore/go/go-petstore/api_fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -1216,14 +1216,16 @@ func (a *FakeApiService) TestQueryParameterCollectionFormat(ctx _context.Context
localVarQueryParams.Add("ioutil", parameterToString(ioutil, "csv"))
localVarQueryParams.Add("http", parameterToString(http, "space"))
localVarQueryParams.Add("url", parameterToString(url, "csv"))
t:=context
if reflect.TypeOf(t).Kind() == reflect.Slice {
s := reflect.ValueOf(t)
for i := 0; i < s.Len(); i++ {
localVarQueryParams.Add("context", parameterToString(s.Index(i), "multi"))
{
t:=context
if reflect.TypeOf(t).Kind() == reflect.Slice {
s := reflect.ValueOf(t)
for i := 0; i < s.Len(); i++ {
localVarQueryParams.Add("context", parameterToString(s.Index(i), "multi"))
}
} else {
localVarQueryParams.Add("context", parameterToString(t, "multi"))
}
} else {
localVarQueryParams.Add("context", parameterToString(t, "multi"))
}
// to determine the Content-Type header
localVarHTTPContentTypes := []string{}
Expand Down
10 changes: 8 additions & 2 deletions samples/client/petstore/go/pet_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ func TestFindPetsByStatus(t *testing.T) {
}

func TestUploadFile(t *testing.T) {
file, _ := os.Open("../python/testfiles/foo.png")
file, err1 := os.Open("testfiles/foo.png")
if err1 != nil {
t.Fatalf("Error opening file: %v", err1)
}

_, r, err := client.PetApi.UploadFile(context.Background(), 12830, &sw.UploadFileOpts{
AdditionalMetadata: optional.NewString("golang"),
Expand All @@ -160,7 +163,10 @@ func TestUploadFile(t *testing.T) {

func TestUploadFileRequired(t *testing.T) {
return // remove when server supports this endpoint
file, _ := os.Open("../python/testfiles/foo.png")
file, err1 := os.Open("testfiles/foo.png")
if err1 != nil {
t.Fatalf("Error opening file: %v", err1)
}

_, r, err := client.PetApi.UploadFileWithRequiredFile(context.Background(), 12830,
file,
Expand Down
Binary file added samples/client/petstore/go/testfiles/foo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading