@stoplight/elements-core 9.0.24 → 9.0.25-beta-0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.esm.js CHANGED
@@ -1846,6 +1846,72 @@ const mergeOneOfAnyOf = (schema) => {
1846
1846
  delete result.anyOf;
1847
1847
  return result;
1848
1848
  };
1849
+ const OAS_FORMAT_RANGES = {
1850
+ int32: { minimum: 0 - Math.pow(2, 31), maximum: Math.pow(2, 31) - 1 },
1851
+ int64: { minimum: Number.MIN_SAFE_INTEGER, maximum: Number.MAX_SAFE_INTEGER },
1852
+ float: { minimum: 0 - Math.pow(2, 128), maximum: Math.pow(2, 128) - 1 },
1853
+ double: { minimum: 0 - Number.MAX_VALUE, maximum: Number.MAX_VALUE },
1854
+ };
1855
+ const stripInferredNumericBounds = (schema) => {
1856
+ var _a;
1857
+ if (!isPlainObject$1(schema)) {
1858
+ return schema;
1859
+ }
1860
+ const result = Object.assign({}, schema);
1861
+ const explicitProperties = Array.isArray((_a = result === null || result === void 0 ? void 0 : result['x-stoplight']) === null || _a === void 0 ? void 0 : _a.explicitProperties)
1862
+ ? result['x-stoplight'].explicitProperties
1863
+ : [];
1864
+ const isNumericType = result.type === 'integer' || result.type === 'number';
1865
+ const formatRange = OAS_FORMAT_RANGES[result.format];
1866
+ const hasExplicitMinimum = explicitProperties.includes('minimum') && result.minimum !== (formatRange === null || formatRange === void 0 ? void 0 : formatRange.minimum);
1867
+ const hasExplicitMaximum = explicitProperties.includes('maximum') && result.maximum !== (formatRange === null || formatRange === void 0 ? void 0 : formatRange.maximum);
1868
+ if (isNumericType && !hasExplicitMinimum) {
1869
+ delete result.minimum;
1870
+ }
1871
+ if (isNumericType && !hasExplicitMaximum) {
1872
+ delete result.maximum;
1873
+ }
1874
+ if (result.properties && isPlainObject$1(result.properties)) {
1875
+ result.properties = Object.fromEntries(Object.entries(result.properties).map(([key, value]) => [key, stripInferredNumericBounds(value)]));
1876
+ }
1877
+ if (Array.isArray(result.items)) {
1878
+ result.items = result.items.map((item) => stripInferredNumericBounds(item));
1879
+ }
1880
+ else if (result.items) {
1881
+ result.items = stripInferredNumericBounds(result.items);
1882
+ }
1883
+ if (Array.isArray(result.allOf)) {
1884
+ result.allOf = result.allOf.map((item) => stripInferredNumericBounds(item));
1885
+ }
1886
+ if (Array.isArray(result.oneOf)) {
1887
+ result.oneOf = result.oneOf.map((item) => stripInferredNumericBounds(item));
1888
+ }
1889
+ if (Array.isArray(result.anyOf)) {
1890
+ result.anyOf = result.anyOf.map((item) => stripInferredNumericBounds(item));
1891
+ }
1892
+ return result;
1893
+ };
1894
+ const FLOAT_ZERO_MARKER = '__FLOAT_ZERO__';
1895
+ const markFloatZeros = (value, schema) => {
1896
+ if (!isPlainObject$1(schema))
1897
+ return value;
1898
+ if (schema.type === 'number' && (schema.format === 'float' || schema.format === 'double') && value === 0) {
1899
+ return FLOAT_ZERO_MARKER;
1900
+ }
1901
+ if (isPlainObject$1(value) && isPlainObject$1(schema.properties)) {
1902
+ return Object.fromEntries(Object.entries(value).map(([k, v]) => { var _a; return [k, markFloatZeros(v, (_a = schema.properties[k]) !== null && _a !== void 0 ? _a : {})]; }));
1903
+ }
1904
+ if (Array.isArray(value) && schema.items && !Array.isArray(schema.items)) {
1905
+ return value.map((item) => markFloatZeros(item, schema.items));
1906
+ }
1907
+ return value;
1908
+ };
1909
+ const stringifyWithFloatZeros = (value) => {
1910
+ var _a;
1911
+ if (value === FLOAT_ZERO_MARKER)
1912
+ return '0.0';
1913
+ return ((_a = safeStringify(value, undefined, 2)) !== null && _a !== void 0 ? _a : '').replace(/"__FLOAT_ZERO__"/g, '0.0');
1914
+ };
1849
1915
  const useGenerateExampleFromMediaTypeContent = (mediaTypeContent, chosenExampleIndex, { skipReadOnly, skipWriteOnly, skipNonRequired, ticks } = {}) => {
1850
1916
  const document = useDocument();
1851
1917
  return React__default.useMemo(() => generateExampleFromMediaTypeContent(mediaTypeContent, document, chosenExampleIndex, {
@@ -1856,7 +1922,7 @@ const useGenerateExampleFromMediaTypeContent = (mediaTypeContent, chosenExampleI
1856
1922
  }), [mediaTypeContent, document, chosenExampleIndex, skipNonRequired, skipWriteOnly, skipReadOnly, ticks]);
1857
1923
  };
1858
1924
  const generateExampleFromMediaTypeContent = (mediaTypeContent, document, chosenExampleIndex = 0, options) => {
1859
- var _a, _b;
1925
+ var _a;
1860
1926
  const textRequestBodySchema = mediaTypeContent === null || mediaTypeContent === void 0 ? void 0 : mediaTypeContent.schema;
1861
1927
  const textRequestBodyExamples = mediaTypeContent === null || mediaTypeContent === void 0 ? void 0 : mediaTypeContent.examples;
1862
1928
  try {
@@ -1865,9 +1931,12 @@ const generateExampleFromMediaTypeContent = (mediaTypeContent, document, chosenE
1865
1931
  }
1866
1932
  else if (textRequestBodySchema) {
1867
1933
  let unwrappedSchema = getResolvedObject(textRequestBodySchema);
1934
+ unwrappedSchema = stripInferredNumericBounds(unwrappedSchema);
1868
1935
  unwrappedSchema = mergeOneOfAnyOf(unwrappedSchema);
1869
1936
  const generated = Sampler.sample(unwrappedSchema, options, document);
1870
- return generated !== null ? (_b = safeStringify(generated, undefined, 2)) !== null && _b !== void 0 ? _b : '' : '';
1937
+ if (generated === null)
1938
+ return '';
1939
+ return stringifyWithFloatZeros(markFloatZeros(generated, unwrappedSchema));
1871
1940
  }
1872
1941
  }
1873
1942
  catch (e) {
@@ -1877,7 +1946,7 @@ const generateExampleFromMediaTypeContent = (mediaTypeContent, document, chosenE
1877
1946
  return '';
1878
1947
  };
1879
1948
  const generateExamplesFromJsonSchema = (schema) => {
1880
- var _a, _b;
1949
+ var _a;
1881
1950
  const examples = [];
1882
1951
  if (Array.isArray(schema === null || schema === void 0 ? void 0 : schema.examples)) {
1883
1952
  schema.examples.forEach((example, index) => {
@@ -1904,6 +1973,7 @@ const generateExamplesFromJsonSchema = (schema) => {
1904
1973
  }
1905
1974
  try {
1906
1975
  let resolvedSchema = getResolvedObject(schema);
1976
+ resolvedSchema = stripInferredNumericBounds(resolvedSchema);
1907
1977
  resolvedSchema = mergeOneOfAnyOf(resolvedSchema);
1908
1978
  const generated = Sampler.sample(resolvedSchema, {
1909
1979
  maxSampleDepth: 4,
@@ -1913,7 +1983,7 @@ const generateExamplesFromJsonSchema = (schema) => {
1913
1983
  ? [
1914
1984
  {
1915
1985
  label: 'default',
1916
- data: (_b = safeStringify(generated, undefined, 2)) !== null && _b !== void 0 ? _b : '',
1986
+ data: stringifyWithFloatZeros(markFloatZeros(generated, resolvedSchema)),
1917
1987
  },
1918
1988
  ]
1919
1989
  : [{ label: 'default', data: '' }];
package/index.js CHANGED
@@ -1867,6 +1867,72 @@ const mergeOneOfAnyOf = (schema) => {
1867
1867
  delete result.anyOf;
1868
1868
  return result;
1869
1869
  };
1870
+ const OAS_FORMAT_RANGES = {
1871
+ int32: { minimum: 0 - Math.pow(2, 31), maximum: Math.pow(2, 31) - 1 },
1872
+ int64: { minimum: Number.MIN_SAFE_INTEGER, maximum: Number.MAX_SAFE_INTEGER },
1873
+ float: { minimum: 0 - Math.pow(2, 128), maximum: Math.pow(2, 128) - 1 },
1874
+ double: { minimum: 0 - Number.MAX_VALUE, maximum: Number.MAX_VALUE },
1875
+ };
1876
+ const stripInferredNumericBounds = (schema) => {
1877
+ var _a;
1878
+ if (!json.isPlainObject(schema)) {
1879
+ return schema;
1880
+ }
1881
+ const result = Object.assign({}, schema);
1882
+ const explicitProperties = Array.isArray((_a = result === null || result === void 0 ? void 0 : result['x-stoplight']) === null || _a === void 0 ? void 0 : _a.explicitProperties)
1883
+ ? result['x-stoplight'].explicitProperties
1884
+ : [];
1885
+ const isNumericType = result.type === 'integer' || result.type === 'number';
1886
+ const formatRange = OAS_FORMAT_RANGES[result.format];
1887
+ const hasExplicitMinimum = explicitProperties.includes('minimum') && result.minimum !== (formatRange === null || formatRange === void 0 ? void 0 : formatRange.minimum);
1888
+ const hasExplicitMaximum = explicitProperties.includes('maximum') && result.maximum !== (formatRange === null || formatRange === void 0 ? void 0 : formatRange.maximum);
1889
+ if (isNumericType && !hasExplicitMinimum) {
1890
+ delete result.minimum;
1891
+ }
1892
+ if (isNumericType && !hasExplicitMaximum) {
1893
+ delete result.maximum;
1894
+ }
1895
+ if (result.properties && json.isPlainObject(result.properties)) {
1896
+ result.properties = Object.fromEntries(Object.entries(result.properties).map(([key, value]) => [key, stripInferredNumericBounds(value)]));
1897
+ }
1898
+ if (Array.isArray(result.items)) {
1899
+ result.items = result.items.map((item) => stripInferredNumericBounds(item));
1900
+ }
1901
+ else if (result.items) {
1902
+ result.items = stripInferredNumericBounds(result.items);
1903
+ }
1904
+ if (Array.isArray(result.allOf)) {
1905
+ result.allOf = result.allOf.map((item) => stripInferredNumericBounds(item));
1906
+ }
1907
+ if (Array.isArray(result.oneOf)) {
1908
+ result.oneOf = result.oneOf.map((item) => stripInferredNumericBounds(item));
1909
+ }
1910
+ if (Array.isArray(result.anyOf)) {
1911
+ result.anyOf = result.anyOf.map((item) => stripInferredNumericBounds(item));
1912
+ }
1913
+ return result;
1914
+ };
1915
+ const FLOAT_ZERO_MARKER = '__FLOAT_ZERO__';
1916
+ const markFloatZeros = (value, schema) => {
1917
+ if (!json.isPlainObject(schema))
1918
+ return value;
1919
+ if (schema.type === 'number' && (schema.format === 'float' || schema.format === 'double') && value === 0) {
1920
+ return FLOAT_ZERO_MARKER;
1921
+ }
1922
+ if (json.isPlainObject(value) && json.isPlainObject(schema.properties)) {
1923
+ return Object.fromEntries(Object.entries(value).map(([k, v]) => { var _a; return [k, markFloatZeros(v, (_a = schema.properties[k]) !== null && _a !== void 0 ? _a : {})]; }));
1924
+ }
1925
+ if (Array.isArray(value) && schema.items && !Array.isArray(schema.items)) {
1926
+ return value.map((item) => markFloatZeros(item, schema.items));
1927
+ }
1928
+ return value;
1929
+ };
1930
+ const stringifyWithFloatZeros = (value) => {
1931
+ var _a;
1932
+ if (value === FLOAT_ZERO_MARKER)
1933
+ return '0.0';
1934
+ return ((_a = json.safeStringify(value, undefined, 2)) !== null && _a !== void 0 ? _a : '').replace(/"__FLOAT_ZERO__"/g, '0.0');
1935
+ };
1870
1936
  const useGenerateExampleFromMediaTypeContent = (mediaTypeContent, chosenExampleIndex, { skipReadOnly, skipWriteOnly, skipNonRequired, ticks } = {}) => {
1871
1937
  const document = useDocument();
1872
1938
  return React.useMemo(() => generateExampleFromMediaTypeContent(mediaTypeContent, document, chosenExampleIndex, {
@@ -1877,7 +1943,7 @@ const useGenerateExampleFromMediaTypeContent = (mediaTypeContent, chosenExampleI
1877
1943
  }), [mediaTypeContent, document, chosenExampleIndex, skipNonRequired, skipWriteOnly, skipReadOnly, ticks]);
1878
1944
  };
1879
1945
  const generateExampleFromMediaTypeContent = (mediaTypeContent, document, chosenExampleIndex = 0, options) => {
1880
- var _a, _b;
1946
+ var _a;
1881
1947
  const textRequestBodySchema = mediaTypeContent === null || mediaTypeContent === void 0 ? void 0 : mediaTypeContent.schema;
1882
1948
  const textRequestBodyExamples = mediaTypeContent === null || mediaTypeContent === void 0 ? void 0 : mediaTypeContent.examples;
1883
1949
  try {
@@ -1886,9 +1952,12 @@ const generateExampleFromMediaTypeContent = (mediaTypeContent, document, chosenE
1886
1952
  }
1887
1953
  else if (textRequestBodySchema) {
1888
1954
  let unwrappedSchema = getResolvedObject(textRequestBodySchema);
1955
+ unwrappedSchema = stripInferredNumericBounds(unwrappedSchema);
1889
1956
  unwrappedSchema = mergeOneOfAnyOf(unwrappedSchema);
1890
1957
  const generated = Sampler__namespace.sample(unwrappedSchema, options, document);
1891
- return generated !== null ? (_b = json.safeStringify(generated, undefined, 2)) !== null && _b !== void 0 ? _b : '' : '';
1958
+ if (generated === null)
1959
+ return '';
1960
+ return stringifyWithFloatZeros(markFloatZeros(generated, unwrappedSchema));
1892
1961
  }
1893
1962
  }
1894
1963
  catch (e) {
@@ -1898,7 +1967,7 @@ const generateExampleFromMediaTypeContent = (mediaTypeContent, document, chosenE
1898
1967
  return '';
1899
1968
  };
1900
1969
  const generateExamplesFromJsonSchema = (schema) => {
1901
- var _a, _b;
1970
+ var _a;
1902
1971
  const examples = [];
1903
1972
  if (Array.isArray(schema === null || schema === void 0 ? void 0 : schema.examples)) {
1904
1973
  schema.examples.forEach((example, index) => {
@@ -1925,6 +1994,7 @@ const generateExamplesFromJsonSchema = (schema) => {
1925
1994
  }
1926
1995
  try {
1927
1996
  let resolvedSchema = getResolvedObject(schema);
1997
+ resolvedSchema = stripInferredNumericBounds(resolvedSchema);
1928
1998
  resolvedSchema = mergeOneOfAnyOf(resolvedSchema);
1929
1999
  const generated = Sampler__namespace.sample(resolvedSchema, {
1930
2000
  maxSampleDepth: 4,
@@ -1934,7 +2004,7 @@ const generateExamplesFromJsonSchema = (schema) => {
1934
2004
  ? [
1935
2005
  {
1936
2006
  label: 'default',
1937
- data: (_b = json.safeStringify(generated, undefined, 2)) !== null && _b !== void 0 ? _b : '',
2007
+ data: stringifyWithFloatZeros(markFloatZeros(generated, resolvedSchema)),
1938
2008
  },
1939
2009
  ]
1940
2010
  : [{ label: 'default', data: '' }];
package/index.mjs CHANGED
@@ -1846,6 +1846,72 @@ const mergeOneOfAnyOf = (schema) => {
1846
1846
  delete result.anyOf;
1847
1847
  return result;
1848
1848
  };
1849
+ const OAS_FORMAT_RANGES = {
1850
+ int32: { minimum: 0 - Math.pow(2, 31), maximum: Math.pow(2, 31) - 1 },
1851
+ int64: { minimum: Number.MIN_SAFE_INTEGER, maximum: Number.MAX_SAFE_INTEGER },
1852
+ float: { minimum: 0 - Math.pow(2, 128), maximum: Math.pow(2, 128) - 1 },
1853
+ double: { minimum: 0 - Number.MAX_VALUE, maximum: Number.MAX_VALUE },
1854
+ };
1855
+ const stripInferredNumericBounds = (schema) => {
1856
+ var _a;
1857
+ if (!isPlainObject$1(schema)) {
1858
+ return schema;
1859
+ }
1860
+ const result = Object.assign({}, schema);
1861
+ const explicitProperties = Array.isArray((_a = result === null || result === void 0 ? void 0 : result['x-stoplight']) === null || _a === void 0 ? void 0 : _a.explicitProperties)
1862
+ ? result['x-stoplight'].explicitProperties
1863
+ : [];
1864
+ const isNumericType = result.type === 'integer' || result.type === 'number';
1865
+ const formatRange = OAS_FORMAT_RANGES[result.format];
1866
+ const hasExplicitMinimum = explicitProperties.includes('minimum') && result.minimum !== (formatRange === null || formatRange === void 0 ? void 0 : formatRange.minimum);
1867
+ const hasExplicitMaximum = explicitProperties.includes('maximum') && result.maximum !== (formatRange === null || formatRange === void 0 ? void 0 : formatRange.maximum);
1868
+ if (isNumericType && !hasExplicitMinimum) {
1869
+ delete result.minimum;
1870
+ }
1871
+ if (isNumericType && !hasExplicitMaximum) {
1872
+ delete result.maximum;
1873
+ }
1874
+ if (result.properties && isPlainObject$1(result.properties)) {
1875
+ result.properties = Object.fromEntries(Object.entries(result.properties).map(([key, value]) => [key, stripInferredNumericBounds(value)]));
1876
+ }
1877
+ if (Array.isArray(result.items)) {
1878
+ result.items = result.items.map((item) => stripInferredNumericBounds(item));
1879
+ }
1880
+ else if (result.items) {
1881
+ result.items = stripInferredNumericBounds(result.items);
1882
+ }
1883
+ if (Array.isArray(result.allOf)) {
1884
+ result.allOf = result.allOf.map((item) => stripInferredNumericBounds(item));
1885
+ }
1886
+ if (Array.isArray(result.oneOf)) {
1887
+ result.oneOf = result.oneOf.map((item) => stripInferredNumericBounds(item));
1888
+ }
1889
+ if (Array.isArray(result.anyOf)) {
1890
+ result.anyOf = result.anyOf.map((item) => stripInferredNumericBounds(item));
1891
+ }
1892
+ return result;
1893
+ };
1894
+ const FLOAT_ZERO_MARKER = '__FLOAT_ZERO__';
1895
+ const markFloatZeros = (value, schema) => {
1896
+ if (!isPlainObject$1(schema))
1897
+ return value;
1898
+ if (schema.type === 'number' && (schema.format === 'float' || schema.format === 'double') && value === 0) {
1899
+ return FLOAT_ZERO_MARKER;
1900
+ }
1901
+ if (isPlainObject$1(value) && isPlainObject$1(schema.properties)) {
1902
+ return Object.fromEntries(Object.entries(value).map(([k, v]) => { var _a; return [k, markFloatZeros(v, (_a = schema.properties[k]) !== null && _a !== void 0 ? _a : {})]; }));
1903
+ }
1904
+ if (Array.isArray(value) && schema.items && !Array.isArray(schema.items)) {
1905
+ return value.map((item) => markFloatZeros(item, schema.items));
1906
+ }
1907
+ return value;
1908
+ };
1909
+ const stringifyWithFloatZeros = (value) => {
1910
+ var _a;
1911
+ if (value === FLOAT_ZERO_MARKER)
1912
+ return '0.0';
1913
+ return ((_a = safeStringify(value, undefined, 2)) !== null && _a !== void 0 ? _a : '').replace(/"__FLOAT_ZERO__"/g, '0.0');
1914
+ };
1849
1915
  const useGenerateExampleFromMediaTypeContent = (mediaTypeContent, chosenExampleIndex, { skipReadOnly, skipWriteOnly, skipNonRequired, ticks } = {}) => {
1850
1916
  const document = useDocument();
1851
1917
  return React__default.useMemo(() => generateExampleFromMediaTypeContent(mediaTypeContent, document, chosenExampleIndex, {
@@ -1856,7 +1922,7 @@ const useGenerateExampleFromMediaTypeContent = (mediaTypeContent, chosenExampleI
1856
1922
  }), [mediaTypeContent, document, chosenExampleIndex, skipNonRequired, skipWriteOnly, skipReadOnly, ticks]);
1857
1923
  };
1858
1924
  const generateExampleFromMediaTypeContent = (mediaTypeContent, document, chosenExampleIndex = 0, options) => {
1859
- var _a, _b;
1925
+ var _a;
1860
1926
  const textRequestBodySchema = mediaTypeContent === null || mediaTypeContent === void 0 ? void 0 : mediaTypeContent.schema;
1861
1927
  const textRequestBodyExamples = mediaTypeContent === null || mediaTypeContent === void 0 ? void 0 : mediaTypeContent.examples;
1862
1928
  try {
@@ -1865,9 +1931,12 @@ const generateExampleFromMediaTypeContent = (mediaTypeContent, document, chosenE
1865
1931
  }
1866
1932
  else if (textRequestBodySchema) {
1867
1933
  let unwrappedSchema = getResolvedObject(textRequestBodySchema);
1934
+ unwrappedSchema = stripInferredNumericBounds(unwrappedSchema);
1868
1935
  unwrappedSchema = mergeOneOfAnyOf(unwrappedSchema);
1869
1936
  const generated = Sampler.sample(unwrappedSchema, options, document);
1870
- return generated !== null ? (_b = safeStringify(generated, undefined, 2)) !== null && _b !== void 0 ? _b : '' : '';
1937
+ if (generated === null)
1938
+ return '';
1939
+ return stringifyWithFloatZeros(markFloatZeros(generated, unwrappedSchema));
1871
1940
  }
1872
1941
  }
1873
1942
  catch (e) {
@@ -1877,7 +1946,7 @@ const generateExampleFromMediaTypeContent = (mediaTypeContent, document, chosenE
1877
1946
  return '';
1878
1947
  };
1879
1948
  const generateExamplesFromJsonSchema = (schema) => {
1880
- var _a, _b;
1949
+ var _a;
1881
1950
  const examples = [];
1882
1951
  if (Array.isArray(schema === null || schema === void 0 ? void 0 : schema.examples)) {
1883
1952
  schema.examples.forEach((example, index) => {
@@ -1904,6 +1973,7 @@ const generateExamplesFromJsonSchema = (schema) => {
1904
1973
  }
1905
1974
  try {
1906
1975
  let resolvedSchema = getResolvedObject(schema);
1976
+ resolvedSchema = stripInferredNumericBounds(resolvedSchema);
1907
1977
  resolvedSchema = mergeOneOfAnyOf(resolvedSchema);
1908
1978
  const generated = Sampler.sample(resolvedSchema, {
1909
1979
  maxSampleDepth: 4,
@@ -1913,7 +1983,7 @@ const generateExamplesFromJsonSchema = (schema) => {
1913
1983
  ? [
1914
1984
  {
1915
1985
  label: 'default',
1916
- data: (_b = safeStringify(generated, undefined, 2)) !== null && _b !== void 0 ? _b : '',
1986
+ data: stringifyWithFloatZeros(markFloatZeros(generated, resolvedSchema)),
1917
1987
  },
1918
1988
  ]
1919
1989
  : [{ label: 'default', data: '' }];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stoplight/elements-core",
3
- "version": "9.0.24",
3
+ "version": "9.0.25-beta-0.0",
4
4
  "main": "./index.js",
5
5
  "sideEffects": [
6
6
  "web-components.min.js",