-
-
Notifications
You must be signed in to change notification settings - Fork 232
fix: stable OpenAPI property and param ordering #71
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0b4fbd0
fix: stable openAPI param ordering
iwong-isp 3a1efce
docs: add bookstore example
iwong-isp baee032
fix: stable openAPI property ordering
iwong-isp 44bed11
fix: revert changing spec.Bytes
iwong-isp 1af10e9
chore: remove extra white space
iwong-isp 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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/danielgtaylor/huma" | ||
| "github.com/danielgtaylor/huma/cli" | ||
| "github.com/danielgtaylor/huma/middleware" | ||
| "github.com/danielgtaylor/huma/responses" | ||
| ) | ||
|
|
||
| // GenreSummary is used to list genres. It does not include the (potentially) | ||
| // large genre content. | ||
| type GenreSummary struct { | ||
| ID string `json:"id" doc:"Genre ID"` | ||
| Description string `json:"description" doc:"Description"` | ||
| Created time.Time `json:"created" doc:"Created date/time as ISO8601"` | ||
| } | ||
|
|
||
| type GenrePutRequest struct { | ||
| Description string `json:"description" doc:"Description"` | ||
| } | ||
|
|
||
| // GenreIDParam gets the genre ID from the URI path. | ||
| type GenreIDParam struct { | ||
| GenreID string `path:"genre-id" pattern:"^[a-zA-Z0-9._-]{1,32}$"` | ||
| } | ||
|
|
||
| // Genre records some content text for later reference. | ||
| type Genre struct { | ||
| ID string `json:"id" doc:"Genre ID"` | ||
| Books []Book `json:"books" doc:"Books"` | ||
| Description string `json:"description" doc:"Description"` | ||
| Created time.Time `json:"created" readOnly:"true" doc:"Created date/time as ISO8601"` | ||
| } | ||
|
|
||
| type Book struct { | ||
| ID string `json:"id" doc:"Book ID"` | ||
| Title string `json:"title" doc:"Title"` | ||
| Author string `json:"author" doc:"Author"` | ||
| Published time.Time `json:"published" doc:"Created date/time as ISO8601"` | ||
| } | ||
|
|
||
| type BookPutRequest struct { | ||
| Title string `json:"title" doc:"Title"` | ||
| Author string `json:"author" doc:"Author"` | ||
| Published time.Time `json:"published" doc:"Created date/time as ISO8601"` | ||
| } | ||
|
|
||
| type BookIDParam struct { | ||
| BookID string `path:"book-id" pattern:"^[a-zA-Z0-9._-]{1,32}$"` | ||
| } | ||
|
|
||
| // We'll use an in-memory DB (a goroutine-safe map). Don't do this in | ||
| // production code! | ||
| var memoryDB = sync.Map{} | ||
|
|
||
| func main() { | ||
| // Create a new router and give our API a title and version. | ||
| app := cli.NewRouter("BookStore API", "1.0.0") | ||
| app.ServerLink("Development server", "http://localhost:8888") | ||
|
|
||
| genres := app.Resource("/v1/genres") | ||
| genres.Get("list-genres", "Returns a list of all genres", | ||
| responses.OK().Model([]*GenreSummary{}), | ||
| ).Run(func(ctx huma.Context) { | ||
| // Create a list of summaries from all the genres. | ||
| summaries := make([]*GenreSummary, 0) | ||
|
|
||
| memoryDB.Range(func(k, v interface{}) bool { | ||
| summaries = append(summaries, &GenreSummary{ | ||
| ID: k.(string), | ||
| Description: v.(Genre).Description, | ||
| Created: v.(Genre).Created, | ||
| }) | ||
| return true | ||
| }) | ||
|
|
||
| ctx.WriteModel(http.StatusOK, summaries) | ||
| }) | ||
|
|
||
| // Add an `id` path parameter to create a genre resource. | ||
| genre := genres.SubResource("/{genre-id}") | ||
|
|
||
| genre.Put("put-genre", "Create or update a genre", | ||
| responses.NoContent(), | ||
| ).Run(func(ctx huma.Context, input struct { | ||
| GenreIDParam | ||
| Body GenrePutRequest | ||
| }) { | ||
| middleware.GetLogger(ctx).Info("Creating a new genre") | ||
|
|
||
| // Set the created time to now and then save the genre in the DB. | ||
| new := Genre{ | ||
| ID: input.GenreID, | ||
| Description: input.Body.Description, | ||
| Created: time.Now(), | ||
| Books: []Book{}, | ||
| } | ||
| memoryDB.Store(input.GenreID, new) | ||
| }) | ||
|
|
||
| genre.Get("get-genre", "Get a genre by its ID", | ||
| responses.OK().Model(Genre{}), | ||
| responses.NotFound(), | ||
| ).Run(func(ctx huma.Context, input GenreIDParam) { | ||
| if g, ok := memoryDB.Load(input.GenreID); ok { | ||
| // Genre with that ID exists! | ||
| ctx.WriteModel(http.StatusOK, g.(Genre)) | ||
| return | ||
| } | ||
|
|
||
| ctx.WriteError(http.StatusNotFound, "Genre "+input.GenreID+" not found") | ||
| }) | ||
|
|
||
| genre.Delete("delete-genre", "Delete a genre by its ID", | ||
| responses.NoContent(), | ||
| responses.NotFound(), | ||
| ).Run(func(ctx huma.Context, input GenreIDParam) { | ||
| if _, ok := memoryDB.Load(input.GenreID); ok { | ||
| // Genre with that ID exists! | ||
| memoryDB.Delete(input.GenreID) | ||
| ctx.WriteHeader(http.StatusNoContent) | ||
| return | ||
| } | ||
|
|
||
| ctx.WriteError(http.StatusNotFound, "Genre "+input.GenreID+" not found") | ||
| }) | ||
|
|
||
| books := genre.SubResource("/books") | ||
| books.Tags("Books by Genre") | ||
|
|
||
| books.Get("list-books", "Returns a list of all books for a genre", | ||
| []huma.Response{ | ||
| responses.OK().Model([]Book{}), | ||
| responses.NotFound(), | ||
| }..., | ||
| ).Run(func(ctx huma.Context, input struct { | ||
| GenreIDParam | ||
| }) { | ||
|
|
||
| if g, ok := memoryDB.Load(input.GenreID); ok { | ||
| ctx.WriteModel(http.StatusOK, g.(Genre).Books) | ||
| return | ||
| } | ||
|
|
||
| ctx.WriteError(http.StatusNotFound, "Genre "+input.GenreID+" not found") | ||
| }) | ||
|
|
||
| book := books.SubResource("/{book-id}") | ||
| book.Put("put-book", "Create or update a book", | ||
| responses.NoContent(), | ||
| ).Run(func(ctx huma.Context, input struct { | ||
| GenreIDParam | ||
| BookIDParam | ||
| Body BookPutRequest | ||
| }) { | ||
| middleware.GetLogger(ctx).Info("Creating a new book") | ||
|
|
||
| if g, ok := memoryDB.Load(input.GenreID); !ok { | ||
| // Genre with that ID doesn't exists! | ||
| ctx.WriteError(http.StatusNotFound, "Genre "+input.GenreID+" not found") | ||
| return | ||
| } else { | ||
| genre := g.(Genre) | ||
| genre.Books = append(genre.Books, Book{ | ||
| Title: input.Body.Title, | ||
| Author: input.Body.Author, | ||
| ID: input.BookID, | ||
| Published: input.Body.Published, | ||
| }) | ||
|
|
||
| memoryDB.Store(input.GenreID, genre) | ||
| } | ||
|
|
||
| }) | ||
|
|
||
| book.Get("get-book", "Get a book by its ID", | ||
| responses.OK().Model(Book{}), | ||
| responses.NotFound(), | ||
| ).Run(func(ctx huma.Context, input struct { | ||
| GenreIDParam | ||
| BookIDParam | ||
| }) { | ||
| if g, ok := memoryDB.Load(input.GenreID); !ok { | ||
| // Genre with that ID exists! | ||
| ctx.WriteError(http.StatusNotFound, "Genre "+input.GenreID+" not found") | ||
| return | ||
| } else { | ||
| for _, book := range g.(Genre).Books { | ||
| if book.ID == input.BookID { | ||
| ctx.WriteModel(http.StatusOK, book) | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ctx.WriteError(http.StatusNotFound, "Book "+input.BookID+" not found") | ||
| }) | ||
|
|
||
| book.Delete("delete-book", "Delete a book by its ID", | ||
| responses.NoContent(), | ||
| responses.NotFound(), | ||
| ).Run(func(ctx huma.Context, input struct { | ||
| GenreIDParam | ||
| BookIDParam | ||
| }) { | ||
| if g, ok := memoryDB.Load(input.GenreID); !ok { | ||
| // Genre with that ID exists! | ||
| ctx.WriteError(http.StatusNotFound, "Genre "+input.GenreID+" not found") | ||
| return | ||
| } else { | ||
| for _, book := range g.(Genre).Books { | ||
| if book.ID == input.BookID { | ||
| ctx.WriteHeader(http.StatusNoContent) | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ctx.WriteError(http.StatusNotFound, "Book "+input.BookID+" not found") | ||
| }) | ||
|
|
||
| // Run the app! | ||
| app.Run() | ||
| } |
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
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
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
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
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.
non-blocking: there's a 'JSONEq' that could be useful here. Something like:
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 considered using that, but unfortunately, the output of the
gabs.Searchends at the operation level. This results inopenapicontaining the full spec for thegetoperation (ex.responses, additional properties for aparameter).JSONEqonly returns true if the entire JSON payload matches (ignoring order). Given we're only interested in using thenameto check order was correctly preserved, I decided with a temp type to only parse the necessary field.