Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Commit

Permalink
Added support for OpenAPI v3 schema rendering. (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
azaslonov committed Mar 17, 2021
1 parent cc94d6d commit 3caa073
Show file tree
Hide file tree
Showing 32 changed files with 973 additions and 351 deletions.
2 changes: 1 addition & 1 deletion data/specs/bookstore.openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"servers": [
{
"url": "https://alzasloneuap05.azure-api.net/book-store-api"
"url": "https://contoso.com"
}
],
"paths": {
Expand Down
178 changes: 1 addition & 177 deletions data/specs/petstore.openapi.json

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"remark": "^13.0.0",
"remark-html": "^13.0.1",
"routing-controllers": "^0.9.0-alpha.6",
"saxen": "^8.1.2",
"slick": "^1.12.2",
"topojson-client": "^3.1.0",
"truncate-html": "^1.0.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export class OperationDetails {
.map(p => p.typeName)
.filter((item, pos, self) => self.indexOf(item) === pos);

const schemasPromises = schemaIds.map(schemaId => this.apiService.getApiSchema(`${apiId}/${schemaId}`));
const schemasPromises = schemaIds.map(schemaId => this.apiService.getApiSchema(this.selectedApiName()));
const schemas = await Promise.all(schemasPromises);
const definitions = schemas.map(x => x.definitions).flat();

Expand Down
16 changes: 16 additions & 0 deletions src/contracts/openapi/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export * from "./openApiComponents";
export * from "./openApiExample";
export * from "./openApiExternalDoc";
export * from "./openApiMediaType";
export * from "./openApiObjectInfo";
export * from "./openApiOperation";
export * from "./openApiParameter";
export * from "./openApiPath";
export * from "./openApiPaths";
export * from "./openApiReference";
export * from "./openApiRequestBody";
export * from "./openApiResponse";
export * from "./openApiResponses";
export * from "./openApiSchema";
export * from "./openApiServer";
export * from "./openApiTag";
50 changes: 50 additions & 0 deletions src/contracts/openapi/openApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
OpenApiComponents,
OpenApiExternalDoc,
OpenApiObjectInfo,
OpenApiPaths,
OpenApiServer,
OpenApiTag
} from "./";

/**
* This is the root document object of the
*/
export interface OpenApiSpec30 {
/**
* This string MUST be the semantic version number of the OpenAPI Specification version that the OpenAPI document uses.
*/
openapi: string;

/**
* The available paths and operations for the API.
*/
paths: OpenApiPaths;

/**
* Provides metadata about the API. The metadata MAY be used by tooling as required.
*/
info: OpenApiObjectInfo;

/**
* Additional external documentation.
*/
externalDocs?: OpenApiExternalDoc;

/**
* An array of Server Objects, which provide connectivity information to a target server.
* If the servers property is not provided, or is an empty array, the default value would
* be a Server Object with a URL value of /.
*/
servers?: OpenApiServer[];

/**
* A list of tags used by the specification with additional metadata.
*/
tags?: OpenApiTag[];

/**
* An element to hold various schemas for the specification.
*/
components: OpenApiComponents;
}
3 changes: 3 additions & 0 deletions src/contracts/openapi/openApiComponents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface OpenApiComponents {
schemas?: any;
}
21 changes: 21 additions & 0 deletions src/contracts/openapi/openApiExample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export interface OpenApiExample {
/**
* Short description for the example.
*/
summary: string;

/**
* Long description for the example.
*/
description: string;

/**
* Embedded literal example. The value field and externalValue field are mutually exclusive. To represent examples of media types that cannot naturally represented in JSON or YAML, use a string value to contain the example, escaping where necessary.
*/
value: any;

/**
* A URL that points to the literal example. This provides the capability to reference examples that cannot easily be included in JSON or YAML documents. The value field and externalValue field are mutually exclusive.
*/
externalValue: string;
}
4 changes: 4 additions & 0 deletions src/contracts/openapi/openApiExternalDoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface OpenApiExternalDoc {
url: string;
description?: string;
}
9 changes: 9 additions & 0 deletions src/contracts/openapi/openApiMediaType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { OpenApiExample } from "./openApiExample";


export interface OpenApiMediaType {
schema: any; // OpenApiSchema | OpenApiReference;
example: OpenApiExample;
examples: any;
encoding: any;
}
6 changes: 6 additions & 0 deletions src/contracts/openapi/openApiObjectInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface OpenApiObjectInfo {
title: string;
description?: string;
version: string;
termsOfService?: string;
}
15 changes: 15 additions & 0 deletions src/contracts/openapi/openApiOperation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { OpenApiParameter } from "./openApiParameter";
import { OpenApiResponses } from "./openApiResponses";
import { OpenApiRequestBody } from "./openApiRequestBody";


export interface OpenApiOperation {
operationId: string;
description: string;
parameters: OpenApiParameter[];
responses: OpenApiResponses;
summary: string;
consumes?: string[];
produces?: string[];
requestBody?: OpenApiRequestBody;
}
10 changes: 10 additions & 0 deletions src/contracts/openapi/openApiParameter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface OpenApiParameter {
name: string;
in: string;
required: boolean;
description: string;
type?: string;
schema?: any;
default?: string;
enum: string[];
}
6 changes: 6 additions & 0 deletions src/contracts/openapi/openApiPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { OpenApiOperation } from "./openApiOperation";


export interface OpenApiPath {
[key: string]: OpenApiOperation;
}
5 changes: 5 additions & 0 deletions src/contracts/openapi/openApiPaths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { OpenApiPath } from "./openApiPath";

export interface OpenApiPaths {
[key: string]: OpenApiPath;
}
3 changes: 3 additions & 0 deletions src/contracts/openapi/openApiReference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface OpenApiReference {
$ref: string;
}
24 changes: 24 additions & 0 deletions src/contracts/openapi/openApiRequestBody.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Bag } from "@paperbits/common";
import { OpenApiMediaType } from "./openApiMediaType";

