You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -505,42 +505,200 @@ authorization, etc. Note that middlewares are server-specific.
505
505
506
506
### Generating API clients
507
507
508
-
**??**
508
+
As well as generating the server-side boilerplate, `oapi-codegen` can also generate API clients.
509
509
510
-
This produces code such as:
510
+
For instance, given an `api.yaml`:
511
511
512
-
```go
513
-
// TODO
512
+
```yaml
513
+
openapi: "3.0.0"
514
+
info:
515
+
version: 1.0.0
516
+
title: Generate models
517
+
paths:
518
+
/client:
519
+
get:
520
+
operationId: getClient
521
+
responses:
522
+
200:
523
+
content:
524
+
application/json:
525
+
schema:
526
+
$ref: "#/components/schemas/ClientType"
527
+
put:
528
+
operationId: updateClient
529
+
responses:
530
+
400:
531
+
content:
532
+
application/json:
533
+
schema:
534
+
type: object
535
+
properties:
536
+
code:
537
+
type: string
538
+
required:
539
+
- code
540
+
components:
541
+
schemas:
542
+
ClientType:
543
+
type: object
544
+
required:
545
+
- name
546
+
properties:
547
+
name:
548
+
type: string
549
+
# NOTE that this is not generated by default because it's not referenced. If you want it, you need to use the following YAML configuration:
550
+
#
551
+
# output-options:
552
+
# skip-prune: true
553
+
Unreferenced:
554
+
type: object
555
+
required:
556
+
- id
557
+
properties:
558
+
id:
559
+
type: int
560
+
```
561
+
562
+
And a `cfg.yaml`:
563
+
564
+
```yaml
565
+
package: client
566
+
output: client.gen.go
567
+
generate:
568
+
models: true
569
+
client: true
514
570
```
515
571
516
-
You are then able to **??**.
572
+
And a `generate.go`:
517
573
518
574
```go
575
+
package client
519
576
577
+
//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen -config cfg.yaml api.yaml
520
578
```
521
579
522
-
### Generating API models
580
+
This would then generate:
523
581
524
-
If you're looking to only generate the models for interacting with a remote service, for instance if you need to hand-roll the API client for whatever reason, you can do this as-is.
582
+
```go
583
+
package client
525
584
526
-
For instance, given a `generate.go`:
585
+
// ...
527
586
528
-
```go
529
-
package onlymodels
587
+
// ClientType defines model for ClientType.
588
+
type ClientType struct {
589
+
Name string `json:"name"`
590
+
}
530
591
531
-
//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen -config cfg.yaml api.yaml
592
+
// ...
593
+
594
+
// Client which conforms to the OpenAPI3 specification for this service.
595
+
type Client struct {
596
+
// The endpoint of the server conforming to this interface, with scheme,
597
+
// https://api.deepmap.com for example. This can contain a path relative
598
+
// to the server, such as https://api.deepmap.com/dev-test, and all the
599
+
// paths in the swagger spec will be appended to the server.
600
+
Server string
601
+
602
+
// Doer for performing requests, typically a *http.Client with any
603
+
// customized settings, such as certificate chains.
604
+
Client HttpRequestDoer
605
+
606
+
// A list of callbacks for modifying requests which are generated before sending over
607
+
// the network.
608
+
RequestEditors []RequestEditorFn
609
+
}
610
+
611
+
// ...
612
+
613
+
// The interface specification for the client above.
log.Fatalf("Expected HTTP 200 but received %d", resp.StatusCode())
689
+
}
690
+
691
+
fmt.Printf("resp.JSON200: %v\n", resp.JSON200)
692
+
}
693
+
694
+
}
541
695
```
542
696
543
-
And an `api.yaml`:
697
+
### Generating API models
698
+
699
+
If you're looking to only generate the models for interacting with a remote service, for instance if you need to hand-roll the API client for whatever reason, you can do this as-is.
700
+
701
+
For instance, given an `api.yaml`:
544
702
545
703
```yaml
546
704
openapi: "3.0.0"
@@ -595,6 +753,23 @@ components:
595
753
type: int
596
754
```
597
755
756
+
And a `cfg.yaml`:
757
+
758
+
```yaml
759
+
package: onlymodels
760
+
output: only-models.gen.go
761
+
generate:
762
+
models: true
763
+
```
764
+
765
+
And a `generate.go`:
766
+
767
+
```go
768
+
package onlymodels
769
+
770
+
//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen -config cfg.yaml api.yaml
0 commit comments