Skip to content

Commit 6f9e7bd

Browse files
Warboss-rusilya.bogdanovilya.bogdanov
authored
Strict server generation (oapi-codegen#499)
* Added strict server generation that does automatic marshaling of request and response bodies, meaning less manual code and forced schema compliance * Support any content type by using io.Reader * removed generating marshalling code from client for content types other than JSON * Multipart and formdata are now passed as forms without any binding * run make generate to fix the build * added sorting of request bodies to reduce number of random changes in generated code, fixed content type in RequestBody's comment * Implemented basic formdata binder (not yet integrated into strict server) * Added tests for strict generation, support for form marshalling, client improvements * Fixed incorrect referencing of request bodies, added sorting of response definitions * Added sorting of content types in responses * Support multipart responses using callback function, added header example * Added sorting of headers in response objects * Added proper testing to strict servers * Fix after master merge * Reuse responses defined in components section, moved strict test to tests * When multiple responses ref to a single reusable response, only the first one will use an alias, all others will generate new structs * Update generated code after merge * Some documentation for strict server * Support for AdditionalProperties when binding forms Co-authored-by: ilya.bogdanov <ilya.bogdanov@ispringsolutions.com> Co-authored-by: ilya.bogdanov <ilya.bogdanov@ispring.com>
1 parent 7da811e commit 6f9e7bd

56 files changed

Lines changed: 7863 additions & 62 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,47 @@ Alternatively, [Gorilla](https://github.com/gorilla/mux) is also 100% compatible
260260

261261
</summary></details>
262262

263+
#### Strict server generation
264+
265+
oapi-codegen also supports generating RPC inspired strict server, that will parse request bodies and encode responses.
266+
The main points of this code is to automate some parsing, abstract user code from server specific code,
267+
and also to force user code to comply with the schema.
268+
It supports binding of `application/json` and `application/x-www-form-urlencoded` to a struct, for `multipart` requests
269+
it generates a `multipart.Reader`, which can be used to either manually iterating over parts or using `runtime.BindMultipart`
270+
function to bind the form to a struct. All other content types are represented by a `io.Reader` interface.
271+
272+
To form a response simply return one of the generated structs with corresponding status code and content type. For example,
273+
to return a status code 200 JSON response for a AddPet use the `AddPet200JSONResponse` struct which will set the correct
274+
Content-Type header, status code and will marshal the response data. You can also return an `error` interface, that will be
275+
cause an `Internal Server Error` response. If you return a response that is not supported by this method, you will get an error.
276+
Unfortunately go does not support union types outside generic code, so we can't type check in compile time.
277+
278+
Short example:
279+
```go
280+
type PetStoreImpl struct {}
281+
func (*PetStoreImpl) GetPets(ctx context.Context, request GetPetsRequestObject) interface{} {
282+
var result []Pet
283+
// Implement me
284+
return GetPets200JSONResponse(result)
285+
}
286+
```
287+
For a complete example see `/examples/petstore-expanded/strict`.
288+
289+
Code is generation with a configuration flag `genrate: strict-server: true` along with any other server (echo, chi, gin and gorilla are supported).
290+
The generated strict wrapper can then be used as an implementation for `ServerInterface`. Setup example:
291+
```go
292+
func SetupHandler() {
293+
var myApi PetStoreImpl
294+
myStrictApiHandler := api.NewStrictHandler(myApi, nil)
295+
e := echo.New()
296+
petstore.RegisterHandlers(e, &myStrictApiHandler)
297+
}
298+
```
299+
300+
Strict server also has its own middlewares. It can access to both request and response structs,
301+
as well as raw request\response data. It can be used for logging the parsed request\response objects, transforming go errors into response structs,
302+
authorization, etc. Note that middlewares are server-specific.
303+
263304
#### Additional Properties in type definitions
264305

265306
[OpenAPI Schemas](https://swagger.io/specification/#schemaObject) implicitly

cmd/oapi-codegen/oapi-codegen.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ func newConfigFromOldConfig(c oldConfiguration) configuration {
323323
opts.Generate.GinServer = true
324324
case "gorilla":
325325
opts.Generate.GorillaServer = true
326+
case "strict-server":
327+
opts.Generate.Strict = true
326328
case "types":
327329
opts.Generate.Models = true
328330
case "spec":

examples/authenticated-api/echo/api/api.gen.go

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/petstore-expanded/chi/api/petstore.gen.go

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/petstore-expanded/echo/api/models/models.gen.go

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/petstore-expanded/gin/api/petstore-types.gen.go

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/petstore-expanded/gorilla/api/petstore.gen.go

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/petstore-expanded/petstore-client.gen.go

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)