Repository: go-swagger/go-swagger
Pull request #2168 (2020-01-07 17:41:11)

feat(parameters): query & path parameters now accept x-go-name
Related to #2167 

codecov[bot]: # [Codecov](https://codecov.io/gh/go-swagger/go-swagger/pull/2168?src=pr&el=h1) Report
> Merging [#2168](https://codecov.io/gh/go-swagger/go-swagger/pull/2168?src=pr&el=desc) into [master](https://codecov.io/gh/go-swagger/go-swagger/commit/1c98855b472d8782c366459428ec2a5e8339ccf2?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/go-swagger/go-swagger/pull/2168/graphs/tree.svg?width=650&token=aQS1DepR8C&height=150&src=pr)](https://codecov.io/gh/go-swagger/go-swagger/pull/2168?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master    #2168   +/-   ##
=======================================
  Coverage   80.36%   80.36%           
=======================================
  Files          47       47           
  Lines        8267     8267           
=======================================
  Hits         6644     6644           
  Misses       1104     1104           
  Partials      519      519
```



------

[Continue to review full report at Codecov](https://codecov.io/gh/go-swagger/go-swagger/pull/2168?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/go-swagger/go-swagger/pull/2168?src=pr&el=footer). Last update [1c98855...a007c78](https://codecov.io/gh/go-swagger/go-swagger/pull/2168?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

fredbi: @maxatome did you test it with client generation, I mean does your spec produce some code that builds from both client and server? Hint: you can add this to our CI spec build here: https://github.com/go-swagger/go-swagger/blob/master/hack/codegen-fixtures.yaml (note: since you have a "bad spec", you should specify only the one that is supposed to work, with its full path). 
maxatome: @fredbi yes it works for client generation too.
Note I added unit tests to check that case as well as server one.
fredbi:    I've seen the unit test, I am talking about making sure (manually, I am not asking for more automated testing here) that the client compiles properly.
maxatome: In the `client/xxx_parameters.go` files, if we have:
- `name: foo`
- `x-go-name: bar`

We have the methods `WithBar(foo string)` and `SetBar(foo string)`.

Patch:
diff --git a/fixtures/enhancements/2167/swagger-error.yml b/fixtures/enhancements/2167/swagger-error.yml
new file mode 100644
index 00000000..762cfba0
--- /dev/null
+++ b/fixtures/enhancements/2167/swagger-error.yml
@@ -0,0 +1,25 @@
+swagger: '2.0'
+info:
+  version: 1.0.0
+  title: Example
+schemes:
+  - http
+produces:
+  - text/plain
+paths:
+  /test/{test_name}:
+    get:
+      operationId: xGoNameInParams
+      summary: Says hello
+      parameters:
+        - in: path
+          name: test_name
+          x-go-name:
+            - bad
+          required: true
+          type: string
+      responses:
+        200:
+          description: A hello world string
+          schema:
+            type: string
diff --git a/fixtures/enhancements/2167/swagger.yml b/fixtures/enhancements/2167/swagger.yml
new file mode 100644
index 00000000..6a0b1d4b
--- /dev/null
+++ b/fixtures/enhancements/2167/swagger.yml
@@ -0,0 +1,37 @@
+swagger: '2.0'
+info:
+  version: 1.0.0
+  title: Example
+schemes:
+  - http
+produces:
+  - text/plain
+paths:
+  /test/{test_name}/on/{test_region}:
+    get:
+      operationId: xGoNameInParams
+      summary: Says hello
+      parameters:
+        - in: path
+          name: test_name
+          x-go-name: MyPathName
+          required: true
+          type: string
+        - in: path
+          name: test_region
+          required: true
+          type: string
+        - in: query
+          name: test_count
+          x-go-name: MyQueryCount
+          required: false
+          type: integer
+        - in: query
+          name: test_limit
+          required: false
+          type: integer
+      responses:
+        200:
+          description: A hello world string
+          schema:
+            type: string
diff --git a/generator/operation.go b/generator/operation.go
index 04e6ed1b..3078ab64 100644
--- a/generator/operation.go
+++ b/generator/operation.go
@@ -734,7 +734,13 @@ func (b *codeGenOpBuilder) MakeParameter(receiver string, resolver *typeResolver
 
 	var child *GenItems
 	id := swag.ToGoName(param.Name)
-	if len(idMapping) > 0 {
+	if goName, ok := param.Extensions["x-go-name"]; ok {
+		id, ok = goName.(string)
+		if !ok {
+			return GenParameter{}, fmt.Errorf(`%s %s, parameter %q: "x-go-name" field must be a string, not a %T`,
+				b.Method, b.Path, param.Name, goName)
+		}
+	} else if len(idMapping) > 0 {
 		id = idMapping[param.In][param.Name]
 	}
 


Test patch:
diff --git a/generator/parameter_test.go b/generator/parameter_test.go
index fc06d9e1..2ef27487 100644
--- a/generator/parameter_test.go
+++ b/generator/parameter_test.go
@@ -4407,3 +4407,29 @@ func TestGenClientParameter_1937(t *testing.T) {
 	}
 	assertParams(t, fixtureConfig, filepath.Join("..", "fixtures", "bugs", "1937", "fixture-1937.yaml"), true, false)
 }
+
+func TestGenParameter_Issue2167(t *testing.T) {
+	assert := assert.New(t)
+
+	gen, err := opBuilder("xGoNameInParams", "../fixtures/enhancements/2167/swagger.yml")
+	if assert.NoError(err) {
+		op, err := gen.MakeOperation()
+		if assert.NoError(err) {
+			buf := bytes.NewBuffer(nil)
+			opts := opts()
+			err := templates.MustGet("clientParameter").Execute(buf, op)
+			if assert.NoError(err) {
+				ff, err := opts.LanguageOpts.FormatContent("x_go_name_in_params_parameters.go", buf.Bytes())
+				if assert.NoError(err) {
+					res := string(ff)
+					assertRegexpInCode(t, `(?m)^\tMyPathName\s+string$`, res)
+					assertRegexpInCode(t, `(?m)^\tTestRegion\s+string$`, res)
+					assertRegexpInCode(t, `(?m)^\tMyQueryCount\s+\*int64$`, res)
+					assertRegexpInCode(t, `(?m)^\tTestLimit\s+\*int64$`, res)
+				} else {
+					fmt.Println(buf.String())
+				}
+			}
+		}
+	}
+}
diff --git a/generator/urlbuilder_test.go b/generator/urlbuilder_test.go
index 75a7a7bc..1ba39c4a 100644
--- a/generator/urlbuilder_test.go
+++ b/generator/urlbuilder_test.go
@@ -276,3 +276,41 @@ func testArrayQueryParams(t testing.TB, filePath, basePath string) {
 		}
 	}
 }
+
+func TestURLBuilder_Issue2167(t *testing.T) {
+	assert := assert.New(t)
+
+	gen, err := opBuilder("xGoNameInParams", "../fixtures/enhancements/2167/swagger.yml")
+	if assert.NoError(err) {
+		op, err := gen.MakeOperation()
+		if assert.NoError(err) {
+			buf := bytes.NewBuffer(nil)
+			opts := opts()
+			err := templates.MustGet("serverUrlbuilder").Execute(buf, op)
+			if assert.NoError(err) {
+				ff, err := opts.LanguageOpts.FormatContent("get_test_test_name_urlbuilder.go", buf.Bytes())
+				if assert.NoError(err) {
+					res := string(ff)
+					assertRegexpInCode(t, `(?m)^\tMyPathName\s+string$`, res)
+					assertRegexpInCode(t, `(?m)^\tTestRegion\s+string$`, res)
+					assertRegexpInCode(t, `(?m)^\tMyQueryCount\s+\*int64$`, res)
+					assertRegexpInCode(t, `(?m)^\tTestLimit\s+\*int64$`, res)
+				} else {
+					fmt.Println(buf.String())
+				}
+			}
+		}
+	}
+}
+
+func TestURLBuilder_Issue2167_Error(t *testing.T) {
+	assert := assert.New(t)
+
+	gen, err := opBuilder("xGoNameInParams", "../fixtures/enhancements/2167/swagger-error.yml")
+	if assert.NoError(err) {
+		_, err := gen.MakeOperation()
+		if assert.Error(err) {
+			assert.Equal(`GET /test/{test_name}, parameter "test_name": "x-go-name" field must be a string, not a []interface {}`, err.Error())
+		}
+	}
+}