Repository: swaggest/openapi-go
Pull request #53 (2022-11-21 09:11:44)

Update jsonschema-go, add options example

github-actions[bot]: ### Lines Of Code

| Language  | Files  |   Lines    |    Code    | Comments  |  Blanks   | Complexity |     Bytes     |
|-----------|--------|------------|------------|-----------|-----------|------------|---------------|
| Go (test) | 7 (+1) | 1748 (+54) | 1280 (+29) | 244 (+15) | 224 (+10) | 25 (+4)    | 45.3K (+1.5K) |
<!-- Sticky Pull Request CommentLOC -->
github-actions[bot]: ### Unit Test Coverage
total:								(statements)				49.6%

<details><summary>Coverage of changed lines</summary>

No changes in testable statements.

</details>

<details><summary>Coverage diff with base branch</summary>

No changes in coverage.

</details>
<!-- Sticky Pull Request Commentunit-test -->
github-actions[bot]: ### Go API Changes

<pre>
# summary
Inferred base version: v0.2.25
Suggested version: v0.2.26
</pre>
<!-- Sticky Pull Request Commentgorelease -->
codecov[bot]: # [Codecov](https://codecov.io/gh/swaggest/openapi-go/pull/53?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=swaggest) Report
> Merging [#53](https://codecov.io/gh/swaggest/openapi-go/pull/53?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=swaggest) (84dc858) into [master](https://codecov.io/gh/swaggest/openapi-go/commit/c38df69bdf38e79a4941b588b2e51f00363b353f?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=swaggest) (c38df69) will **not change** coverage.
> The diff coverage is `n/a`.

```diff
@@           Coverage Diff           @@
##           master      #53   +/-   ##
=======================================
  Coverage   41.93%   41.93%           
=======================================
  Files           5        5           
  Lines        3570     3570           
=======================================
  Hits         1497     1497           
  Misses       1784     1784           
  Partials      289      289           
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `41.93% <ø> (ø)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=swaggest#carryforward-flags-in-the-pull-request-comment) to find out more.


:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=swaggest)


Patch:
diff --git a/go.mod b/go.mod
index 65e8260..1d21488 100644
--- a/go.mod
+++ b/go.mod
@@ -6,7 +6,7 @@ require (
 	github.com/bool64/dev v0.2.22
 	github.com/stretchr/testify v1.8.1
 	github.com/swaggest/assertjson v1.7.0
-	github.com/swaggest/jsonschema-go v0.3.41
+	github.com/swaggest/jsonschema-go v0.3.42
 	github.com/swaggest/refl v1.1.0
 	gopkg.in/yaml.v2 v2.4.0
 )
diff --git a/go.sum b/go.sum
index 142431a..4d6d7b4 100644
--- a/go.sum
+++ b/go.sum
@@ -64,8 +64,8 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
 github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
 github.com/swaggest/assertjson v1.7.0 h1:SKw5Rn0LQs6UvmGrIdaKQbMR1R3ncXm5KNon+QJ7jtw=
 github.com/swaggest/assertjson v1.7.0/go.mod h1:vxMJMehbSVJd+dDWFCKv3QRZKNTpy/ktZKTz9LOEDng=
-github.com/swaggest/jsonschema-go v0.3.41 h1:YsrBk5TW+S7/jTudvo1jWUJTECAIBdfk4iM7O9kzbnU=
-github.com/swaggest/jsonschema-go v0.3.41/go.mod h1:yt5lcjdqywYtNnzkFgYnyfvMwlH2XHEDitKw749AV1w=
+github.com/swaggest/jsonschema-go v0.3.42 h1:Eepch6hlDtEuSVgfBH4NsP9F2Yf/KGj+7MwgBk618ac=
+github.com/swaggest/jsonschema-go v0.3.42/go.mod h1:yt5lcjdqywYtNnzkFgYnyfvMwlH2XHEDitKw749AV1w=
 github.com/swaggest/refl v1.1.0 h1:a+9a75Kv6ciMozPjVbOfcVTEQe81t2R3emvaD9oGQGc=
 github.com/swaggest/refl v1.1.0/go.mod h1:g3Qa6ki0A/L2yxiuUpT+cuBURuRaltF5SDQpg1kMZSY=
 github.com/yosuke-furukawa/json5 v0.1.2-0.20201207051438-cf7bb3f354ff/go.mod h1:sw49aWDqNdRJ6DYUtIQiaA3xyj2IL9tjeNYmX2ixwcU=


