import handleJson from "./handleJson.js"; import type { OpenAPIV2 } from "openapi-types"; import type { Field } from "../Field.js"; const swaggerApiDefinition: OpenAPIV2.Document = { swagger: "2.0", basePath: "/", info: { title: "API Platform's demo", version: "0.0.0", description: "This is a demo application of the [API Platform](https://api-platform.com) framework.\n[Its source code](https://github.com/api-platform/demo) includes various examples, check it out!\n", }, paths: { "/books": { get: { tags: ["Book"], operationId: "getBookCollection", produces: [ "application/ld+json", "application/hal+json", "application/xml", "text/xml", "application/json", "application/x-yaml", "text/csv", "text/html", ], summary: "Retrieves the collection of Book resources.", responses: { "200": { description: "Book collection response", schema: { type: "array", items: { $ref: "#/definitions/Book" }, }, }, }, }, post: { tags: ["Book"], operationId: "postBookCollection", consumes: [ "application/ld+json", "application/hal+json", "application/xml", "text/xml", "application/json", "application/x-yaml", "text/csv", "text/html", ], produces: [ "application/ld+json", "application/hal+json", "application/xml", "text/xml", "application/json", "application/x-yaml", "text/csv", "text/html", ], summary: "Creates a Book resource.", parameters: [ { name: "book", in: "body", description: "The new Book resource", schema: { $ref: "#/definitions/Book" }, }, ], responses: { "201": { description: "Book resource created", schema: { $ref: "#/definitions/Book" }, }, "400": { description: "Invalid input" }, "404": { description: "Resource not found" }, }, }, }, "/books/{id}": { get: { tags: ["Book"], operationId: "getBookItem", produces: [ "application/ld+json", "application/hal+json", "application/xml", "text/xml", "application/json", "application/x-yaml", "text/csv", "text/html", ], summary: "Retrieves a Book resource.", parameters: [ { name: "id", in: "path", required: true, type: "integer" }, ], responses: { "200": { description: "Book resource response", schema: { $ref: "#/definitions/Book" }, }, "404": { description: "Resource not found" }, }, }, put: { tags: ["Book"], operationId: "putBookItem", consumes: [ "application/ld+json", "application/hal+json", "application/xml", "text/xml", "application/json", "application/x-yaml", "text/csv", "text/html", ], produces: [ "application/ld+json", "application/hal+json", "application/xml", "text/xml", "application/json", "application/x-yaml", "text/csv", "text/html", ], summary: "Replaces the Book resource.", parameters: [ { name: "id", in: "path", type: "integer", required: true }, { name: "book", in: "body", description: "The updated Book resource", schema: { $ref: "#/definitions/Book" }, }, ], responses: { "200": { description: "Book resource updated", schema: { $ref: "#/definitions/Book" }, }, "400": { description: "Invalid input" }, "404": { description: "Resource not found" }, }, }, delete: { tags: ["Book"], operationId: "deleteBookItem", summary: "Removes the Book resource.", responses: { "204": { description: "Book resource deleted" }, "404": { description: "Resource not found" }, }, parameters: [ { name: "id", in: "path", type: "integer", required: true }, ], }, }, "/reviews": { get: { tags: ["Review"], operationId: "getReviewCollection", produces: [ "application/ld+json", "application/hal+json", "application/xml", "text/xml", "application/json", "application/x-yaml", "text/csv", "text/html", ], summary: "Retrieves the collection of Review resources.", responses: { "200": { description: "Review collection response", schema: { type: "array", items: { $ref: "#/definitions/Review" }, }, }, }, }, post: { tags: ["Review"], operationId: "postReviewCollection", consumes: [ "application/ld+json", "application/hal+json", "application/xml", "text/xml", "application/json", "application/x-yaml", "text/csv", "text/html", ], produces: [ "application/ld+json", "application/hal+json", "application/xml", "text/xml", "application/json", "application/x-yaml", "text/csv", "text/html", ], summary: "Creates a Review resource.", parameters: [ { name: "review", in: "body", description: "The new Review resource", schema: { $ref: "#/definitions/Review" }, }, ], responses: { "201": { description: "Review resource created", schema: { $ref: "#/definitions/Review" }, }, "400": { description: "Invalid input" }, "404": { description: "Resource not found" }, }, }, }, "/reviews/{id}": { get: { tags: ["Review"], operationId: "getReviewItem", produces: [ "application/ld+json", "application/hal+json", "application/xml", "text/xml", "application/json", "application/x-yaml", "text/csv", "text/html", ], summary: "Retrieves a Review resource.", parameters: [ { name: "id", in: "path", required: true, type: "integer" }, ], responses: { "200": { description: "Review resource response", schema: { $ref: "#/definitions/Review" }, }, "404": { description: "Resource not found" }, }, }, put: { tags: ["Review"], operationId: "putReviewItem", consumes: [ "application/ld+json", "application/hal+json", "application/xml", "text/xml", "application/json", "application/x-yaml", "text/csv", "text/html", ], produces: [ "application/ld+json", "application/hal+json", "application/xml", "text/xml", "application/json", "application/x-yaml", "text/csv", "text/html", ], summary: "Replaces the Review resource.", parameters: [ { name: "id", in: "path", type: "integer", required: true }, { name: "review", in: "body", description: "The updated Review resource", schema: { $ref: "#/definitions/Review" }, }, ], responses: { "200": { description: "Review resource updated", schema: { $ref: "#/definitions/Review" }, }, "400": { description: "Invalid input" }, "404": { description: "Resource not found" }, }, }, delete: { tags: ["Review"], operationId: "deleteReviewItem", summary: "Removes the Review resource.", responses: { "204": { description: "Review resource deleted" }, "404": { description: "Resource not found" }, }, parameters: [ { name: "id", in: "path", type: "integer", required: true }, ], }, }, }, definitions: { Book: { type: "object", externalDocs: { url: "http://schema.org/Book" }, properties: { id: { type: "integer" }, isbn: { description: "The ISBN of the book", type: "string" }, description: { description: "A description of the item", type: "string", }, author: { description: "The author of this content or rating. Please note that author is special in that HTML 5 provides a special mechanism for indicating authorship via the rel tag. That is equivalent to this and may be used interchangeably", type: "string", }, title: { description: "The title of the book", type: "string" }, bookFormat: { type: "string", description: "The publication format of the book.", enum: ["AUDIOBOOK_FORMAT", "E_BOOK", "PAPERBACK", "HARDCOVER"], }, publicationDate: { description: "The date on which the CreativeWork was created or the item was added to a DataFeed", type: "string", format: "date-time", }, }, required: [ "description", "author", "title", "bookFormat", "publicationDate", ], }, Review: { type: "object", externalDocs: { url: "http://schema.org/Review" }, properties: { id: { type: "integer" }, rating: { type: "integer" }, body: { description: "The actual body of the review", type: "string", }, book: { description: "The item that is being reviewed/rated", type: "string", }, author: { description: "Author the author of the review", type: "string", }, publicationDate: { description: "Author the author of the review", type: "string", format: "date-time", }, }, required: ["book"], }, }, }; const parsed = [ { name: "books", url: "https://demo.api-platform.com/books", id: null, title: "Book", fields: [ { name: "id", id: null, range: null, type: "integer", reference: null, required: false, embedded: null, enum: null, description: "", }, { name: "isbn", id: null, range: null, type: "string", reference: null, required: false, embedded: null, enum: null, description: "The ISBN of the book", }, { name: "description", id: null, range: null, type: "string", reference: null, required: true, embedded: null, enum: null, description: "A description of the item", }, { name: "author", id: null, range: null, type: "string", reference: null, required: true, embedded: null, enum: null, description: "The author of this content or rating. Please note that author is special in that HTML 5 provides a special mechanism for indicating authorship via the rel tag. That is equivalent to this and may be used interchangeably", }, { name: "title", id: null, range: null, type: "string", reference: null, required: true, embedded: null, enum: null, description: "The title of the book", }, { name: "bookFormat", id: null, range: null, type: "string", reference: null, embedded: null, enum: { "Audiobook format": "AUDIOBOOK_FORMAT", "E book": "E_BOOK", Paperback: "PAPERBACK", Hardcover: "HARDCOVER", }, required: true, description: "The publication format of the book.", }, { name: "publicationDate", id: null, range: null, type: "dateTime", reference: null, required: true, embedded: null, enum: null, description: "The date on which the CreativeWork was created or the item was added to a DataFeed", }, ], readableFields: [ { name: "id", id: null, range: null, reference: null, required: false, description: "", }, { name: "isbn", id: null, range: null, reference: null, required: false, description: "The ISBN of the book", }, { name: "description", id: null, range: null, reference: null, required: true, description: "A description of the item", }, { name: "author", id: null, range: null, reference: null, required: true, description: "The author of this content or rating. Please note that author is special in that HTML 5 provides a special mechanism for indicating authorship via the rel tag. That is equivalent to this and may be used interchangeably", }, { name: "title", id: null, range: null, reference: null, required: true, description: "The title of the book", }, { name: "publicationDate", id: null, range: null, reference: null, required: true, description: "The date on which the CreativeWork was created or the item was added to a DataFeed", }, ], writableFields: [ { name: "id", id: null, range: null, reference: null, required: false, description: "", }, { name: "isbn", id: null, range: null, reference: null, required: false, description: "The ISBN of the book", }, { name: "description", id: null, range: null, reference: null, required: true, description: "A description of the item", }, { name: "author", id: null, range: null, reference: null, required: true, description: "The author of this content or rating. Please note that author is special in that HTML 5 provides a special mechanism for indicating authorship via the rel tag. That is equivalent to this and may be used interchangeably", }, { name: "title", id: null, range: null, reference: null, required: true, description: "The title of the book", }, { name: "publicationDate", id: null, range: null, reference: null, required: true, description: "The date on which the CreativeWork was created or the item was added to a DataFeed", }, ], }, { name: "reviews", url: "https://demo.api-platform.com/reviews", id: null, title: "Review", fields: [ { name: "id", id: null, range: null, type: "integer", reference: null, required: false, embedded: null, enum: null, description: "", }, { name: "rating", id: null, range: null, type: "integer", reference: null, required: false, embedded: null, enum: null, description: "", }, { name: "body", id: null, range: null, type: "string", reference: null, required: false, embedded: null, enum: null, description: "The actual body of the review", }, { name: "book", id: null, range: null, reference: { name: "books", url: "https://demo.api-platform.com/books", id: null, title: "Book", fields: [ { name: "id", id: null, range: null, reference: null, required: false, description: "", }, { name: "isbn", id: null, range: null, reference: null, required: false, description: "The ISBN of the book", }, { name: "description", id: null, range: null, reference: null, required: true, description: "A description of the item", }, { name: "author", id: null, range: null, reference: null, required: true, description: "The author of this content or rating. Please note that author is special in that HTML 5 provides a special mechanism for indicating authorship via the rel tag. That is equivalent to this and may be used interchangeably", }, { name: "title", id: null, range: null, reference: null, required: true, description: "The title of the book", }, { name: "publicationDate", id: null, range: null, reference: null, required: true, description: "The date on which the CreativeWork was created or the item was added to a DataFeed", }, ], readableFields: [ { name: "id", id: null, range: null, reference: null, required: false, description: "", }, { name: "isbn", id: null, range: null, reference: null, required: false, description: "The ISBN of the book", }, { name: "description", id: null, range: null, reference: null, required: true, description: "A description of the item", }, { name: "author", id: null, range: null, reference: null, required: true, description: "The author of this content or rating. Please note that author is special in that HTML 5 provides a special mechanism for indicating authorship via the rel tag. That is equivalent to this and may be used interchangeably", }, { name: "title", id: null, range: null, reference: null, required: true, description: "The title of the book", }, { name: "publicationDate", id: null, range: null, reference: null, required: true, description: "The date on which the CreativeWork was created or the item was added to a DataFeed", }, ], writableFields: [ { name: "id", id: null, range: null, reference: null, required: false, description: "", }, { name: "isbn", id: null, range: null, reference: null, required: false, description: "The ISBN of the book", }, { name: "description", id: null, range: null, reference: null, required: true, description: "A description of the item", }, { name: "author", id: null, range: null, reference: null, required: true, description: "The author of this content or rating. Please note that author is special in that HTML 5 provides a special mechanism for indicating authorship via the rel tag. That is equivalent to this and may be used interchangeably", }, { name: "title", id: null, range: null, reference: null, required: true, description: "The title of the book", }, { name: "publicationDate", id: null, range: null, reference: null, required: true, description: "The date on which the CreativeWork was created or the item was added to a DataFeed", }, ], }, required: true, description: "The item that is being reviewed/rated", }, { name: "author", id: null, range: null, type: "string", reference: null, required: false, embedded: null, enum: null, description: "Author the author of the review", }, { name: "publicationDate", id: null, range: null, type: "string", reference: null, required: false, embedded: null, enum: null, description: "Author the author of the review", }, ], readableFields: [ { name: "id", id: null, range: null, reference: null, required: false, description: "", }, { name: "rating", id: null, range: null, reference: null, required: false, description: "", }, { name: "body", id: null, range: null, reference: null, required: false, description: "The actual body of the review", }, { name: "book", id: null, range: null, reference: { name: "books", url: "https://demo.api-platform.com/books", id: null, title: "Book", fields: [ { name: "id", id: null, range: null, reference: null, required: false, description: "", }, { name: "isbn", id: null, range: null, reference: null, required: false, description: "The ISBN of the book", }, { name: "description", id: null, range: null, reference: null, required: true, description: "A description of the item", }, { name: "author", id: null, range: null, reference: null, required: true, description: "The author of this content or rating. Please note that author is special in that HTML 5 provides a special mechanism for indicating authorship via the rel tag. That is equivalent to this and may be used interchangeably", }, { name: "title", id: null, range: null, reference: null, required: true, description: "The title of the book", }, { name: "publicationDate", id: null, range: null, reference: null, required: true, description: "The date on which the CreativeWork was created or the item was added to a DataFeed", }, ], readableFields: [ { name: "id", id: null, range: null, reference: null, required: false, description: "", }, { name: "isbn", id: null, range: null, reference: null, required: false, description: "The ISBN of the book", }, { name: "description", id: null, range: null, reference: null, required: true, description: "A description of the item", }, { name: "author", id: null, range: null, reference: null, required: true, description: "The author of this content or rating. Please note that author is special in that HTML 5 provides a special mechanism for indicating authorship via the rel tag. That is equivalent to this and may be used interchangeably", }, { name: "title", id: null, range: null, reference: null, required: true, description: "The title of the book", }, { name: "publicationDate", id: null, range: null, reference: null, required: true, description: "The date on which the CreativeWork was created or the item was added to a DataFeed", }, ], writableFields: [ { name: "id", id: null, range: null, reference: null, required: false, description: "", }, { name: "isbn", id: null, range: null, reference: null, required: false, description: "The ISBN of the book", }, { name: "description", id: null, range: null, reference: null, required: true, description: "A description of the item", }, { name: "author", id: null, range: null, reference: null, required: true, description: "The author of this content or rating. Please note that author is special in that HTML 5 provides a special mechanism for indicating authorship via the rel tag. That is equivalent to this and may be used interchangeably", }, { name: "title", id: null, range: null, reference: null, required: true, description: "The title of the book", }, { name: "publicationDate", id: null, range: null, reference: null, required: true, description: "The date on which the CreativeWork was created or the item was added to a DataFeed", }, ], }, required: true, description: "The item that is being reviewed/rated", }, { name: "author", id: null, range: null, reference: null, required: false, description: "Author the author of the review", }, { name: "publicationDate", id: null, range: null, reference: null, required: false, description: "Author the author of the review", }, ], writableFields: [ { name: "id", id: null, range: null, reference: null, required: false, description: "", }, { name: "rating", id: null, range: null, reference: null, required: false, description: "", }, { name: "body", id: null, range: null, reference: null, required: false, description: "The actual body of the review", }, { name: "book", id: null, range: null, reference: { name: "books", url: "https://demo.api-platform.com/books", id: null, title: "Book", fields: [ { name: "id", id: null, range: null, reference: null, required: false, description: "", }, { name: "isbn", id: null, range: null, reference: null, required: false, description: "The ISBN of the book", }, { name: "description", id: null, range: null, reference: null, required: true, description: "A description of the item", }, { name: "author", id: null, range: null, reference: null, required: true, description: "The author of this content or rating. Please note that author is special in that HTML 5 provides a special mechanism for indicating authorship via the rel tag. That is equivalent to this and may be used interchangeably", }, { name: "title", id: null, range: null, reference: null, required: true, description: "The title of the book", }, { name: "publicationDate", id: null, range: null, reference: null, required: true, description: "The date on which the CreativeWork was created or the item was added to a DataFeed", }, ], readableFields: [ { name: "id", id: null, range: null, reference: null, required: false, description: "", }, { name: "isbn", id: null, range: null, reference: null, required: false, description: "The ISBN of the book", }, { name: "description", id: null, range: null, reference: null, required: true, description: "A description of the item", }, { name: "author", id: null, range: null, reference: null, required: true, description: "The author of this content or rating. Please note that author is special in that HTML 5 provides a special mechanism for indicating authorship via the rel tag. That is equivalent to this and may be used interchangeably", }, { name: "title", id: null, range: null, reference: null, required: true, description: "The title of the book", }, { name: "publicationDate", id: null, range: null, reference: null, required: true, description: "The date on which the CreativeWork was created or the item was added to a DataFeed", }, ], writableFields: [ { name: "id", id: null, range: null, reference: null, required: false, description: "", }, { name: "isbn", id: null, range: null, reference: null, required: false, description: "The ISBN of the book", }, { name: "description", id: null, range: null, reference: null, required: true, description: "A description of the item", }, { name: "author", id: null, range: null, reference: null, required: true, description: "The author of this content or rating. Please note that author is special in that HTML 5 provides a special mechanism for indicating authorship via the rel tag. That is equivalent to this and may be used interchangeably", }, { name: "title", id: null, range: null, reference: null, required: true, description: "The title of the book", }, { name: "publicationDate", id: null, range: null, reference: null, required: true, description: "The date on which the CreativeWork was created or the item was added to a DataFeed", }, ], }, required: true, description: "The item that is being reviewed/rated", }, { name: "author", id: null, range: null, reference: null, required: false, description: "Author the author of the review", }, { name: "publicationDate", id: null, range: null, reference: null, required: false, description: "Author the author of the review", }, ], }, ]; describe(`Parse Swagger Documentation from Json`, () => { const toBeParsed = handleJson( swaggerApiDefinition, "https://demo.api-platform.com" ); test(`Properties to be equal`, () => { expect(toBeParsed[0].name).toBe(parsed[0].name); expect(toBeParsed[0].url).toBe(parsed[0].url); expect(toBeParsed[0].id).toBe(parsed[0].id); const toBeParsedFields = toBeParsed[0].fields as Field[]; expect(toBeParsedFields[0]).toEqual(parsed[0].fields[0]); expect(toBeParsedFields[1]).toEqual(parsed[0].fields[1]); expect(toBeParsedFields[2]).toEqual(parsed[0].fields[2]); expect(toBeParsedFields[3]).toEqual(parsed[0].fields[3]); expect(toBeParsedFields[4]).toEqual(parsed[0].fields[4]); expect(toBeParsedFields[5]).toEqual(parsed[0].fields[5]); expect(toBeParsed[1].name).toBe(parsed[1].name); expect(toBeParsed[1].url).toBe(parsed[1].url); expect(toBeParsed[1].id).toBe(parsed[1].id); expect((toBeParsed[1].fields as Field[])[0]).toEqual(parsed[1].fields[0]); }); });