-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
snake_case vs camelCase URL query params #10804
Comments
I think camelCase is used more around js/node devs, so thats a pro for that. |
Maybe it makes sense when upstream api uses snake_case? |
With a badge, the underlying API is something the user doesn't necessarily interact with so I don't think there's any benefit to vertical consistency with the upstream API here. I'd say horizontal consistency across badges gives more of a benefit for users in this case. |
I agree, I don't think you take a look e.g. at the Matrix API before creating a badge for your README. |
another pro for camelCase is that it makes url more compact, this makes it a bit easier to read as part of a url thats mostly not very short. It seems to me this discussion tends towards camelCase.
once the follow up questions are created i think we can close this issue. |
Given the number of these we have, I think I would want to avoid doing it with redirects. Given that constraint, lets sketch out a couple of quite different approaches that I have partially thought through. Pattern 1: The ScalpelLets say we're starting off with this badge class: const queryParamSchema = Joi.object({
foo_bar: optionalUrl,
}).required()
class FakeService {
static route = {
base: 'service/noun',
pattern: ':variable',
queryParamSchema,
}
static openApi = {
'/service/noun/{variable}': {
get: {
summary: 'Service Noun',
parameters: [
pathParam({
name: 'variable',
example: 'example',
}),
queryParam({
name: 'foo_bar',
example: 'example',
}),
],
},
},
}
async handle({ project }, params) {
const fooBar = params.foo_bar
return { message: fooBar }
}
} The problem we've got is we want to change OK, so first lets define a global helper function we can use, given this will need to happen in lots of places. As a starting point, lets define it like this: function getQueryParam(queryParams, ...keys) {
for (const key of keys) {
if (queryParams[key] !== undefined) {
return queryParams[key]
}
}
return undefined
} This will allow us to pass an object and one or more keys we're going to look for in that object in order. Now lets use that to update our class. // modify the query param schema so it will accept both foo_bar and fooBar
const queryParamSchema = Joi.object({
foo_bar: optionalUrl,
fooBar: Joi.string(),
}).required()
class FakeService {
static route = {
base: 'service/noun',
pattern: ':variable',
queryParamSchema,
}
static openApi = {
'/service/noun/{variable}': {
get: {
summary: 'Service Noun',
parameters: [
pathParam({
name: 'variable',
example: 'example',
}),
// document only fooBar in the frontend
queryParam({
name: 'fooBar',
example: 'example',
}),
],
},
},
}
async handle({ project }, queryParams) {
// use our helper function to look for queryParams.fooBar first, and then fall back to queryParams.foo_bar second
const fooBar = getQueryParam(queryParams, 'fooBar', 'foo_bar')
return { message: fooBar }
}
} This would allow the user to use either This would be fine for optional query params. We'll have to be a bit more clever on the schema so that either one variant or the other is required, but not both. One nice thing about this pattern is it gives us the ability to gradually migrate services case-by-case. The bad thing is we have this compatibility code in lots of different places. Pattern 2: The SledgehammerIf the bad thing about pattern 1 is we end up with this compatibility code sprinkled throughout the codebase, how about we centralise it? Every badge request is processed by shields/core/base-service/base.js Lines 414 to 440 in 152b8e9
What if we centralise that logic. Before we validate the If we do that, any This has slightly different tradeoffs. The good things about this are:
however there are some drawbacks:
So I guess the next questions are:
|
Over the last 10+ years we have accumulated a lot of code written by a lot of different people.
In general, I think we've done a reasonable job of enforcing relatively consistent code styles and naming conventions, via documentation and/or lint rules, but there are obviously some exceptions.
One significant "blind spot" where we have a lot of variance is whether we use
snake_case
orcamelCase
URL query params.There's a bunch of places where we are using camelCase. For example:
shields/services/maven-metadata/maven-metadata.service.js
Lines 7 to 11 in 41d072e
shields/services/pypi/pypi-base.js
Lines 23 to 25 in 41d072e
shields/services/nexus/nexus.service.js
Lines 51 to 57 in 41d072e
and a bunch of places where we are using snake_case. For example:
shields/services/vpm/vpm-version.service.js
Lines 6 to 9 in 41d072e
shields/services/gitea/gitea-last-commit.service.js
Lines 26 to 32 in 41d072e
shields/services/website-status.js
Lines 16 to 21 in 41d072e
I think the other day we acquired the first case where we have one of each next to each other on the same service 🤦
shields/services/matrix/matrix.service.js
Lines 12 to 17 in 41d072e
I have not counted exactly, but roughly speaking we have about half and half.
The standard params that apply to all badges are all camelCase:
labelColor
,logoColor
,logoSize
I think we've at least managed to avoid having any params that use
snake-case
🤞 but maybe somewhere in the codebase there is an example 😬There are a couple of ways we can change the names of query params without making a breaking change. One way we can do it is with redirects. Another would be to write the
queryParamSchema
s to accept both formats but only document one for the services where we want to fix this. Having got to the stage where we have hundreds of service integrations, this is going to be quite difficult to unpick, and I wouldn't want to do it all at once. It would be nice to gradually work towards fixing this though.I suggest we:
Given:
labelColor
,logoColor
,logoSize
I'm gong to suggest we standardise on camelCase, but I also feel like there is a reason why we used snake_case in a lot of cases, and I can't remember what it is.
The text was updated successfully, but these errors were encountered: