Skip to content

Commit ba6524e

Browse files
authored
Enable writeOnly (#146)
1 parent 509586a commit ba6524e

File tree

6 files changed

+67
-16
lines changed

6 files changed

+67
-16
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
/.vscode
55
/bench-*.txt
66
/vendor
7+
go.work
8+
go.work.sum

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/bool64/dev v0.2.39
77
github.com/stretchr/testify v1.8.2
88
github.com/swaggest/assertjson v1.9.0
9-
github.com/swaggest/jsonschema-go v0.3.73
9+
github.com/swaggest/jsonschema-go v0.3.74
1010
github.com/swaggest/refl v1.3.1
1111
gopkg.in/yaml.v2 v2.4.0
1212
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ
3333
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
3434
github.com/swaggest/assertjson v1.9.0 h1:dKu0BfJkIxv/xe//mkCrK5yZbs79jL7OVf9Ija7o2xQ=
3535
github.com/swaggest/assertjson v1.9.0/go.mod h1:b+ZKX2VRiUjxfUIal0HDN85W0nHPAYUbYH5WkkSsFsU=
36-
github.com/swaggest/jsonschema-go v0.3.73 h1:gU1pBzF3pkZ1GDD3dRMdQoCjrA0sldJ+QcM7aSSPgvc=
37-
github.com/swaggest/jsonschema-go v0.3.73/go.mod h1:qp+Ym2DIXHlHzch3HKz50gPf2wJhKOrAB/VYqLS2oJU=
36+
github.com/swaggest/jsonschema-go v0.3.74 h1:hkAZBK3RxNWU013kPqj0Q/GHGzYCCm9WcUTnfg2yPp0=
37+
github.com/swaggest/jsonschema-go v0.3.74/go.mod h1:qp+Ym2DIXHlHzch3HKz50gPf2wJhKOrAB/VYqLS2oJU=
3838
github.com/swaggest/refl v1.3.1 h1:XGplEkYftR7p9cz1lsiwXMM2yzmOymTE9vneVVpaOh4=
3939
github.com/swaggest/refl v1.3.1/go.mod h1:4uUVFVfPJ0NSX9FPwMPspeHos9wPFlCMGoPRllUbpvA=
4040
github.com/yudai/gojsondiff v1.0.0 h1:27cbfqXLVEJ1o8I6v3y9lg8Ydm53EKqHXAOMxEGlCOA=

openapi3/jsonschema.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ func (s *SchemaOrRef) toJSONSchema(ctx toJSONSchemaContext) jsonschema.SchemaOrB
164164
jso.Format = ss.Format
165165
jso.Default = ss.Default
166166
jso.ReadOnly = ss.ReadOnly
167+
jso.WriteOnly = ss.WriteOnly
168+
jso.Deprecated = ss.Deprecated
167169

168170
if ss.Example != nil {
169171
jso.WithExamples(*ss.Example)
@@ -186,7 +188,7 @@ func (s *SchemaOrRef) FromJSONSchema(schema jsonschema.SchemaOrBool) {
186188

187189
js := schema.TypeObject
188190
if js.Ref != nil {
189-
if deprecated, ok := js.ExtraProperties["deprecated"].(bool); ok && deprecated {
191+
if js.Deprecated != nil && *js.Deprecated {
190192
s.Schema = (&Schema{}).WithAllOf(
191193
SchemaOrRef{
192194
Schema: (&Schema{}).WithDeprecated(true),
@@ -230,9 +232,7 @@ func (s *SchemaOrRef) FromJSONSchema(schema jsonschema.SchemaOrBool) {
230232
os.Example = &js.Examples[0]
231233
}
232234

233-
if deprecated, ok := js.ExtraProperties["deprecated"].(bool); ok {
234-
os.Deprecated = &deprecated
235-
}
235+
os.Deprecated = js.Deprecated
236236

237237
if js.Type != nil {
238238
if js.Type.SimpleTypes != nil {
@@ -309,12 +309,9 @@ func (s *SchemaOrRef) FromJSONSchema(schema jsonschema.SchemaOrBool) {
309309
}
310310

311311
os.ReadOnly = js.ReadOnly
312+
os.WriteOnly = js.WriteOnly
312313
os.UniqueItems = js.UniqueItems
313314

314-
if writeOnly, ok := js.ExtraProperties["writeOnly"].(bool); ok {
315-
os.WriteOnly = &writeOnly
316-
}
317-
318315
for name, val := range js.ExtraProperties {
319316
if strings.HasPrefix(name, "x-") {
320317
if os.MapOfAnything == nil {

openapi3/reflect_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,3 +1461,59 @@ func TestSelfReference(t *testing.T) {
14611461
}
14621462
}`, reflector.SpecSchema())
14631463
}
1464+
1465+
func TestReadOnlyWriteOnlyDeprecated(t *testing.T) {
1466+
type ExampleObject struct {
1467+
Foo string `json:"foo" readOnly:"true"`
1468+
Bar string `json:"bar" writeOnly:"true"`
1469+
Baz string `json:"baz" deprecated:"true"`
1470+
}
1471+
1472+
type ExampleResponse struct {
1473+
ExampleObject `json:"example"`
1474+
}
1475+
1476+
type ExampleRequest struct {
1477+
ExampleObject `json:"example"`
1478+
}
1479+
1480+
reflector := openapi3.NewReflector()
1481+
op, err := reflector.NewOperationContext("GET", "/some/path")
1482+
require.NoError(t, err)
1483+
1484+
op.AddReqStructure(ExampleRequest{})
1485+
op.AddRespStructure(ExampleResponse{})
1486+
require.NoError(t, reflector.AddOperation(op))
1487+
1488+
assertjson.EqMarshal(t, `{
1489+
"openapi":"3.0.3","info":{"title":"","version":""},
1490+
"paths":{
1491+
"/some/path":{
1492+
"get":{
1493+
"responses":{
1494+
"200":{
1495+
"description":"OK",
1496+
"content":{
1497+
"application/json":{
1498+
"schema":{"$ref":"#/components/schemas/Openapi3TestExampleResponse"}
1499+
}
1500+
}
1501+
}
1502+
}
1503+
}
1504+
}
1505+
},
1506+
"components":{
1507+
"schemas":{
1508+
"Openapi3TestExampleObject":{
1509+
"type":"object",
1510+
"properties":{"bar":{"type":"string", "writeOnly": true},"baz":{"type":"string", "deprecated": true},"foo":{"type":"string","readOnly":true}}
1511+
},
1512+
"Openapi3TestExampleResponse":{
1513+
"type":"object",
1514+
"properties":{"example":{"$ref":"#/components/schemas/Openapi3TestExampleObject"}}
1515+
}
1516+
}
1517+
}
1518+
}`, reflector.SpecSchema())
1519+
}

openapi31/jsonschema.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ func isDeprecated(schema jsonschema.SchemaOrBool) *bool {
1111
return nil
1212
}
1313

14-
if d, ok := schema.TypeObject.ExtraProperties["deprecated"].(bool); ok {
15-
return &d
16-
}
17-
18-
return nil
14+
return schema.TypeObject.Deprecated
1915
}
2016

2117
type toJSONSchemaContext struct {

0 commit comments

Comments
 (0)