Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c2a443b
Added strict server generation that does automatic marshaling of requ…
Nov 7, 2021
ca52c02
Support any content type by using io.Reader
Nov 27, 2021
b26bf81
removed generating marshalling code from client for content types oth…
Dec 13, 2021
063323b
Multipart and formdata are now passed as forms without any binding
Dec 14, 2021
97b84b8
run make generate to fix the build
Dec 14, 2021
e663353
added sorting of request bodies to reduce number of random changes in…
Dec 15, 2021
86a4606
Implemented basic formdata binder (not yet integrated into strict ser…
Jan 27, 2022
44df273
Merge remote-tracking branch 'origin/master' into strict
Jan 27, 2022
d3434b9
Added tests for strict generation, support for form marshalling, clie…
Apr 20, 2022
f7fa96d
Merge remote-tracking branch 'origin/master' into strict
Apr 20, 2022
7df598b
Fixed incorrect referencing of request bodies, added sorting of respo…
Apr 20, 2022
bed3a46
Added sorting of content types in responses
Apr 20, 2022
5c14954
Support multipart responses using callback function, added header exa…
Apr 24, 2022
15d7246
Added sorting of headers in response objects
May 2, 2022
e7abd03
Added proper testing to strict servers
May 2, 2022
8b51d47
Merge remote-tracking branch 'origin/master' into strict
May 22, 2022
240fa5c
Fix after master merge
May 22, 2022
57e8fa0
Reuse responses defined in components section, moved strict test to t…
May 22, 2022
d814010
When multiple responses ref to a single reusable response, only the f…
May 22, 2022
5048eac
Merge remote-tracking branch 'origin/master' into strict
Jun 1, 2022
c8d951f
Update generated code after merge
Jun 1, 2022
1d77d2f
Merge remote-tracking branch 'origin/master' into strict
Jun 13, 2022
825c4db
Some documentation for strict server
Jun 19, 2022
1a7338b
Support for AdditionalProperties when binding forms
Jun 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,47 @@ Alternatively, [Gorilla](https://github.com/gorilla/mux) is also 100% compatible

</summary></details>

#### Strict server generation

oapi-codegen also supports generating RPC inspired strict server, that will parse request bodies and encode responses.
The main points of this code is to automate some parsing, abstract user code from server specific code,
and also to force user code to comply with the schema.
It supports binding of `application/json` and `application/x-www-form-urlencoded` to a struct, for `multipart` requests
it generates a `multipart.Reader`, which can be used to either manually iterating over parts or using `runtime.BindMultipart`
function to bind the form to a struct. All other content types are represented by a `io.Reader` interface.

To form a response simply return one of the generated structs with corresponding status code and content type. For example,
to return a status code 200 JSON response for a AddPet use the `AddPet200JSONResponse` struct which will set the correct
Content-Type header, status code and will marshal the response data. You can also return an `error` interface, that will be
cause an `Internal Server Error` response. If you return a response that is not supported by this method, you will get an error.
Unfortunately go does not support union types outside generic code, so we can't type check in compile time.

Short example:
```go
type PetStoreImpl struct {}
func (*PetStoreImpl) GetPets(ctx context.Context, request GetPetsRequestObject) interface{} {
var result []Pet
// Implement me
return GetPets200JSONResponse(result)
}
```
For a complete example see `/examples/petstore-expanded/strict`.

Code is generation with a configuration flag `genrate: strict-server: true` along with any other server (echo, chi, gin and gorilla are supported).
The generated strict wrapper can then be used as an implementation for `ServerInterface`. Setup example:
```go
func SetupHandler() {
var myApi PetStoreImpl
myStrictApiHandler := api.NewStrictHandler(myApi, nil)
e := echo.New()
petstore.RegisterHandlers(e, &myStrictApiHandler)
}
```

Strict server also has its own middlewares. It can access to both request and response structs,
as well as raw request\response data. It can be used for logging the parsed request\response objects, transforming go errors into response structs,
authorization, etc. Note that middlewares are server-specific.

#### Additional Properties in type definitions

[OpenAPI Schemas](https://swagger.io/specification/#schemaObject) implicitly
Expand Down
2 changes: 2 additions & 0 deletions cmd/oapi-codegen/oapi-codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ func newConfigFromOldConfig(c oldConfiguration) configuration {
opts.Generate.GinServer = true
case "gorilla":
opts.Generate.GorillaServer = true
case "strict-server":
opts.Generate.Strict = true
case "types":
opts.Generate.Models = true
case "spec":
Expand Down
5 changes: 1 addition & 4 deletions examples/authenticated-api/echo/api/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions examples/petstore-expanded/chi/api/petstore.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions examples/petstore-expanded/echo/api/models/models.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions examples/petstore-expanded/gin/api/petstore-types.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions examples/petstore-expanded/gorilla/api/petstore.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions examples/petstore-expanded/petstore-client.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading