-
Notifications
You must be signed in to change notification settings - Fork 24
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
fix: correctly determine auto-paginated field #156
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -294,30 +294,48 @@ function pagingField(messages: MessagesMap, method: MethodDescriptorProto) { | |
field.label === | ||
plugin.google.protobuf.FieldDescriptorProto.Label.LABEL_REPEATED | ||
); | ||
if (repeatedFields.length !== 1) { | ||
if (repeatedFields.length === 0) { | ||
return undefined; | ||
} | ||
if (repeatedFields.length === 1) { | ||
return repeatedFields[0]; | ||
} | ||
// According to https://aip.dev/client-libraries/4233, if there are two | ||
// or more repeated fields in the output, we must take the the first one | ||
// (in order of appearance in the file AND field number). | ||
// We believe that all proto fields have numbers, hence !. | ||
const minFieldNumber = repeatedFields.reduce( | ||
(min: number, field: plugin.google.protobuf.IFieldDescriptorProto) => { | ||
if (field.number! < min) { | ||
min = field.number!; | ||
} | ||
return min; | ||
}, | ||
repeatedFields[0].number! | ||
); | ||
if (minFieldNumber !== repeatedFields[0].number) { | ||
console.warn( | ||
`Warning: method ${method.name} has several repeated fields in the output type and violates https://aip.dev/client-libraries/4233 for auto-pagination. Disabling auto-pagination for this method.` | ||
); | ||
console.warn('Fields considered for pagination:'); | ||
console.warn( | ||
repeatedFields.map(field => `${field.name} = ${field.number}`).join('\n') | ||
); | ||
} | ||
return repeatedFields[0]; | ||
} | ||
|
||
function pagingFieldName(messages: MessagesMap, method: MethodDescriptorProto) { | ||
const repeatedFields = pagingField(messages, method); | ||
if (repeatedFields && repeatedFields.name) { | ||
return repeatedFields.name; | ||
} else { | ||
return undefined; | ||
} | ||
const field = pagingField(messages, method); | ||
return field?.name; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good to see you living your best life here |
||
} | ||
|
||
function pagingResponseType( | ||
messages: MessagesMap, | ||
method: MethodDescriptorProto | ||
) { | ||
const repeatedFields = pagingField(messages, method); | ||
if (repeatedFields && repeatedFields.typeName) { | ||
return repeatedFields.typeName; //.google.showcase.v1beta1.EchoResponse | ||
} | ||
return undefined; | ||
const field = pagingField(messages, method); | ||
return field?.typeName; //.google.showcase.v1beta1.EchoResponse | ||
} | ||
|
||
export function getHeaderParams(rule: plugin.google.api.IHttpRule): string[] { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So warnings like this always give me a moment's pause. Chances are this will be run by a robot most of the time, and a human will often never see the output. Realistically - do we think this merits an error, and human attention? Or are we ok with the warning getting ignored?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about this for a while. If the API is broken in this specific way, we cannot fix it (AIPs and annotations have no way of saying "forget it, it's not auto-paginated"). AIP actually suggests to fail in this case, but then we just cannot generate this API at all, end of story.
How about this: we'll fail by default and will proceed with a command line option?
If you're OK with this approach, I'll first implement it (in a separate PR) and then will apply here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the surface, this sounds correct. If we know an API will be generated poorly, I'd rather us not generate at all, and harass the partner team to fix the proto.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK! Failing now, also #158 to make it possible to ignore the error.