@@ -1746,6 +1746,82 @@ public async Task SerializePatternPropertiesAsExtensionInEarlierVersions(OpenApi
17461746 Assert . True ( JsonNode . DeepEquals ( JsonNode . Parse ( expected ) , JsonNode . Parse ( actual ) ) ) ;
17471747 }
17481748
1749+ [ Theory ]
1750+ [ InlineData ( OpenApiSpecVersion . OpenApi2_0 ) ]
1751+ [ InlineData ( OpenApiSpecVersion . OpenApi3_0 ) ]
1752+ public async Task SerializeSingleExampleAsExampleInV2V3WhenExampleUnset ( OpenApiSpecVersion version )
1753+ {
1754+ var expected = """
1755+ {
1756+ "example": "example value"
1757+ }
1758+ """ ;
1759+ var schema = new OpenApiSchema
1760+ {
1761+ Examples =
1762+ [
1763+ JsonValue . Create ( "example value" ) !
1764+ ]
1765+ } ;
1766+
1767+ var actual = await schema . SerializeAsJsonAsync ( version ) ;
1768+
1769+ Assert . True ( JsonNode . DeepEquals ( JsonNode . Parse ( expected ) , JsonNode . Parse ( actual ) ) ) ;
1770+ }
1771+
1772+ [ Theory ]
1773+ [ InlineData ( OpenApiSpecVersion . OpenApi2_0 ) ]
1774+ [ InlineData ( OpenApiSpecVersion . OpenApi3_0 ) ]
1775+ public async Task SerializeMultipleExamplesInV2V3WhenExampleUnset ( OpenApiSpecVersion version )
1776+ {
1777+ var expected = """
1778+ {
1779+ "example": "example value",
1780+ "x-jsonschema-examples": [
1781+ 42
1782+ ]
1783+ }
1784+ """ ;
1785+ var schema = new OpenApiSchema
1786+ {
1787+ Examples =
1788+ [
1789+ JsonValue . Create ( "example value" ) ! ,
1790+ JsonValue . Create ( 42 ) !
1791+ ]
1792+ } ;
1793+
1794+ var actual = await schema . SerializeAsJsonAsync ( version ) ;
1795+
1796+ Assert . True ( JsonNode . DeepEquals ( JsonNode . Parse ( expected ) , JsonNode . Parse ( actual ) ) ) ;
1797+ }
1798+
1799+ [ Theory ]
1800+ [ InlineData ( OpenApiSpecVersion . OpenApi3_1 ) ]
1801+ public async Task SerializeExamplesAsJsonSchemaKeywordInV31AndLater ( OpenApiSpecVersion version )
1802+ {
1803+ var expected = """
1804+ {
1805+ "examples": [
1806+ "example value",
1807+ 42
1808+ ]
1809+ }
1810+ """ ;
1811+ var schema = new OpenApiSchema
1812+ {
1813+ Examples =
1814+ [
1815+ JsonValue . Create ( "example value" ) ! ,
1816+ JsonValue . Create ( 42 ) !
1817+ ]
1818+ } ;
1819+
1820+ var actual = await schema . SerializeAsJsonAsync ( version ) ;
1821+
1822+ Assert . True ( JsonNode . DeepEquals ( JsonNode . Parse ( expected ) , JsonNode . Parse ( actual ) ) ) ;
1823+ }
1824+
17491825 [ Theory ]
17501826 [ InlineData ( OpenApiSpecVersion . OpenApi2_0 ) ]
17511827 [ InlineData ( OpenApiSpecVersion . OpenApi3_0 ) ]
@@ -1837,6 +1913,76 @@ public void DeserializePatternPropertiesExtensionInV3AssignsPatternPropertiesPro
18371913 Assert . True ( schema . Extensions is null || ! schema . Extensions . ContainsKey ( "x-jsonschema-patternProperties" ) ) ;
18381914 }
18391915
1916+ [ Fact ]
1917+ public void DeserializeExamplesExtensionInV2AssignsExamplesProperty ( )
1918+ {
1919+ var jsonContent = """
1920+ {
1921+ "swagger": "2.0",
1922+ "info": { "title": "Test", "version": "1.0" },
1923+ "paths": {},
1924+ "definitions": {
1925+ "TestSchema": {
1926+ "type": "string",
1927+ "example": "primary example",
1928+ "x-jsonschema-examples": [
1929+ "secondary example",
1930+ 42
1931+ ]
1932+ }
1933+ }
1934+ }
1935+ """ ;
1936+
1937+ var readResult = OpenApiDocument . Parse ( jsonContent , "json" ) ;
1938+
1939+ Assert . Empty ( readResult . Diagnostic . Errors ) ;
1940+ var schema = readResult . Document . Components . Schemas [ "TestSchema" ] ;
1941+ Assert . Equal ( "primary example" , schema . Example ? . GetValue < string > ( ) ) ;
1942+ Assert . NotNull ( schema . Examples ) ;
1943+ Assert . Collection (
1944+ schema . Examples ,
1945+ example => Assert . Equal ( "secondary example" , example . GetValue < string > ( ) ) ,
1946+ example => Assert . Equal ( 42 , example . GetValue < int > ( ) ) ) ;
1947+ Assert . True ( schema . Extensions is null || ! schema . Extensions . ContainsKey ( OpenApiConstants . JsonSchemaExamplesExtension ) ) ;
1948+ }
1949+
1950+ [ Fact ]
1951+ public void DeserializeExamplesExtensionInV3AssignsExamplesProperty ( )
1952+ {
1953+ var jsonContent = """
1954+ {
1955+ "openapi": "3.0.0",
1956+ "info": { "title": "Test", "version": "1.0" },
1957+ "paths": {},
1958+ "components": {
1959+ "schemas": {
1960+ "TestSchema": {
1961+ "type": "string",
1962+ "example": "primary example",
1963+ "x-jsonschema-examples": [
1964+ "secondary example",
1965+ 42
1966+ ]
1967+ }
1968+ }
1969+ }
1970+ }
1971+ """ ;
1972+
1973+ var readResult = OpenApiDocument . Parse ( jsonContent , "json" ) ;
1974+
1975+ Assert . Empty ( readResult . Diagnostic . Errors ) ;
1976+ var schema = readResult . Document . Components . Schemas [ "TestSchema" ] ;
1977+ Assert . Equal ( "primary example" , schema . Example ? . GetValue < string > ( ) ) ;
1978+ Assert . NotNull ( schema . Examples ) ;
1979+ Assert . Collection (
1980+ schema . Examples ,
1981+ example => Assert . Equal ( "secondary example" , example . GetValue < string > ( ) ) ,
1982+ example => Assert . Equal ( 42 , example . GetValue < int > ( ) ) ) ;
1983+ Assert . True ( schema . Extensions is null || ! schema . Extensions . ContainsKey ( OpenApiConstants . JsonSchemaExamplesExtension ) ) ;
1984+ }
1985+
18401986 [ Fact ]
18411987 public void DeserializeContainsExtensionsInV3AssignsContainsProperties ( )
18421988 {
0 commit comments