diff --git a/docs/api-sdk/SDK_MODELS_HIERARCHY.md b/docs/api-sdk/SDK_MODELS_HIERARCHY.md index 9764abe6fd..248dd75db6 100644 --- a/docs/api-sdk/SDK_MODELS_HIERARCHY.md +++ b/docs/api-sdk/SDK_MODELS_HIERARCHY.md @@ -20,13 +20,20 @@ The purpose of the revivers is to convert JSON parsed object to the model the re Example of a reviver: ```typescript -export function reviveMyModel(data: any, dictionaries?: any): T | undefined { +export function reviveMyModel(data: any, dictionaries?: any): T | undefined { if (!data) { return ; } data.myField = reviverMyFiedType(data.myField, dictionaries); return data as T; } ``` +> [!TIP] +> The reviver can help to remove the dictionary fields from a revived object when used without dictionary: +> +> ```typescript +> reviveMyModel(data); +> ``` + ## Core models (and base models extension) To be able to use the core models in different applications, the base models must be extended the same way it is done in the generated SDKs, based on Swagger specification using the same definitions. diff --git a/packages/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/src/main/resources/typescriptFetch/api/api.mustache b/packages/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/src/main/resources/typescriptFetch/api/api.mustache index 810dae642c..054319dd96 100644 --- a/packages/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/src/main/resources/typescriptFetch/api/api.mustache +++ b/packages/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/src/main/resources/typescriptFetch/api/api.mustache @@ -22,7 +22,7 @@ export type {{classname}}{{#uppercaseFirst}}{{nickname}}{{/uppercaseFirst}}{{#up export interface {{classname}}{{#uppercaseFirst}}{{nickname}}{{/uppercaseFirst}}RequestData { {{#allParams}} /** {{#description}}{{{description}}}{{/description}}{{^description}}{{#isArray}}List of {{#plurialize}}{{#noArrayInType}}{{{dataType}}}{{/noArrayInType}}{{/plurialize}}{{/isArray}}{{/description}} */ - '{{baseName}}'{{^required}}?{{/required}}{{#required}}{{#defaultValue}}?{{/defaultValue}}{{/required}}: {{#isEnum}}{{classname}}{{#uppercaseFirst}}{{nickname}}{{/uppercaseFirst}}{{#uppercaseFirst}}{{paramName}}{{/uppercaseFirst}}Enum{{#isArray}}[]{{/isArray}}{{/isEnum}}{{^isEnum}}{{#isBodyParam}}{{{baseType}}}{{#isArray}}[]{{/isArray}}{{/isBodyParam}}{{^isBodyParam}}{{{dataType}}}{{/isBodyParam}}{{/isEnum}}; + '{{baseName}}'{{^required}}?{{/required}}{{#required}}{{#defaultValue}}?{{/defaultValue}}{{/required}}: {{#isEnum}}{{classname}}{{#uppercaseFirst}}{{nickname}}{{/uppercaseFirst}}{{#uppercaseFirst}}{{paramName}}{{/uppercaseFirst}}Enum{{#isArray}}[]{{/isArray}}{{/isEnum}}{{^isEnum}}{{#isBodyParam}}{{{baseType}}}{{#keepRevivers}}NoDictionary{{/keepRevivers}}{{#isArray}}[]{{/isArray}}{{/isBodyParam}}{{^isBodyParam}}{{{dataType}}}{{/isBodyParam}}{{/isEnum}}; {{/allParams}} } {{/operation}} diff --git a/packages/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/src/main/resources/typescriptFetch/model/model.mustache b/packages/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/src/main/resources/typescriptFetch/model/model.mustache index 2b3057d38c..cccd7b7ae3 100644 --- a/packages/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/src/main/resources/typescriptFetch/model/model.mustache +++ b/packages/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/src/main/resources/typescriptFetch/model/model.mustache @@ -22,12 +22,13 @@ import { {{import}} } from '../{{#kebabCase}}{{import}}{{/kebabCase}}'; {{#model}} {{#description}} /** - * {{{description}}} + * {{{description}}}{{#keepRevivers}} + * Note: This interface does not expose the dictionary field that can be part of the revived object.{{/keepRevivers}} */ {{/description}} - {{^isEnum}} - {{^oneOf}} -export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{ + {{^isEnum}} + {{^oneOf}} +export interface {{classname}}{{#keepRevivers}}NoDictionary{{/keepRevivers}} {{#parent}}extends {{{parent}}} {{/parent}}{ {{#vars}} {{#description}} /** {{{description}}} */ @@ -49,23 +50,7 @@ export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{ {{/isEnum}} {{/isArray}} {{/description}} - {{#readOnly}} readonly {{/readOnly}}{{#propertyDeclaration}}{{baseName}}{{/propertyDeclaration}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}; - {{/vars}} - {{#vars}} - {{#vendorExtensions}} - {{#x-field-name}} - /** Dictionary extraction for {{baseName}} */ - {{#readOnly}}readonly {{/readOnly}}{{x-field-name}}{{^required}}?{{/required}}: {{x-field-type}}; - {{/x-field-name}} - {{/vendorExtensions}} - {{/vars}} - {{#vars}} - {{#vendorExtensions}} - {{#x-map-name}} - /** Dictionary extraction for {{baseName}} */ - {{#readOnly}}readonly {{/readOnly}}{{x-map-name}}{{^required}}?{{/required}}: { [key: string]: {{x-field-type}} | undefined }; - {{/x-map-name}} - {{/vendorExtensions}} + {{#readOnly}} readonly {{/readOnly}}{{#propertyDeclaration}}{{baseName}}{{/propertyDeclaration}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{#keepRevivers}}NoDictionary{{/keepRevivers}}{{/isEnum}}; {{/vars}} } {{/oneOf}} @@ -95,4 +80,40 @@ export type {{classname}} = {{#trimPipe}}{{#allowableValues}}{{#values}}'{{.}}' {{/isEnum}} {{/model}} {{/models}} + +{{#keepRevivers}} +{{#models}} +{{#model}} + {{#description}} +/** + * {{{description}}} + */ + {{/description}} + {{^isEnum}} + {{^oneOf}} +export interface {{classname}} extends {{classname}}NoDictionary { + {{#vars}} + /** @inheritdoc */ + {{#readOnly}} readonly {{/readOnly}}{{#propertyDeclaration}}{{baseName}}{{/propertyDeclaration}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}; + {{#vendorExtensions}} + {{#x-field-name}} + /** Dictionary extraction for {{baseName}} */ + {{#readOnly}}readonly {{/readOnly}}{{x-field-name}}{{^required}}?{{/required}}: {{x-field-type}}; + {{/x-field-name}} + {{/vendorExtensions}} + {{/vars}} + {{#vars}} + {{#vendorExtensions}} + {{#x-map-name}} + /** Dictionary extraction for {{baseName}} */ + {{#readOnly}}readonly {{/readOnly}}{{x-map-name}}{{^required}}?{{/required}}: { [key: string]: {{x-field-type}} | undefined }; + {{/x-map-name}} + {{/vendorExtensions}} + {{/vars}} + {{/oneOf}} + {{/isEnum}} +} +{{/model}} +{{/models}} +{{/keepRevivers}} {{/noUnusedImport}} diff --git a/packages/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/src/main/resources/typescriptFetch/model/reviver.mustache b/packages/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/src/main/resources/typescriptFetch/model/reviver.mustache index 779dc3a658..7d63fe842a 100644 --- a/packages/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/src/main/resources/typescriptFetch/model/reviver.mustache +++ b/packages/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/src/main/resources/typescriptFetch/model/reviver.mustache @@ -5,7 +5,7 @@ */ {{#noUnusedImport}} {{#noEmptyLines}} -import { {{classname}} } from './{{#kebabCase}}{{classname}}{{/kebabCase}}'; +import { {{classname}}, {{classname}}NoDictionary } from './{{#kebabCase}}{{classname}}{{/kebabCase}}'; {{#imports}} import { {{import}} } from '../{{#kebabCase}}{{import}}{{/kebabCase}}'; import { revive{{import}} } from '../{{#kebabCase}}{{import}}{{/kebabCase}}'; @@ -23,12 +23,12 @@ import { utils, reviveArray, reviveMap, type ReviverOptions, reviveDictionarized {{#model}} {{^isEnum}} {{^isEnumRef}} -export function revive{{classname}}(data: undefined, dictionaries?: any, options?: ReviverOptions): undefined; +export function revive{{classname}}(data: undefined, dictionaries?: any, options?: ReviverOptions): undefined; export function revive{{classname}}(data: {{classname}}, dictionaries?: any, options?: ReviverOptions): {{classname}} {{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}}; export function revive{{classname}}(data: any, dictionaries?: any, options?: ReviverOptions): {{classname}} {{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}} | undefined; -export function revive{{classname}}(data: T, dictionaries?: any, options?: ReviverOptions): T {{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}}; -export function revive{{classname}}(data: any, dictionaries?: any, options?: ReviverOptions): T {{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}} | undefined; -export function revive{{classname}}(data: any, dictionaries?: any, options?: ReviverOptions): T {{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}} | undefined { +export function revive{{classname}}(data: T, dictionaries?: any, options?: ReviverOptions): T {{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}}; +export function revive{{classname}}(data: any, dictionaries?: any, options?: ReviverOptions): T {{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}} | undefined; +export function revive{{classname}}(data: any, dictionaries?: any, options?: ReviverOptions): T {{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}} | undefined { if (!data) { return ; } {{#noEmptyLines}} {{#vendorExtensions}}