Split Response Types by Encoding/Status#231
Open
natsukagami wants to merge 5 commits into
Open
Conversation
|
Is this change backwards compatible with existing generated code, meaning, if someone re-generates their code, will their existing code break? It seems to me like it should not, unless they've copy/pasted anonymous types into their code. I've started a v2 branch for changes which break people's code. We might have to put it into v2 if that's the case. |
Contributor
Author
|
It would break code that has responses defined as references to a schema. For example type Pet struct { /*...*/ }
type AResponse {
JSON200 *Pet
}
// becomes
type AResponseJSON200 Pet
type AResponse {
JSON200 *AResponseJSON200
}would now break if the user passes |
Contributor
|
@deepmap-marcinr would you mind having a second look? With the latest version of this PR, I think @natsukagami found a good compromise between backward compatibility and usability/functionality. Thanks! |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What is the problem?
Currently, the response struct in
client-with-responseshave all their possible output structures embedded into a single struct. For example, fromcomponents.gen.go:Notice that there are two possible outputs,
JSON200andJSONDefault, which are anonymous structs.The anonymous structs make it difficult to perform certain
tasks with it.
Redundant copy-pasting of definition
One such use is passing the struct into a function.
The
handleResponsefunction has to either have the entire anonymous struct declaration copied to its signature...or declare another struct with identical layout, either way takes a redundant copy-paste.
Another example is creating a struct of the same type.
The generated code for the client looks like this
(see diff):
we can see a similar dedundant copy-paste pattern.
Difficult to enforce server-client type equality
Suppose we wanted to enforce the server to return the same type as the client definition.
This is commonly a source of problems, where the server is returning a different structure than the client expects.
This is hard to perform with a anonymous struct, because creating such structs are more difficult and error-prone.
If we wanted to write custom logic for responses (for example, validation of responses, to be used in testing), the embedded structs don't help.
Our approach
In short, we separate all different responses into
independent type declarations.
For example, the above
EnsureEverythingIsReferencedResponsewill now be generated asNote that the response now go through another struct-wrapping.
This might be a breaking change, for example, if the response is just a reference to a schema:
In such case, we provide an "escape-hatch" to the old style by enabling the
--alias-typescommand line option (which is defined inoapi-codegento aggressively alias types). If the option is specified, all new type declarations will instead become type aliases. This means no wrapping whatsoever.Status of the PR
The implementation is complete.
This change is