Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion openapi3/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ paths:
schema:
type: string
content:
application/json:
application/json:
schema:
$ref: "#/components/schemas/Pets"
default:
Expand Down Expand Up @@ -585,6 +585,7 @@ func ExampleReflector_AddOperation_queryObject() {
// type: string
// type: object
// quux:
// format: double
// type: number
Comment on lines 587 to 589
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 | Confidence: High

The example update demonstrates the new format annotation in generated documentation, improving example accuracy. This serves as effective documentation for users expecting format annotations in their OpenAPI specs.

// type: object
// Openapi3TestJsonFilter:
Expand Down
16 changes: 16 additions & 0 deletions openapi3/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@
r := &Reflector{}
r.SpecEns()

r.DefaultOptions = append(r.DefaultOptions, jsonschema.InterceptSchema(func(params jsonschema.InterceptSchemaParams) (stop bool, err error) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 | Confidence: High

The change adds format annotations for numeric types in OpenAPI 3.0 schemas, aligning with OpenAPI specification standards. While this improves specification compliance, the implementation unconditionally sets formats without checking if they were already defined. This could override custom formats set by users or other interceptors in the chain. The change follows the same pattern as the OpenAPI 3.1 implementation (#140), but lacks a guard condition to preserve existing formats.

Code Suggestion:

switch params.Value.Kind() {
case reflect.Int64:
	if params.Schema.Format == nil {
		params.Schema.WithFormat("int64")
	}
// ... other cases

// See https://spec.openapis.org/oas/v3.0.0.html#data-types.
switch params.Value.Kind() { //nolint // Not all kinds have formats defined.
case reflect.Int64:
params.Schema.WithFormat("int64")
case reflect.Int32:
params.Schema.WithFormat("int32")
case reflect.Float32:
params.Schema.WithFormat("float")

Check notice on line 37 in openapi3/reflect.go

View workflow job for this annotation

GitHub Actions / test (stable)

3 statement(s) are not covered by tests.
case reflect.Float64:
params.Schema.WithFormat("double")
Comment on lines +36 to +39
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 | Confidence: Medium

The format names "float" and "double" align with OpenAPI 3.0 specification, but the code lacks inline comments explaining that these correspond to IEEE 754 single and double precision formats. Adding brief specification references would improve clarity for maintainers.

Code Suggestion:

case reflect.Float32:
	params.Schema.WithFormat("float") // IEEE 754 single precision
case reflect.Float64:
	params.Schema.WithFormat("double") // IEEE 754 double precision

}

return false, nil
}))

return r
}

Expand Down
2 changes: 1 addition & 1 deletion openapi3/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ func TestReflector_AddOperation_defName(t *testing.T) {
"paths":{
"/foo":{
"post":{
"parameters":[{"name":"bar","in":"header","schema":{"type":"number"}}],
"parameters":[{"name":"bar","in":"header","schema":{"type":"number","format":"double"}}],
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 | Confidence: High

The test update correctly validates the new format behavior, but only covers the float64 case. The test suite would benefit from additional cases verifying int32, int64, and float32 format assignments to ensure full coverage of the new logic.

"requestBody":{
"content":{
"application/json":{"schema":{"$ref":"#/components/schemas/Openapi3TestReqJSON"}},
Expand Down
Loading