/**
* Describes a single request body.
*/
export interface OpenApiRequestBody {
/**
* A brief description of the request body. This could contain examples of use.
*/
description: string;

/**
* Determines if the request body is required in the request. Defaults to false.
*/
required: boolean;

/**
* The content of the request body. The key is a media type or media type range and the value describes it.
* For requests that match multiple keys, only the most specific key is applicable,
* e.g. text/plain overrides text/*
*/
content: Bag<OpenApiMediaType>;
}
10 changes: 10 additions & 0 deletions src/contracts/openapi/openApiResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Bag } from "@paperbits/common";
import { OpenApiMediaType } from "./openApiMediaType";
import { OpenApiParameter } from "./openApiParameter";


export interface OpenApiResponse {
description: string;
headers: Bag<OpenApiParameter>;
content: Bag<OpenApiMediaType>;
}
5 changes: 5 additions & 0 deletions src/contracts/openapi/openApiResponses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { OpenApiResponse } from "./openApiResponse";

export interface OpenApiResponses {
[key: string]: OpenApiResponse;
}
1 change: 1 addition & 0 deletions src/contracts/openapi/openApiSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export interface OpenApiSchema { }
5 changes: 5 additions & 0 deletions src/contracts/openapi/openApiServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

export interface OpenApiServer {
url: string;
description?: string;
}
8 changes: 8 additions & 0 deletions src/contracts/openapi/openApiTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { OpenApiExternalDoc } from "./openApiExternalDoc";


export interface OpenApiTag {
name: string;
description?: string;
externalDocs?: OpenApiExternalDoc;
}
12 changes: 6 additions & 6 deletions src/contracts/request.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ParameterContract } from "./parameter";
import { RepresentationContract } from "./representation";

/*
Model of API operation request
*/
/**
* Model of API operation request
*/
export interface RequestContract {
description?: string;
queryParameters: ParameterContract[];
headers: ParameterContract[];
representations: RepresentationContract[];
queryParameters?: ParameterContract[];
headers?: ParameterContract[];
representations?: RepresentationContract[];
}
32 changes: 29 additions & 3 deletions src/contracts/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export interface SchemaObjectContract extends ReferenceObjectContract {
*/
required?: string[];

readOnly?: boolean;

properties?: Bag<SchemaObjectContract>;

items?: SchemaObjectContract;
Expand Down Expand Up @@ -72,7 +74,25 @@ export interface SchemaObjectContract extends ReferenceObjectContract {

minProperties?: number;

/**
* Example of the payload represented by this schema object.
*/
example?: string;

/**
* Format of payload example represented by this schema object. It is used for syntax highlighting.
*/
exampleFormat?: string;

/**
* Raw schema representation.
*/
rawSchema?: string;

/**
* Raw schema format. It is used for syntax highlighting.
*/
rawSchemaFormat?: string;
}

/**
Expand Down Expand Up @@ -122,17 +142,23 @@ export interface OpenApiSchemaContract {
};
}

export interface XsdSchemaContract {
value: string;
}


/**
*
*/
export interface SchemaContract extends ArmResource {
export interface SchemaContract extends ArmResource {
properties: {
contentType: string;
document?: SwaggerSchemaContract | OpenApiSchemaContract;
document?: SwaggerSchemaContract | OpenApiSchemaContract | XsdSchemaContract;
};
}

export enum SchemaType {
swagger = "application/vnd.ms-azure-apim.swagger.definitions+json",
openapi = "application/vnd.oai.openapi.components+json"
openapi = "application/vnd.oai.openapi.components+json",
xsd = "application/vnd.ms-azure-apim.xsd+xml"
}
Loading

0 comments on commit 3caa073

Please sign in to comment.