generated from Himenon/template-js
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Make the behavior of anyOf equivalent to oneOf (#70)
- Loading branch information
Showing
7 changed files
with
205 additions
and
5 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
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
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
101 changes: 101 additions & 0 deletions
101
test/__tests__/__snapshots__/multi-type.test.domain.ts.snap
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,101 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Multi Type apiClient 1`] = ` | ||
"// | ||
// Generated by @himenon/openapi-typescript-code-generator | ||
// | ||
// OpenApi : 3.0.1 | ||
// | ||
// License : MIT | ||
// | ||
import { Schemas } from \\"./types\\"; | ||
export interface RequestBody$putAnyOf { | ||
\\"application/json\\": Schemas.Cat | Schemas.Dog; | ||
} | ||
export interface RequestBody$patchOneOf { | ||
\\"application/json\\": Schemas.Cat | Schemas.Dog; | ||
} | ||
export type RequestContentType$putAnyOf = keyof RequestBody$putAnyOf; | ||
export interface Params$putAnyOf { | ||
requestBody: RequestBody$putAnyOf[\\"application/json\\"]; | ||
} | ||
export type RequestContentType$patchOneOf = keyof RequestBody$patchOneOf; | ||
export interface Params$patchOneOf { | ||
requestBody: RequestBody$patchOneOf[\\"application/json\\"]; | ||
} | ||
export type HttpMethod = \\"GET\\" | \\"PUT\\" | \\"POST\\" | \\"DELETE\\" | \\"OPTIONS\\" | \\"HEAD\\" | \\"PATCH\\" | \\"TRACE\\"; | ||
export interface ObjectLike { | ||
[key: string]: any; | ||
} | ||
export interface QueryParameter { | ||
value: any; | ||
style?: \\"form\\" | \\"spaceDelimited\\" | \\"pipeDelimited\\" | \\"deepObject\\"; | ||
explode: boolean; | ||
} | ||
export interface QueryParameters { | ||
[key: string]: QueryParameter; | ||
} | ||
export type SuccessResponses = void; | ||
export namespace ErrorResponse { | ||
export type putAnyOf = void; | ||
export type patchOneOf = void; | ||
} | ||
export interface ApiClient<RequestOption> { | ||
request: <T = SuccessResponses>(httpMethod: HttpMethod, url: string, headers: ObjectLike | any, requestBody: ObjectLike | any, queryParameters: QueryParameters | undefined, options?: RequestOption) => Promise<T>; | ||
} | ||
export class Client<RequestOption> { | ||
private baseUrl: string; | ||
constructor(private apiClient: ApiClient<RequestOption>, baseUrl: string) { this.baseUrl = baseUrl.replace(/\\\\/$/, \\"\\"); } | ||
/** | ||
* operationId: putAnyOf | ||
* Request URI: /pets | ||
*/ | ||
public async putAnyOf(params: Params$putAnyOf, option?: RequestOption): Promise<void> { | ||
const url = this.baseUrl + \`/pets\`; | ||
const headers = { | ||
\\"Content-Type\\": \\"application/json\\" | ||
}; | ||
return this.apiClient.request(\\"PUT\\", url, headers, params.requestBody, undefined, option); | ||
} | ||
/** | ||
* operationId: patchOneOf | ||
* Request URI: /pets | ||
*/ | ||
public async patchOneOf(params: Params$patchOneOf, option?: RequestOption): Promise<void> { | ||
const url = this.baseUrl + \`/pets\`; | ||
const headers = { | ||
\\"Content-Type\\": \\"application/json\\" | ||
}; | ||
return this.apiClient.request(\\"PATCH\\", url, headers, params.requestBody, undefined, option); | ||
} | ||
} | ||
" | ||
`; | ||
exports[`Multi Type types 1`] = ` | ||
"// | ||
// Generated by @himenon/openapi-typescript-code-generator | ||
// | ||
// OpenApi : 3.0.1 | ||
// | ||
// License : MIT | ||
// | ||
export namespace Schemas { | ||
export interface Pet { | ||
pet_type: string; | ||
} | ||
export type Dog = Schemas.Pet & { | ||
bark?: boolean; | ||
breed?: \\"Dingo\\" | \\"Husky\\" | \\"Retriever\\" | \\"Shepherd\\"; | ||
}; | ||
export type Cat = Schemas.Pet & { | ||
hunts?: boolean; | ||
age?: number; | ||
}; | ||
} | ||
" | ||
`; |
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,18 @@ | ||
import * as fs from "fs"; | ||
import * as path from "path"; | ||
|
||
import * as Utils from "../utils"; | ||
|
||
describe("Multi Type", () => { | ||
test("types", () => { | ||
const generateCode = fs.readFileSync(path.join(__dirname, "../code/mulit-type-test.domain/types.ts"), { encoding: "utf-8" }); | ||
const text = Utils.replaceVersionInfo(generateCode); | ||
expect(text).toMatchSnapshot(); | ||
}); | ||
|
||
test("apiClient", () => { | ||
const generateCode = fs.readFileSync(path.join(__dirname, "../code/mulit-type-test.domain/apiClient.ts"), { encoding: "utf-8" }); | ||
const text = Utils.replaceVersionInfo(generateCode); | ||
expect(text).toMatchSnapshot(); | ||
}); | ||
}); |
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
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,71 @@ | ||
openapi: 3.0.1 | ||
info: | ||
version: 1.0.0 | ||
title: multi-type.test.domain | ||
description: Library test schema | ||
license: | ||
name: MIT | ||
|
||
servers: | ||
- url: "http://multi-type.test.domain/" | ||
description: Development Environment | ||
|
||
components: | ||
schemas: | ||
Pet: | ||
type: object | ||
required: | ||
- pet_type | ||
properties: | ||
pet_type: | ||
type: string | ||
discriminator: | ||
propertyName: pet_type | ||
Dog: | ||
allOf: | ||
- $ref: "#/components/schemas/Pet" | ||
- type: object | ||
# all other properties specific to a `Dog` | ||
properties: | ||
bark: | ||
type: boolean | ||
breed: | ||
type: string | ||
enum: [Dingo, Husky, Retriever, Shepherd] | ||
Cat: | ||
allOf: | ||
- $ref: "#/components/schemas/Pet" | ||
- type: object | ||
properties: | ||
hunts: | ||
type: boolean | ||
age: | ||
type: integer | ||
|
||
paths: | ||
/pets: | ||
put: | ||
operationId: putAnyOf | ||
requestBody: | ||
content: | ||
application/json: | ||
schema: | ||
anyOf: | ||
- $ref: "#/components/schemas/Cat" | ||
- $ref: "#/components/schemas/Dog" | ||
responses: | ||
"200": | ||
description: Updated | ||
|
||
patch: | ||
operationId: patchOneOf | ||
requestBody: | ||
content: | ||
application/json: | ||
schema: | ||
oneOf: | ||
- $ref: "#/components/schemas/Cat" | ||
- $ref: "#/components/schemas/Dog" | ||
responses: | ||
"200": | ||
description: Updated |