-
-
Notifications
You must be signed in to change notification settings - Fork 125
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
Nested struct visibility #314
Nested struct visibility #314
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #314 +/- ##
=======================================
Coverage 95.13% 95.13%
=======================================
Files 19 19
Lines 2773 2777 +4
=======================================
+ Hits 2638 2642 +4
Misses 98 98
Partials 37 37 ☔ View full report in Codecov by Sentry. |
@CptKirk I added a commit with a slightly different approach. JSON Schema allows for type: object
properties:
foo:
$ref: '#/components/schemas/FooSchema'
readOnly: true The validator didn't support that as it always resolved refs before checking validations. The commit I added changes that behavior to check at the top level, which supports how we generate schemas and works fine if no Please let me know what you think! The only weird thing I ran into is that Stoplight Docs don't mark it as read-only, but that seems to be a bug as both Swagger UI and Scalar Docs do the right thing for this simple example: type GreetingOutput struct {
Body struct {
Message string `json:"message" example:"Hello, world!" doc:"Greeting message"`
Another struct {
Foo string `json:"foo"`
} `json:"another" readOnly:"true"`
}
} |
@danielgtaylor thanks for providing the input! I checked out your solution and indeed I can confirm, that the validation works correctly with your changes but the stoplight docs do not. The problem seems to be, that Stoplight parses the OpenAPI spec differently than Swagger UI, because with my proposed changes Stoplight Docs become correct. I have created a little test case to figure out what the exact problem is. I created the following case: type NestedStruct struct {
Value int `json:"value"`
Foo struct {
Bar string `json:"bar"`
} `json:"foo" readOnly:"true"`
}
func main() {
router := chi.NewMux()
api := humachi.New(router, huma.DefaultConfig("My API", "1.0.0"))
huma.Register(api, huma.Operation{
OperationID: "post-nested",
Method: http.MethodPost,
Path: "/nested",
Summary: "Create a nested",
Tags: []string{"Nested"},
}, func(
ctx context.Context,
input *struct {
Body *NestedStruct
}) (
*struct {
Body *NestedStruct
}, error) {
resp := &NestedStruct{}
return &struct{ Body *NestedStruct }{Body: resp}, nil
})
http.ListenAndServe("127.0.0.1:8888", router)
} Then I downloaded the OpenAPI spec for mine and your solution, ran a diff and indeed there is a small difference: --- danielgtaylor.json 2024-03-15 11:51:57.546482363 +0100
+++ cptkirk.json 2024-03-15 11:52:40.267077258 +0100
@@ -100,20 +100,21 @@
],
"type": "object"
},
"NestedStructFooStruct": {
"additionalProperties": false,
"properties": {
"bar": {
"type": "string"
}
},
+ "readOnly": true,
"required": [
"bar"
],
"type": "object"
}
}
},
"info": {
"title": "My API",
"version": "1.0.0" I agree that a new method in the |
@CptKirk the problem I'm running into is this scenario: type Foo struct {
Field string `json:"field"`
}
type ExampleResponse1 struct {
Body struct {
Example Foo `json:"example" readOnly:"true"`
}
}
type ExampleResponse2 struct {
Body struct {
Example Foo `json:"example"` // NOT readOnly!
}
} In this case it is incorrect to mark the generated I wonder if we can get Stoplight to fix their support of |
@danielgtaylor Ah, now I see the problem. Yeah you are right, this is a case where my proposed solution would break things. Thanks for taking the time explaining it to me. I think you can merge your solution as it fixes the problem with validation. I will look over the weekend into the issue with Stoplight and will try to fix it. |
This pull request enables
readOnly
,writeOnly
anddeprecated
struct tags on nested structs.Resolves issue: #313