stoplightio / json-schema-viewer

A JSON Schema viewer React component
Apache License 2.0
175 stars 37 forks source link

Add support for rendering vendor extensions #236

Closed weyert closed 7 months ago

weyert commented 1 year ago

I would like to purpose the ability to render or visualise vendor extensions within the JSON Schema Viewer.

Context

I have vendor extensions which I want to visualise or show up in the schema definition

Current Behavior

No ability to display vendor extensions

Expected Behavior

I want to have function similar to renderRowAddon to allow render vendor extensions

Possible Solution(s)

I have been working on implementing a function named renderExtensionAddon with the signature:

({ schemaNode: SchemaNode; nestingLevel: number; vendorExtensions: Record<string, unknown>}) => React.ReactNode

As you can see the function will return a React node back which can be rendered by the component, and the responsibility for returning the component is off loaded to the consumer of JsonSchemaViewer.

This would be used by the TopLevelSchemaRow and SchemaRow components, in a way, such as:

// Add around line 40-41
{hasVendorProperties && renderExtensionAddon
          ? renderExtensionAddon({ schemaNode, nestingLevel, vendorExtensions })
          : null}

Note: Currently, there is no way to easily extract vendor extensions from a schema node, only way is to grab it from fragment-property and then get all properties prefixed with x-.

In the SchemaRow-component a similar code can be added as above:

// Add around line 180-181
{hasVendorProperties && renderExtensionAddon ? (
              <Box>{renderExtensionAddon({ schemaNode, nestingLevel, vendorExtensions })}</Box>
            ) : null}

A potential implementation of the renderExtensionAddon could be:

/**
 * @private
 * A helper function to render vendor extensions of an OpenaPI schema definition
 */
export function renderExtensions({ schemaNode, nestingLevel, vendorExtensions }: ExtensionRowProps) {
  // Avoid rendering vendor extensions for the zero nesting level or root of a schema
  if (nestingLevel === 0) {
    return null;
  }
  return <VendorExtensions node={schemaNode} vendorExtensions={vendorExtensions} />;
}

I am happy to raise a pull request for these changes. I have been using it in a fork of the component and so far it works as expected.

weyert commented 1 year ago

@P0lip @marbemac Sorry, for bugging you. I would love to contribute this functionality. I already got the appropriate approvals from my work to contribute. Would it be possible to discuss my proposal?

P0lip commented 1 year ago

@falsaffa @mnaumanali94 thoughts? ^

weyert commented 1 year ago

@paulatulis @P0lip @falsaffa @mnaumanali94 Any feedback? Do you dislike my approach? Shall I keep my own fork instead of merge it upstream?

weyert commented 11 months ago

@P0lip Just checkin in if there is still interest

carlo-quinonez commented 11 months ago

This would be a great addition. We have several custom keywords that we would like to have displayed on the docs.

weyert commented 10 months ago

Created a PR: https://github.com/stoplightio/json-schema-viewer/pull/245

stoplight-bot commented 7 months ago

:tada: This issue has been resolved in version 4.16.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: