Skip to content
Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ How the specification maps onto Go:
`x-go-name` to choose a better one. A component that is only a `$ref`, only
`null` or the empty schema declares no type; references to it use what it
stands for.
A parameter's `x-go-name` names it in Go, as its field, argument or variable,
while requests keep its name: `q` with `"x-go-name": "QueryID"` is the field
`QueryID`, still sent as `?q=`.

## Usage

Expand Down
3 changes: 3 additions & 0 deletions README/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,6 @@ How the specification maps onto Go:
`x-go-name` to choose a better one. A component that is only a `$ref`, only
`null` or the empty schema declares no type; references to it use what it
stands for.
A parameter's `x-go-name` names it in Go, as its field, argument or variable,
while requests keep its name: `q` with `"x-go-name": "QueryID"` is the field
`QueryID`, still sent as `?q=`.
3 changes: 2 additions & 1 deletion docs/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ strip the fixed text itself, answering 404 where it is missing.
`x-go-name` and `x-enum-varnames` / `x-enumNames` work as in
[oapi-codegen](https://github.com/oapi-codegen/oapi-codegen/blob/main/docs/extensions.md),
a property's own extensions beside a `$ref` winning over the referenced
schema's. These would fit too, each as oapi-codegen has it, once a spec needs
schema's; `x-go-name` names parameters too. These would fit too, each as
oapi-codegen has it, once a spec needs
it:

- **`x-go-type` / `x-go-type-import`:** a field's or a type's Go type, given,
Expand Down
9 changes: 6 additions & 3 deletions ir/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,17 @@ func fromParam(p *openapi.Parameter, apiTitle string) (Param, error) {
return param, fmt.Errorf("schema is required")
}

param.GoName = strcase.ToGoCamel(p.Name)
// an x-go-name, as on schemas, names the parameter in Go while the request keeps its name
name := cmp.Or(extGoName(p.Extensions), p.Name)

param.GoName = strcase.ToGoCamel(name)
if p.In == openapi.ParameterLocationPath {
param.GoName = pathParamName(p.Name)
param.GoName = pathParamName(name)
}

param.AllowSlashes = p.In == openapi.ParameterLocationPath && allowsSlashes(p)

param.FieldName = goPascal(p.Name)
param.FieldName = goPascal(name)

schema := paramSchema(p.Schema)
switch d := deref(schema); {
Expand Down
13 changes: 8 additions & 5 deletions ir/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,19 +514,22 @@ func getDescription(s *openapi.Schema, name string) string {

// goNameOverride returns the schema's x-go-name extension, or "" if unset.
// See https://github.com/oapi-codegen/oapi-codegen/blob/main/docs/extensions.md#x-go-name.
func goNameOverride(s *openapi.Schema) string {
if len(s.Extensions) == 0 {
func goNameOverride(s *openapi.Schema) string { return extGoName(s.Extensions) }

// extGoName returns the x-go-name in the extensions ext, or "" if unset.
func extGoName(ext openapi.Extensions) string {
if len(ext) == 0 {
return ""
}

var ext struct {
var v struct {
GoName string `json:"x-go-name"`
}
if err := json.Unmarshal(s.Extensions, &ext); err != nil {
if err := json.Unmarshal(ext, &v); err != nil {
return ""
}

return ext.GoName
return v.GoName
}

// enumNameOverrides returns the schema's x-enum-varnames or x-enumNames
Expand Down
8 changes: 6 additions & 2 deletions testdata/golden/api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
{
"name": "q",
"in": "query",
"schema": { "type": "string" }
"description": "An x-go-name names the parameter in Go while the request keeps its name.",
"schema": { "type": "string" },
"x-go-name": "ReferenceCode"
}
],
"responses": {
Expand All @@ -41,10 +43,12 @@
{
"name": "id",
"in": "path",
"description": "An x-go-name names a path parameter in Go too.",
"required": true,
"schema": {
"type": "string"
}
},
"x-go-name": "PageID"
}
],
"responses": {
Expand Down
6 changes: 3 additions & 3 deletions testdata/golden/pkg/golden/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Usage:
* import './api.js'; // or <script src="api.js"></script>
* const result = await window.API.getStatement();
* const result = await window.API.getPage(id);
* const result = await window.API.getPage(pageID);
* const result = await window.API.search();
* const result = await window.API.getTrial(id);
* const result = await window.API.updateUser(id);
Expand Down Expand Up @@ -116,14 +116,14 @@ window.API = {
// GET /statements
getStatement: (params = {}) => {
const q = new URLSearchParams();
if (params.q != null) q.set("q", String(params.q));
if (params.referenceCode != null) q.set("q", String(params.referenceCode));
return apiFetch(`/statements?${q}`, {
operationId: "GetStatement",
});
},
// GET /pages/{id}
// Retrieve a page
getPage: (id) => apiFetch(`/pages/${id}`, {
getPage: (pageID) => apiFetch(`/pages/${pageID}`, {
operationId: "GetPage",
}),
// POST /search
Expand Down
8 changes: 4 additions & 4 deletions testdata/golden/pkg/golden/client.gen.go

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

6 changes: 3 additions & 3 deletions testdata/golden/pkg/golden/client.gen_test.go

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

20 changes: 11 additions & 9 deletions testdata/golden/pkg/golden/ir.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
],
"queryParams": [
{
"varName": "params.Q",
"goName": "q",
"fieldName": "Q",
"varName": "params.ReferenceCode",
"goName": "referenceCode",
"fieldName": "ReferenceCode",
"jsonName": "q",
"type": "string",
"parseExpr": "%s",
"parseErrFree": true
"parseErrFree": true,
"description": "An x-go-name names the parameter in Go while the request keeps its name."
}
],
"hasParams": true,
Expand Down Expand Up @@ -55,18 +56,19 @@
"pathTemplate": "/pages/{id}",
"joinPathArgs": [
"\"pages\"",
"escapePathSegment(id)"
"escapePathSegment(pageID)"
],
"pathParams": [
{
"varName": "id",
"goName": "id",
"fieldName": "ID",
"varName": "pageID",
"goName": "pageID",
"fieldName": "PageID",
"jsonName": "id",
"type": "string",
"required": true,
"parseExpr": "%s",
"parseErrFree": true
"parseErrFree": true,
"description": "An x-go-name names a path parameter in Go too."
}
],
"hasParams": true,
Expand Down
8 changes: 4 additions & 4 deletions testdata/golden/pkg/golden/server.gen.go

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

3 changes: 2 additions & 1 deletion testdata/golden/pkg/golden/types.gen.go

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

Loading