Test patch:
diff --git a/openapi3/example_misc_test.go b/openapi3/example_misc_test.go
new file mode 100644
index 0000000..a1ded54
--- /dev/null
+++ b/openapi3/example_misc_test.go
@@ -0,0 +1,54 @@
+package openapi3_test
+
+import (
+	"fmt"
+	"net/http"
+	"reflect"
+
+	"github.com/swaggest/assertjson"
+	"github.com/swaggest/jsonschema-go"
+	"github.com/swaggest/openapi-go/openapi3"
+)
+
+func ExampleReflector_options() {
+	r := openapi3.Reflector{}
+
+	// Reflector embeds jsonschema.Reflector and it is possible to configure optional behavior.
+	r.Reflector.DefaultOptions = append(r.Reflector.DefaultOptions,
+		jsonschema.InterceptNullability(func(params jsonschema.InterceptNullabilityParams) {
+			// Removing nullability from non-pointer slices (regardless of omitempty).
+			if params.Type.Kind() != reflect.Ptr && params.Schema.HasType(jsonschema.Null) && params.Schema.HasType(jsonschema.Array) {
+				*params.Schema.Type = jsonschema.Array.Type()
+			}
+		}))
+
+	type req struct {
+		Foo []int `json:"foo"`
+	}
+
+	oc := openapi3.OperationContext{
+		Operation: &openapi3.Operation{},
+		Input:     new(req),
+	}
+
+	_ = r.SetupRequest(oc)
+	_ = r.SpecEns().AddOperation(http.MethodGet, "/foo", *oc.Operation)
+
+	j, _ := assertjson.MarshalIndentCompact(r.Spec, "", " ", 120)
+
+	fmt.Println(string(j))
+
+	// Output:
+	// {
+	//  "openapi":"3.0.3","info":{"title":"","version":""},
+	//  "paths":{
+	//   "/foo":{
+	//    "get":{
+	//     "requestBody":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/Openapi3TestReq"}}}},
+	//     "responses":{"204":{"description":"No Content"}}
+	//    }
+	//   }
+	//  },
+	//  "components":{"schemas":{"Openapi3TestReq":{"type":"object","properties":{"foo":{"type":"array","items":{"type":"integer"}}}}}}
+	// }
+}
diff --git a/openapi3/reflect_test.go b/openapi3/reflect_test.go
index dec7001..7889aa7 100644
--- a/openapi3/reflect_test.go
+++ b/openapi3/reflect_test.go
@@ -535,7 +535,7 @@ func TestReflector_SetupRequest_queryNamedObject(t *testing.T) {
 	  }
 	 },
 	 "components":{
-	  "schemas":{"Openapi3TestLabels":{"type":"object","additionalProperties":{"type":"number"},"nullable":true}}
+	  "schemas":{"Openapi3TestLabels":{"type":"object","additionalProperties":{"type":"number"}}}
 	 }
 	}`)
 
@@ -543,7 +543,7 @@ func TestReflector_SetupRequest_queryNamedObject(t *testing.T) {
 
 	js, found := reflector.ResolveJSONSchemaRef("#/components/schemas/Openapi3TestLabels")
 	assert.True(t, found)
-	assertjson.EqualMarshal(t, []byte(`{"type":["object","null"],"additionalProperties":{"type":"number"}}`), js)
+	assertjson.EqualMarshal(t, []byte(`{"type":"object","additionalProperties":{"type":"number"}}`), js)
 }
 
 func TestReflector_SetupRequest_jsonQuery(t *testing.T) {