Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: update to new parser api #746

Merged
merged 10 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions library/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ module.exports = {
tsconfig: 'tsconfig.json',
},
},
"moduleNameMapper": {
"^nimma/legacy$": "<rootDir>/../node_modules/nimma/dist/legacy/cjs/index.js",
"^nimma/(.*)": "<rootDir>/../node_modules/nimma/dist/cjs/$1"
}
};
14,713 changes: 1,444 additions & 13,269 deletions library/package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@
"get:version": "echo $npm_package_version"
},
"dependencies": {
"@asyncapi/avro-schema-parser": "^1.1.0",
"@asyncapi/openapi-schema-parser": "^2.0.1",
"@asyncapi/protobuf-schema-parser": "^1.0.0",
"@asyncapi/parser": "^1.18.0",
"@asyncapi/avro-schema-parser": "3.0.3",
"@asyncapi/openapi-schema-parser": "3.0.4",
"@asyncapi/protobuf-schema-parser": "1.0.0",
"@asyncapi/parser": "^3.0.0-next-major-spec.1",
"highlight.js": "^10.7.2",
"isomorphic-dompurify": "^0.13.0",
"marked": "^4.0.14",
Expand Down
64 changes: 31 additions & 33 deletions library/src/components/Schema.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { useState, useEffect, useContext } from 'react';
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
import { Schema as SchemaType } from '@asyncapi/parser';
import { SchemaInterface } from '@asyncapi/parser';

import { Href, CollapseButton, Markdown, Extensions } from './index';
import { SchemaHelpers } from '../helpers';

interface Props {
schemaName?: React.ReactNode;
schema?: SchemaType;
schema?: SchemaInterface;
required?: boolean;
isPatternProperty?: boolean;
isProperty?: boolean;
Expand Down Expand Up @@ -58,18 +58,23 @@ export const Schema: React.FunctionComponent<Props> = ({
const constraints = SchemaHelpers.humanizeConstraints(schema);
const externalDocs = schema.externalDocs();

const renderType = schema.ext(SchemaHelpers.extRenderType) !== false;
const rawValue = schema.ext(SchemaHelpers.extRawValue) === true;
const parameterLocation = schema.ext(SchemaHelpers.extParameterLocation);
const isExpandable = SchemaHelpers.isExpandable(schema) || dependentSchemas;
const renderTypeExt = schema.extensions().get(SchemaHelpers.extRenderType);
const renderType = renderTypeExt !== undefined && renderTypeExt.value() !== false;

const rawValueExt = schema.extensions().get(SchemaHelpers.extRawValue);
const rawValue = rawValueExt !== undefined && rawValueExt.value() === true;

const parameterLocationExt = schema.extensions().get(SchemaHelpers.extParameterLocation);
const parameterLocation = parameterLocationExt !== undefined && parameterLocationExt.value() === true;

let schemaType = SchemaHelpers.toSchemaType(schema);
const isExpandable = SchemaHelpers.isExpandable(schema) || dependentSchemas;

isCircular =
isCircular ||
schema.isCircular() ||
schema.ext('x-parser-circular') ||
false;
const uid = schema.uid();
const uid = schema.$id();

const schemaItems = schema.items();
if (schemaItems && !Array.isArray(schemaItems)) {
Expand All @@ -83,14 +88,13 @@ export const Schema: React.FunctionComponent<Props> = ({
isCircular =
isCircular ||
schemaItems.isCircular() ||
schemaItems.ext('x-parser-circular') ||
false;
if (
isCircular &&
typeof (schemaItems as any).circularSchema === 'function'
) {
schemaType = SchemaHelpers.toSchemaType(
(schemaItems as any).circularSchema(),
schemaItems
);
}
} else if (
Expand Down Expand Up @@ -222,7 +226,7 @@ export const Schema: React.FunctionComponent<Props> = ({
)}
</div>

{schema.hasDescription() && (
{schema.description() !== undefined && (
<div>
<Markdown>{schema.description()}</Markdown>
</div>
Expand All @@ -247,7 +251,7 @@ export const Schema: React.FunctionComponent<Props> = ({
{schema.enum() && (
<ul className="text-xs">
Allowed values:{' '}
{schema.enum().map((e, idx) => (
{schema.enum()?.map((e, idx) => (
<li
key={idx}
className="border inline-block text-orange-600 rounded ml-1 py-0 px-2"
Expand Down Expand Up @@ -278,7 +282,7 @@ export const Schema: React.FunctionComponent<Props> = ({
{schema.examples() && (
<ul className="text-xs">
Examples values:{' '}
{schema.examples().map((e, idx) => (
{schema.examples()?.map((e, idx) => (
<li
key={idx}
className="border inline-block text-orange-600 rounded ml-1 py-0 px-2 break-all"
Expand All @@ -304,8 +308,7 @@ export const Schema: React.FunctionComponent<Props> = ({

{schema.oneOf() &&
schema
.oneOf()
.map((s, idx) => (
.oneOf()?.map((s, idx) => (
<Schema
key={idx}
schema={s}
Expand All @@ -314,8 +317,7 @@ export const Schema: React.FunctionComponent<Props> = ({
))}
{schema.anyOf() &&
schema
.anyOf()
.map((s, idx) => (
.anyOf()?.map((s, idx) => (
<Schema
key={idx}
schema={s}
Expand All @@ -324,8 +326,7 @@ export const Schema: React.FunctionComponent<Props> = ({
))}
{schema.allOf() &&
schema
.allOf()
.map((s, idx) => (
.allOf()?.map((s, idx) => (
<Schema
key={idx}
schema={s}
Expand Down Expand Up @@ -381,7 +382,7 @@ export const Schema: React.FunctionComponent<Props> = ({
};

interface SchemaPropertiesProps {
schema: SchemaType;
schema: SchemaInterface;
}

const SchemaProperties: React.FunctionComponent<SchemaPropertiesProps> = ({
Expand All @@ -394,7 +395,7 @@ const SchemaProperties: React.FunctionComponent<SchemaPropertiesProps> = ({

const required = schema.required() || [];
const patternProperties = schema.patternProperties();
const circularProps = schema.ext('x-parser-circular-props') || [];
const circularProps = schema.extensions().get('x-parser-circular-props')?.value() || [];

return (
<>
Expand All @@ -412,7 +413,7 @@ const SchemaProperties: React.FunctionComponent<SchemaPropertiesProps> = ({
key={propertyName}
/>
))}
{Object.entries(patternProperties).map(([propertyName, property]) => (
{Object.entries(patternProperties || {}).map(([propertyName, property]) => (
<Schema
schema={property}
schemaName={propertyName}
Expand All @@ -427,19 +428,18 @@ const SchemaProperties: React.FunctionComponent<SchemaPropertiesProps> = ({
};

interface AdditionalPropertiesProps {
schema: SchemaType;
schema: SchemaInterface;
}

const AdditionalProperties: React.FunctionComponent<AdditionalPropertiesProps> = ({
schema,
}) => {
if (schema.ext(SchemaHelpers.extRenderAdditionalInfo) === false) {
if (schema.extensions().get(SchemaHelpers.extRenderAdditionalInfo)?.value() === false) {
return null;
}

let type = schema.type();
type = Array.isArray(type) ? type : [type];
if (!type.includes('object')) {
if (type === undefined || !type.includes('object')) {
return null;
}

Expand All @@ -464,13 +464,12 @@ const AdditionalProperties: React.FunctionComponent<AdditionalPropertiesProps> =
};

interface SchemaItemsProps {
schema: SchemaType;
schema: SchemaInterface;
}

const SchemaItems: React.FunctionComponent<SchemaItemsProps> = ({ schema }) => {
let type = schema.type();
type = Array.isArray(type) ? type : [type];
if (!type.includes('array')) {
if (type === undefined || !type.includes('array')) {
return null;
}
const items = schema.items();
Expand All @@ -495,19 +494,18 @@ const SchemaItems: React.FunctionComponent<SchemaItemsProps> = ({ schema }) => {
};

interface AdditionalItemsProps {
schema: SchemaType;
schema: SchemaInterface;
}

const AdditionalItems: React.FunctionComponent<AdditionalItemsProps> = ({
schema,
}) => {
if (schema.ext(SchemaHelpers.extRenderAdditionalInfo) === false) {
if (schema.extensions().get(SchemaHelpers.extRenderAdditionalInfo)?.value() === false) {
return null;
}

let type = schema.type();
type = Array.isArray(type) ? type : [type];
if (!type.includes('array')) {
if (type === undefined || !type.includes('array')) {
return null;
}
if (!Array.isArray(schema.items())) {
Expand Down
4 changes: 2 additions & 2 deletions library/src/components/Tag.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';
import { Tag as TagType } from '@asyncapi/parser';
import { TagInterface } from '@asyncapi/parser';

import { Href } from './Href';

interface Props {
tag: TagType;
tag: TagInterface;
}

export const Tag: React.FunctionComponent<Props> = ({ tag }) => {
Expand Down
4 changes: 2 additions & 2 deletions library/src/components/Tags.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';
import { Tag as TagType } from '@asyncapi/parser';
import { TagInterface } from '@asyncapi/parser';

import { Tag } from './Tag';

interface Props {
tags?: TagType[];
tags?: TagInterface[];
}

export const Tags: React.FunctionComponent<Props> = ({ tags }) => {
Expand Down
2 changes: 1 addition & 1 deletion library/src/components/__tests__/Extensions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React from 'react';
import { render, screen } from '@testing-library/react';

// @ts-ignore
import SchemaModel from '@asyncapi/parser/lib/models/schema';
import {SchemaV2 as SchemaModel } from '@asyncapi/parser';

import { Extensions } from '../Extensions';

Expand Down
12 changes: 5 additions & 7 deletions library/src/components/__tests__/Schema.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

import React from 'react';
import { render, screen } from '@testing-library/react';

// @ts-ignore
import SchemaModel from '@asyncapi/parser/lib/models/schema';
import {SchemaV2 as SchemaModel } from '@asyncapi/parser';

import { Schema } from '../Schema';

Expand All @@ -24,7 +22,7 @@ describe('Schema component', () => {
};
schema.properties.circular = schema;
schema.properties.circularTwo = schema;
const schemaModel = new SchemaModel(schema);
const schemaModel = new SchemaModel(schema as any);

render(<Schema schema={schemaModel} />);

Expand All @@ -51,7 +49,7 @@ describe('Schema component', () => {
schema.properties.circular = schema;
schema.properties.circularTwo = schema;
schema['x-parser-circular-props'] = ['circular', 'circularTwo'];
const schemaModel = new SchemaModel(schema);
const schemaModel = new SchemaModel(schema as any);

render(<Schema schema={schemaModel} />);

Expand Down Expand Up @@ -79,7 +77,7 @@ describe('Schema component', () => {
},
},
};
const schemaModel = new SchemaModel(schema);
const schemaModel = new SchemaModel(schema as any);

render(<Schema schema={schemaModel} />);

Expand All @@ -101,7 +99,7 @@ describe('Schema component', () => {
},
},
};
const schemaModel = new SchemaModel(schema);
const schemaModel = new SchemaModel(schema as any);

render(<Schema schema={schemaModel} />);

Expand Down
4 changes: 2 additions & 2 deletions library/src/containers/AsyncApi/AsyncApi.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Component } from 'react';
import { AsyncAPIDocument } from '@asyncapi/parser';
import { AsyncAPIDocumentInterface } from '@asyncapi/parser';

import AsyncApiStandalone from './Standalone';

Expand All @@ -17,7 +17,7 @@ export interface AsyncApiProps {
}

interface AsyncAPIState {
asyncapi?: AsyncAPIDocument;
asyncapi?: AsyncAPIDocumentInterface;
error?: ErrorObject;
}

Expand Down
4 changes: 2 additions & 2 deletions library/src/containers/AsyncApi/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from 'react';
import { AsyncAPIDocument } from '@asyncapi/parser';
import { AsyncAPIDocumentInterface } from '@asyncapi/parser';
import useResizeObserver from 'use-resize-observer';

import { Sidebar } from '../Sidebar/Sidebar';
Expand All @@ -15,7 +15,7 @@ import { SpecificationContext, ConfigContext } from '../../contexts';
import { ErrorObject } from '../../types';

interface Props {
asyncapi: AsyncAPIDocument;
asyncapi: AsyncAPIDocumentInterface;
config: ConfigInterface;
error?: ErrorObject;
}
Expand Down
4 changes: 2 additions & 2 deletions library/src/containers/AsyncApi/Standalone.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Component } from 'react';
import { AsyncAPIDocument } from '@asyncapi/parser';
import { AsyncAPIDocumentInterface } from '@asyncapi/parser';

import { SpecificationHelpers } from '../../helpers';
import { ErrorObject, PropsSchema } from '../../types';
Expand All @@ -15,7 +15,7 @@ export interface AsyncApiProps {
}

interface AsyncAPIState {
asyncapi?: AsyncAPIDocument;
asyncapi?: AsyncAPIDocumentInterface;
error?: ErrorObject;
}

Expand Down
Loading