-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create PoC for generating model docs from open API JSON (#342)
- Loading branch information
Showing
17 changed files
with
42,965 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,5 @@ ts/ | |
!.yarn/versions | ||
docusaurus/video/docusaurus/shared | ||
node_modules/.yarn-integrity | ||
yarn.lock | ||
|
||
node_modules |
48 changes: 48 additions & 0 deletions
48
docusaurus/video/docusaurus/docs/api/_common_/CallEventModels.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import React from 'react'; | ||
import OpenApiModels from './OpenApiModels'; | ||
import apiJson from '../video-client-openapi.json'; | ||
|
||
const filter = (apiJson) => | ||
Object.keys(apiJson.components.schemas).filter( | ||
(key) => | ||
apiJson.components.schemas[key]['x-stream-event-call-type'] === true, | ||
); | ||
|
||
const events = filter(apiJson).map((key) => { | ||
const type = apiJson.components.schemas[key].properties.type.default || '-'; | ||
const description = apiJson.components.schemas[key].description || '-'; | ||
|
||
return { key, type, description }; | ||
}); | ||
|
||
events.sort((e1, e2) => (e1.type < e2.type ? -1 : e1.type > e2.type ? 1 : 0)); | ||
|
||
const CallEventModels = () => { | ||
return ( | ||
<React.Fragment> | ||
<table> | ||
<thead> | ||
<th>Name</th> | ||
<th>Description</th> | ||
</thead> | ||
{events.map((event) => ( | ||
<tr> | ||
<td> | ||
<a href={'#' + event.key}> | ||
<code>{event.type}</code> | ||
</a> | ||
</td> | ||
<td>{event.description}</td> | ||
</tr> | ||
))} | ||
</table> | ||
<OpenApiModels | ||
modelFilter={filter} | ||
recursive={true} | ||
apiJson={apiJson} | ||
></OpenApiModels> | ||
</React.Fragment> | ||
); | ||
}; | ||
|
||
export default CallEventModels; |
50 changes: 50 additions & 0 deletions
50
docusaurus/video/docusaurus/docs/api/_common_/OpenApiModels.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React from 'react'; | ||
import { parseModel } from './open-api-model-parser'; | ||
|
||
const OpenApiModels = ({ modelName, modelFilter, recursive = true, apiJson }) => { | ||
|
||
const models = React.useMemo(() => { | ||
if (!modelName && !modelFilter) { | ||
return []; | ||
} | ||
return parseModel({modelName, modelFilter, recursive, apiJson}); | ||
}, [modelName, modelFilter]); | ||
|
||
return ( | ||
<div> | ||
{models.map((model) => ( | ||
<React.Fragment key={model.name}> | ||
<h2 id={model.name}>{model.name}</h2> | ||
<table data-testid={model.name + '-table'}> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>Type</th> | ||
<th>Description</th> | ||
<th>Constraints</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{model.properties.map(p => { | ||
return ( | ||
<tr key={model.name + p.name}> | ||
<td data-testid={model.name + '-' + p.name + '-name'}> | ||
<code>{p.name}</code> | ||
</td> | ||
<td data-testid={model.name + '-' + p.name + '-type'}> | ||
{p.type.definitionLink ? <a data-testid={model.name + '-' + p.name + '-typelink'} href={p.type.definitionLink}><code>{p.type.formattedName}</code></a> : <code>{p.type.formattedName}</code>} | ||
</td> | ||
<td data-testid={model.name + '-' + p.name + '-description'}>{p.description || '-'}</td> | ||
<td data-testid={model.name + '-' + p.name + '-constraints'}>{p.constraints.join(', ') || '-'}</td> | ||
</tr> | ||
); | ||
})} | ||
</tbody> | ||
</table> | ||
</React.Fragment> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
export default OpenApiModels; |
88 changes: 88 additions & 0 deletions
88
docusaurus/video/docusaurus/docs/api/_common_/open-api-model-parser.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import videoApiJson from '../video-openapi.json'; | ||
|
||
export const parseModel = ({ modelName, modelFilter, recursive = true, apiJson = videoApiJson }) => { | ||
const models = modelName ? [{name: modelName, properties: []}] : modelFilter(apiJson).map(_modelName => ({name: _modelName, properties: []})); | ||
|
||
for (let i = 0; i < models.length; i++) { | ||
const model = models[i]; | ||
|
||
const schemaJson = apiJson.components.schemas[model.name]; | ||
const propertiesJson = schemaJson.properties; | ||
|
||
model.properties = Object.keys(propertiesJson).map(propertyName => { | ||
const property = propertiesJson[propertyName]; | ||
|
||
const type = parsePropertyType(property); | ||
|
||
// enums are not yet parsed | ||
const isEnum = apiJson.components.schemas[type.name] && !apiJson.components.schemas[type.name].properties; | ||
const shouldDisplayTypeDefLink = recursive && !isEnum; | ||
|
||
if (recursive && !isEnum && apiJson.components.schemas[type.name] && !models.find(r => r.name === type.name)) { | ||
models.push({name: type.name, properties: []}); | ||
} | ||
|
||
const description = parsePropertyDescription(property); | ||
|
||
const isRequired = schemaJson.required?.includes(propertyName); | ||
const constraints = parsePropertyConstraints(property, isRequired); | ||
|
||
|
||
return { | ||
name: propertyName, | ||
type: {...type, definitionLink: shouldDisplayTypeDefLink ? type.definitionLink : undefined}, | ||
constraints, | ||
description | ||
} | ||
}); | ||
} | ||
|
||
return models; | ||
} | ||
|
||
export const parsePropertyType = (property) => { | ||
let name; | ||
let definitionLink; | ||
let formattedName; | ||
let isArray = property.type === 'array'; | ||
if (property.$ref || isArray && property.items?.$ref) { | ||
const ref = isArray ? property.items?.$ref : property.$ref; | ||
// Example $ref: #/components/schemas/EdgeResponse | ||
name = ref?.split('/')?.pop() || ''; | ||
definitionLink = `#${name}`; | ||
} else { | ||
name = (isArray ? property.items?.type : property.type) || '' | ||
} | ||
|
||
formattedName = name; | ||
|
||
if (property.enum || isArray && property.items.enum) { | ||
const enumValues = isArray ? property.items.enum : property.enum; | ||
formattedName += ` (${enumValues.join(', ')})` | ||
} | ||
|
||
formattedName = isArray ? `${formattedName}[]` : formattedName; | ||
|
||
return { | ||
name, definitionLink, formattedName, isArray | ||
} | ||
} | ||
|
||
export const parsePropertyDescription = (property) => { | ||
return property.description; | ||
} | ||
|
||
export const parsePropertyConstraints = (property, required) => { | ||
const constraints = []; | ||
if (required) { | ||
constraints.push('Required'); | ||
} | ||
if (property.minimum !== undefined) { | ||
constraints.push(`Minimum: ${property.minimum}`) | ||
} | ||
if (property.maximum !== undefined) { | ||
constraints.push(`Maximum: ${property.maximum}`) | ||
} | ||
|
||
return constraints; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.