Skip to content

Commit

Permalink
Fix client call by checking if resource show route has params
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmingoia committed Nov 5, 2024
1 parent 48d6b0b commit 1b95fcb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
4 changes: 2 additions & 2 deletions tools/generate-client-tpl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const inflect = require("./inflect");

module.exports = (name, resourceSchema, pathIdField) => {
module.exports = (name, resourceSchema, pathIdField, hasIncludeParam) => {
const namePlural = inflect.pluralize(name);
const nameCamel = inflect.camelize(name);
const nameCamelPlural = inflect.camelize(namePlural);
Expand Down Expand Up @@ -79,7 +79,7 @@ func (c *Client) Create${nameCamel}(d *${nameCamel}) (*${nameCamel}, error) {
}
func (c *Client) Get${nameCamel}(id string) (*${nameCamel}, error) {
req, err := rootlygo.NewGet${nameCamel}Request(c.Rootly.Server, id)
req, err := rootlygo.NewGet${nameCamel}Request(c.Rootly.Server, id${hasIncludeParam ? ', nil' : ''})
if err != nil {
return nil, errors.Errorf("Error building request: %s", err.Error())
}
Expand Down
21 changes: 19 additions & 2 deletions tools/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function generateReadOnlyClient(name) {
collectionSchema.parameters &&
collectionSchema.parameters[0] &&
collectionSchema.parameters[0].name;
const code = clientReadOnlyTpl(name, resourceSchema(name), pathIdField);
const code = clientReadOnlyTpl(name, resourceSchema(name), pathIdField, hasIncludeParam(name));
fs.writeFileSync(
path.resolve(__dirname, "..", "client", `${inflect.pluralize(name)}.go`),
code
Expand All @@ -179,7 +179,7 @@ function generateClient(name) {
collectionSchema.parameters &&
collectionSchema.parameters[0] &&
collectionSchema.parameters[0].name;
const code = clientTpl(name, resourceSchema(name), pathIdField);
const code = clientTpl(name, resourceSchema(name), pathIdField, hasIncludeParam(name));
fs.writeFileSync(
path.resolve(__dirname, "..", "client", `${inflect.pluralize(name)}.go`),
code
Expand Down Expand Up @@ -352,3 +352,20 @@ function collectionPathSchema(name) {
})
.map((url) => swagger.paths[url])[0];
}

function hasIncludeParam(name) {
const paramsSchema = Object.keys(swagger.paths)
.filter((url) => {
const get = swagger.paths[url].get;
return (
get &&
get.operationId.replace(/ /g, "") ===
`get${inflect.singularize(inflect.camelize(name))}`
);
})
.map((url) => swagger.paths[url])[0]?.get;

return paramsSchema &&
paramsSchema.parameters &&
paramsSchema.parameters.some((param) => param.name === "include");
}

0 comments on commit 1b95fcb

Please sign in to comment.