Skip to content

Commit

Permalink
Merge pull request #396 from inaturalist/395-id-param-missing-in-some…
Browse files Browse the repository at this point in the history
…-v2-project-endpoints

Fix issues in projects endpoints (missing ID param, support array of IDs or slug, ...)
  • Loading branch information
pleary authored Dec 21, 2023
2 parents 351f41c + 279add9 commit ec257b1
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 41 deletions.
8 changes: 4 additions & 4 deletions lib/controllers/v1/projects_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ const ProjectsController = class ProjectsController {

static async show( req ) {
InaturalistAPI.setPerPage( req, { max: 100 } );
const ids = _.filter( req.params.id.split( "," ), _.identity );
const ids = _.filter( req.params.id.toString( ).split( "," ), _.identity );
if ( ids.length > req.query.per_page ) {
throw util.httpError( 422, "Too many IDs" );
}
Expand Down Expand Up @@ -290,7 +290,7 @@ const ProjectsController = class ProjectsController {
null,
"prup.owner_id = pu.id AND prup.owner_type = 'ProjectUser' AND prup.name = 'updates'"
)
.where( "p.id IN (?)", ids )
.where( "p.id IN ?", ids )
.where( "pu.user_id = ?", req.userSession.user_id );
const { rows } = await pgClient.query( query.toString( ) );
_.each( rows, row => {
Expand Down Expand Up @@ -344,13 +344,13 @@ const ProjectsController = class ProjectsController {

static async posts( req ) {
const { page, perPage } = InaturalistAPI.paginationData( req, { default: 10, max: 30 } );
const ids = _.filter( req.params.id.split( "," ), _.identity );
const ids = _.filter( req.params.id.toString( ).split( "," ), _.identity );
let numericIDs = _.filter( ids, id => Number( id ) );
if ( _.isEmpty( numericIDs ) ) { numericIDs = [-1]; }
const query = squel.select( ).field( "posts.*, count(*) OVER() AS total_count" )
.from( "posts" )
.join( "projects", null, "posts.parent_id = projects.id AND parent_type='Project'" )
.where( "projects.id IN (?) OR projects.slug IN (?)", numericIDs, ids )
.where( "projects.id IN ? OR projects.slug IN ?", numericIDs, ids )
.where( "posts.published_at IS NOT NULL" )
.order( "posts.published_at", false )
.limit( perPage )
Expand Down
18 changes: 0 additions & 18 deletions lib/controllers/v2/projects_controller.js

This file was deleted.

6 changes: 3 additions & 3 deletions lib/views/swagger_v1.yml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -1562,10 +1562,10 @@ paths:
get:
summary: Membership of current user
description: |
Given a project ID, return the details of the authenticated user's
membership in that project
Given an ID, or an array of IDs in comma-delimited format, return the details of the
authenticated user's membership in these projects
parameters:
- $ref: "#/parameters/path_id"
- $ref: "#/parameters/path_multi_id"
tags:
- Projects
security:
Expand Down
2 changes: 1 addition & 1 deletion openapi/paths/v2/projects.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const _ = require( "lodash" );
const projectsSearchSchema = require( "../../schema/request/projects_search" );
const transform = require( "../../joi_to_openapi_parameter" );
const ProjectsController = require( "../../../lib/controllers/v2/projects_controller" );
const ProjectsController = require( "../../../lib/controllers/v1/projects_controller" );

module.exports = sendWrapper => {
async function GET( req, res ) {
Expand Down
2 changes: 1 addition & 1 deletion openapi/paths/v2/projects/{id}.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Joi = require( "joi" );
const transform = require( "../../../joi_to_openapi_parameter" );
const projectsController = require( "../../../../lib/controllers/v2/projects_controller" );
const projectsController = require( "../../../../lib/controllers/v1/projects_controller" );

module.exports = sendWrapper => {
async function GET( req, res ) {
Expand Down
19 changes: 14 additions & 5 deletions openapi/paths/v2/projects/{id}/members.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
const _ = require( "lodash" );
const Joi = require( "joi" );
const transform = require( "../../../../joi_to_openapi_parameter" );
const ProjectsController = require( "../../../../../lib/controllers/v2/projects_controller" );
const observationsSearchSchema = require( "../../../../schema/request/projects_members" );
const ProjectsController = require( "../../../../../lib/controllers/v1/projects_controller" );
const projectsMembersSchema = require( "../../../../schema/request/projects_members" );

module.exports = sendWrapper => {
async function GET( req, res ) {
const results = await ProjectsController.members( req );
sendWrapper( req, res, null, results );
}

const parameters = _.map(
observationsSearchSchema.$_terms.keys,
child => transform( child.schema.label( child.key ) )
const parameters = [
transform(
Joi.number( ).integer( )
.label( "id" )
.meta( { in: "path" } )
.required( )
.description( "A single ID" )
)
].concat(
_.map( projectsMembersSchema.$_terms.keys, child => (
transform( child.schema.label( child.key ) )
) )
);
parameters.push(
transform( Joi.string( ).label( "X-HTTP-Method-Override" ).meta( { in: "header" } ) )
Expand Down
7 changes: 4 additions & 3 deletions openapi/paths/v2/projects/{id}/membership.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Joi = require( "joi" );
const transform = require( "../../../../joi_to_openapi_parameter" );
const projectsController = require( "../../../../../lib/controllers/v2/projects_controller" );
const projectsController = require( "../../../../../lib/controllers/v1/projects_controller" );

module.exports = sendWrapper => {
async function GET( req, res ) {
Expand All @@ -16,11 +16,12 @@ module.exports = sendWrapper => {
}],
parameters: [
transform(
Joi.number( ).integer( )
Joi.array( )
.items( Joi.number( ).integer( ) )
.label( "id" )
.meta( { in: "path" } )
.required( )
.description( "A single project ID" )
.description( "A single ID or a comma-separated list of them" )
),
transform( Joi.string( ).label( "fields" ).meta( { in: "query" } ) ),
transform( Joi.string( ).label( "X-HTTP-Method-Override" ).meta( { in: "header" } ) )
Expand Down
18 changes: 14 additions & 4 deletions openapi/paths/v2/projects/{id}/posts.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const _ = require( "lodash" );
const Joi = require( "joi" );
const transform = require( "../../../../joi_to_openapi_parameter" );
const ProjectsController = require( "../../../../../lib/controllers/v2/projects_controller" );
const ProjectsController = require( "../../../../../lib/controllers/v1/projects_controller" );
const projectsPostsSchema = require( "../../../../schema/request/projects_posts" );

module.exports = sendWrapper => {
Expand All @@ -10,9 +10,19 @@ module.exports = sendWrapper => {
sendWrapper( req, res, null, results );
}

const parameters = _.map(
projectsPostsSchema.$_terms.keys,
child => transform( child.schema.label( child.key ) )
const parameters = [
transform(
Joi.array( )
.items( Joi.number( ).integer( ) )
.label( "id" )
.meta( { in: "path" } )
.required( )
.description( "A single ID or a comma-separated list of them" )
)
].concat(
_.map( projectsPostsSchema.$_terms.keys, child => (
transform( child.schema.label( child.key ) )
) )
);
parameters.push(
transform( Joi.string( ).label( "X-HTTP-Method-Override" ).meta( { in: "header" } ) )
Expand Down
4 changes: 2 additions & 2 deletions openapi/schema/request/projects_members.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module.exports = Joi.object( ).keys( {
page: Joi.number( ).integer( ),
per_page: Joi.number( ).integer( ),
fields: Joi.any( ),
order_by: Joi.array( ).items( Joi.string( ).valid(
order_by: Joi.string( ).valid(
"observations_count",
"login"
) ).default( "observations_count" )
).default( "observations_count" )
} ).unknown( false );

0 comments on commit ec257b1

Please sign in to comment.