Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ import {
{{/discriminator}}
{{>modelGenericInterfaces}}

/**
* Check if a given object implements the {{classname}} interface.
*/
export function instanceOf{{classname}}(value: object): boolean {
let isInstance = true;
{{#vars}}
{{#required}}
isInstance = isInstance && "{{name}}" in value;
{{/required}}
{{/vars}}

return isInstance;
}

export function {{classname}}FromJSON(json: any): {{classname}} {
return {{classname}}FromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{{#oneOf}}
import {
{{{.}}},
instanceOf{{{.}}},
{{{.}}}FromJSON,
{{{.}}}FromJSONTyped,
{{{.}}}ToJSON,
Expand Down Expand Up @@ -51,7 +52,14 @@ export function {{classname}}ToJSON(value?: {{classname}} | null): any {
throw new Error(`No variant of {{classname}} exists with '{{discriminator.propertyName}}=${value['{{discriminator.propertyName}}']}'`);
}
{{/discriminator}}

{{^discriminator}}
return { {{#oneOf}}...{{{.}}}ToJSON(value){{^-last}}, {{/-last}}{{/oneOf}} };
{{#oneOf}}
if (instanceOf{{{.}}}(value)) {
return {{{.}}}ToJSON(value as {{{.}}});
}
{{/oneOf}}

return {};
{{/discriminator}}
Comment on lines +55 to 64
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely familiar with this code, but I think something similar should also be added to modelGeneric.mustache.
In a project of mine where I use allOf for inheritance. The base class has a {class}FromJSONTyped function that handles all the discriminator cases, but {class}ToJSON doesn't include any discriminator code at all and just returns an object with properties of the base class.

I'm currently patching the generated code to add ignoreDiscriminator to the ToJSON functions which will be set to true when invoked from the child class's ToJSON, if false ToJSON in the base class calls the respective child class ToJSON based on the discriminator.

Here are the relevant models of my openapi config:

      "Activity": {
        "required": ["$type", "id", "title"],
        "type": "object",
        "properties": {
          "$type": {
            "$ref": "#/components/schemas/ActivityType"
          },
          "id": {
            "type": "string"
          },
          "title": {
            "type": "string"
          },
        },
        "additionalProperties": false,
        "discriminator": {
          "propertyName": "$type",
          "mapping": {
            "exercise": "#/components/schemas/ActivityExercise",
            "job": "#/components/schemas/ActivityJob",
          }
        }
      },
      "ActivityExercise": {
        "required": ["$type", "category"],
        "type": "object",
        "allOf": [
          {
            "$ref": "#/components/schemas/Activity"
          }
        ],
        "properties": {
          "$type": {
            "$ref": "#/components/schemas/ActivityType"
          },
          "category": {
            "$ref": "#/components/schemas/ActivityCategory"
          },
          "muscleShares": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/MuscleShare"
            },
            "nullable": true
          }
        },
        "additionalProperties": false
      },
      "ActivityJob": {
        "required": ["$type"],
        "type": "object",
        "allOf": [
          {
            "$ref": "#/components/schemas/Activity"
          }
        ],
        "properties": {
          "$type": {
            "$ref": "#/components/schemas/ActivityType"
          },
          "muscleShares": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/MuscleShare"
            },
            "nullable": true
          }
        },
        "additionalProperties": false
      },

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ export interface AdditionalPropertiesClass {
mapOfMapProperty?: { [key: string]: { [key: string]: string; }; };
}

/**
* Check if a given object implements the AdditionalPropertiesClass interface.
*/
export function instanceOfAdditionalPropertiesClass(value: object): boolean {
let isInstance = true;

return isInstance;
}

export function AdditionalPropertiesClassFromJSON(json: any): AdditionalPropertiesClass {
return AdditionalPropertiesClassFromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ export interface AllOfWithSingleRef {
singleRefType?: SingleRefType | null;
}

/**
* Check if a given object implements the AllOfWithSingleRef interface.
*/
export function instanceOfAllOfWithSingleRef(value: object): boolean {
let isInstance = true;

return isInstance;
}

export function AllOfWithSingleRefFromJSON(json: any): AllOfWithSingleRef {
return AllOfWithSingleRefFromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ export interface Animal {
color?: string;
}

/**
* Check if a given object implements the Animal interface.
*/
export function instanceOfAnimal(value: object): boolean {
let isInstance = true;
isInstance = isInstance && "className" in value;

return isInstance;
}

export function AnimalFromJSON(json: any): Animal {
return AnimalFromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export interface ArrayOfArrayOfNumberOnly {
arrayArrayNumber?: Array<Array<number>>;
}

/**
* Check if a given object implements the ArrayOfArrayOfNumberOnly interface.
*/
export function instanceOfArrayOfArrayOfNumberOnly(value: object): boolean {
let isInstance = true;

return isInstance;
}

export function ArrayOfArrayOfNumberOnlyFromJSON(json: any): ArrayOfArrayOfNumberOnly {
return ArrayOfArrayOfNumberOnlyFromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export interface ArrayOfNumberOnly {
arrayNumber?: Array<number>;
}

/**
* Check if a given object implements the ArrayOfNumberOnly interface.
*/
export function instanceOfArrayOfNumberOnly(value: object): boolean {
let isInstance = true;

return isInstance;
}

export function ArrayOfNumberOnlyFromJSON(json: any): ArrayOfNumberOnly {
return ArrayOfNumberOnlyFromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ export interface ArrayTest {
arrayArrayOfModel?: Array<Array<ReadOnlyFirst>>;
}

/**
* Check if a given object implements the ArrayTest interface.
*/
export function instanceOfArrayTest(value: object): boolean {
let isInstance = true;

return isInstance;
}

export function ArrayTestFromJSON(json: any): ArrayTest {
return ArrayTestFromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ export interface Capitalization {
aTTNAME?: string;
}

/**
* Check if a given object implements the Capitalization interface.
*/
export function instanceOfCapitalization(value: object): boolean {
let isInstance = true;

return isInstance;
}

export function CapitalizationFromJSON(json: any): Capitalization {
return CapitalizationFromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ export interface Cat extends Animal {
declawed?: boolean;
}

/**
* Check if a given object implements the Cat interface.
*/
export function instanceOfCat(value: object): boolean {
let isInstance = true;

return isInstance;
}

export function CatFromJSON(json: any): Cat {
return CatFromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export interface CatAllOf {
declawed?: boolean;
}

/**
* Check if a given object implements the CatAllOf interface.
*/
export function instanceOfCatAllOf(value: object): boolean {
let isInstance = true;

return isInstance;
}

export function CatAllOfFromJSON(json: any): CatAllOf {
return CatAllOfFromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ export interface Category {
name: string;
}

/**
* Check if a given object implements the Category interface.
*/
export function instanceOfCategory(value: object): boolean {
let isInstance = true;
isInstance = isInstance && "name" in value;

return isInstance;
}

export function CategoryFromJSON(json: any): Category {
return CategoryFromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export interface ClassModel {
_class?: string;
}

/**
* Check if a given object implements the ClassModel interface.
*/
export function instanceOfClassModel(value: object): boolean {
let isInstance = true;

return isInstance;
}

export function ClassModelFromJSON(json: any): ClassModel {
return ClassModelFromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export interface Client {
client?: string;
}

/**
* Check if a given object implements the Client interface.
*/
export function instanceOfClient(value: object): boolean {
let isInstance = true;

return isInstance;
}

export function ClientFromJSON(json: any): Client {
return ClientFromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export interface DeprecatedObject {
name?: string;
}

/**
* Check if a given object implements the DeprecatedObject interface.
*/
export function instanceOfDeprecatedObject(value: object): boolean {
let isInstance = true;

return isInstance;
}

export function DeprecatedObjectFromJSON(json: any): DeprecatedObject {
return DeprecatedObjectFromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ export interface Dog extends Animal {
breed?: string;
}

/**
* Check if a given object implements the Dog interface.
*/
export function instanceOfDog(value: object): boolean {
let isInstance = true;

return isInstance;
}

export function DogFromJSON(json: any): Dog {
return DogFromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export interface DogAllOf {
breed?: string;
}

/**
* Check if a given object implements the DogAllOf interface.
*/
export function instanceOfDogAllOf(value: object): boolean {
let isInstance = true;

return isInstance;
}

export function DogAllOfFromJSON(json: any): DogAllOf {
return DogAllOfFromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ export const EnumArraysArrayEnumEnum = {
export type EnumArraysArrayEnumEnum = typeof EnumArraysArrayEnumEnum[keyof typeof EnumArraysArrayEnumEnum];


/**
* Check if a given object implements the EnumArrays interface.
*/
export function instanceOfEnumArrays(value: object): boolean {
let isInstance = true;

return isInstance;
}

export function EnumArraysFromJSON(json: any): EnumArrays {
return EnumArraysFromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ export const EnumTestEnumNumberEnum = {
export type EnumTestEnumNumberEnum = typeof EnumTestEnumNumberEnum[keyof typeof EnumTestEnumNumberEnum];


/**
* Check if a given object implements the EnumTest interface.
*/
export function instanceOfEnumTest(value: object): boolean {
let isInstance = true;
isInstance = isInstance && "enumStringRequired" in value;

return isInstance;
}

export function EnumTestFromJSON(json: any): EnumTest {
return EnumTestFromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ export interface FileSchemaTestClass {
files?: Array<any>;
}

/**
* Check if a given object implements the FileSchemaTestClass interface.
*/
export function instanceOfFileSchemaTestClass(value: object): boolean {
let isInstance = true;

return isInstance;
}

export function FileSchemaTestClassFromJSON(json: any): FileSchemaTestClass {
return FileSchemaTestClassFromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export interface Foo {
bar?: string;
}

/**
* Check if a given object implements the Foo interface.
*/
export function instanceOfFoo(value: object): boolean {
let isInstance = true;

return isInstance;
}

export function FooFromJSON(json: any): Foo {
return FooFromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ export interface FooGetDefaultResponse {
string?: Foo;
}

/**
* Check if a given object implements the FooGetDefaultResponse interface.
*/
export function instanceOfFooGetDefaultResponse(value: object): boolean {
let isInstance = true;

return isInstance;
}

export function FooGetDefaultResponseFromJSON(json: any): FooGetDefaultResponse {
return FooGetDefaultResponseFromJSONTyped(json, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,19 @@ export interface FormatTest {
patternWithDigitsAndDelimiter?: string;
}

/**
* Check if a given object implements the FormatTest interface.
*/
export function instanceOfFormatTest(value: object): boolean {
let isInstance = true;
isInstance = isInstance && "number" in value;
isInstance = isInstance && "_byte" in value;
isInstance = isInstance && "date" in value;
isInstance = isInstance && "password" in value;

return isInstance;
}

export function FormatTestFromJSON(json: any): FormatTest {
return FormatTestFromJSONTyped(json, false);
}
Expand Down
Loading