@@ -23,6 +23,10 @@ $id: "https://example.com/schemas/user"
2323$schema: "https://json-schema.org/draft/2020-12/schema"
2424format: object
2525pattern: "^user_"
26+ contentEncoding: base64
27+ contentMediaType: application/octet-stream
28+ contentSchema:
29+ type: string
2630multipleOf: 1.0
2731minimum: 0.0
2832maximum: 1000.0
@@ -174,6 +178,15 @@ x-metadata:
174178 require .Equal (t , "object" , schema .GetFormat ())
175179 require .Equal (t , "^user_" , schema .GetPattern ())
176180
181+ // Test content keywords
182+ require .Equal (t , "base64" , schema .GetContentEncoding ())
183+ require .Equal (t , "application/octet-stream" , schema .GetContentMediaType ())
184+ require .NotNil (t , schema .GetContentSchema ())
185+ require .True (t , schema .GetContentSchema ().IsSchema ())
186+ contentSchemaTypes := schema .GetContentSchema ().GetSchema ().GetType ()
187+ require .Len (t , contentSchemaTypes , 1 )
188+ require .Equal (t , oas3 .SchemaTypeString , contentSchemaTypes [0 ])
189+
177190 // Test anchor, $id, and schema
178191 require .NotNil (t , schema .Anchor )
179192 require .Equal (t , "user-schema" , * schema .Anchor )
@@ -575,3 +588,139 @@ properties:
575588 _ , ok = schema .Properties .Get ("name" )
576589 assert .True (t , ok , "property 'name' should exist" )
577590}
591+
592+ func TestSchema_Unmarshal_ContentKeywords_Success (t * testing.T ) {
593+ t .Parallel ()
594+
595+ tests := []struct {
596+ name string
597+ yml string
598+ expectedContentEncoding string
599+ expectedContentMediaType string
600+ checkContentSchema bool
601+ contentSchemaType oas3.SchemaType
602+ contentSchemaRequired []string
603+ contentSchemaProperties []string
604+ }{
605+ {
606+ name : "contentEncoding only" ,
607+ yml : `
608+ type: string
609+ contentEncoding: base64
610+ ` ,
611+ expectedContentEncoding : "base64" ,
612+ },
613+ {
614+ name : "contentMediaType only" ,
615+ yml : `
616+ type: string
617+ contentMediaType: application/octet-stream
618+ ` ,
619+ expectedContentMediaType : "application/octet-stream" ,
620+ },
621+ {
622+ name : "contentEncoding with contentMediaType" ,
623+ yml : `
624+ type: string
625+ contentEncoding: base64
626+ contentMediaType: image/png
627+ ` ,
628+ expectedContentEncoding : "base64" ,
629+ expectedContentMediaType : "image/png" ,
630+ },
631+ {
632+ name : "contentMediaType with contentSchema object" ,
633+ yml : `
634+ type: string
635+ contentMediaType: application/json
636+ contentSchema:
637+ type: object
638+ required:
639+ - subStringProperty
640+ properties:
641+ subStringProperty:
642+ type: string
643+ ` ,
644+ expectedContentMediaType : "application/json" ,
645+ checkContentSchema : true ,
646+ contentSchemaType : oas3 .SchemaTypeObject ,
647+ contentSchemaRequired : []string {"subStringProperty" },
648+ contentSchemaProperties : []string {"subStringProperty" },
649+ },
650+ {
651+ name : "all content keywords together" ,
652+ yml : `
653+ type: string
654+ contentEncoding: base64
655+ contentMediaType: application/json
656+ contentSchema:
657+ type: object
658+ required:
659+ - id
660+ - name
661+ properties:
662+ id:
663+ type: integer
664+ name:
665+ type: string
666+ tags:
667+ type: array
668+ items:
669+ type: string
670+ ` ,
671+ expectedContentEncoding : "base64" ,
672+ expectedContentMediaType : "application/json" ,
673+ checkContentSchema : true ,
674+ contentSchemaType : oas3 .SchemaTypeObject ,
675+ contentSchemaRequired : []string {"id" , "name" },
676+ contentSchemaProperties : []string {"id" , "name" , "tags" },
677+ },
678+ }
679+
680+ for _ , tt := range tests {
681+ t .Run (tt .name , func (t * testing.T ) {
682+ t .Parallel ()
683+
684+ var schema oas3.Schema
685+
686+ validationErrs , err := marshaller .Unmarshal (t .Context (), bytes .NewBufferString (tt .yml ), & schema )
687+ require .NoError (t , err , "unmarshal should succeed" )
688+ require .Empty (t , validationErrs , "should have no validation errors" )
689+
690+ if tt .expectedContentEncoding != "" {
691+ assert .Equal (t , tt .expectedContentEncoding , schema .GetContentEncoding ())
692+ } else {
693+ assert .Empty (t , schema .GetContentEncoding ())
694+ }
695+
696+ if tt .expectedContentMediaType != "" {
697+ assert .Equal (t , tt .expectedContentMediaType , schema .GetContentMediaType ())
698+ } else {
699+ assert .Empty (t , schema .GetContentMediaType ())
700+ }
701+
702+ if tt .checkContentSchema {
703+ require .NotNil (t , schema .GetContentSchema (), "contentSchema should not be nil" )
704+ require .True (t , schema .GetContentSchema ().IsSchema (), "contentSchema should be a schema" )
705+
706+ cs := schema .GetContentSchema ().GetSchema ()
707+
708+ types := cs .GetType ()
709+ require .Len (t , types , 1 )
710+ assert .Equal (t , tt .contentSchemaType , types [0 ])
711+
712+ assert .Equal (t , tt .contentSchemaRequired , cs .GetRequired ())
713+
714+ require .NotNil (t , cs .GetProperties ())
715+ for _ , prop := range tt .contentSchemaProperties {
716+ propSchema , ok := cs .GetProperties ().Get (prop )
717+ assert .True (t , ok , "property %q should exist in contentSchema" , prop )
718+ assert .NotNil (t , propSchema , "property %q schema should not be nil" , prop )
719+ }
720+ assert .Equal (t , len (tt .contentSchemaProperties ), cs .GetProperties ().Len ())
721+ } else {
722+ assert .Nil (t , schema .GetContentSchema (), "contentSchema should be nil" )
723+ }
724+ })
725+ }
726+ }
0 commit comments