Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: required properties are supplied via a constructor function #52

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions partials/model.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@
*/
export class {{@key}} {

static propertyTypes: Array<{name: string, type: string}> = [
static propertyTypes: Array<PropertyMetadata> = [
{{#each properties}}
{
name: "{{@key}}",
type: "{{typeConvert this}}"
type: "{{typeConvert this}}",
required: {{#if _required}}true{{else}}false{{/if}}
},
{{/each}}
];

{{#each properties}}
public {{@key}}{{#unless _required}}?{{/unless}}: {{typeConvert this}};
{{/each}}

constructor(
{{#each properties}}{{#if _required}}{{@key}}: {{typeConvert this}},{{/if}}{{/each}}
) {
{{#each properties}}{{#if _required}}
this.{{@key}} = {{@key}};{{/if~}}{{/each~}}
}
}
6 changes: 6 additions & 0 deletions template/model.ts.handlebars
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export interface PropertyMetadata {
name: string,
type: string,
required: boolean
}

{{#each components.schemas}}
{{> model}}

Expand Down
14 changes: 12 additions & 2 deletions template/serializer.ts.handlebars
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{{#if _options.testRun }}
const model = require("./model");
const PropertyMetadata = model.PropertyMetadata;
{{else}}
import * as model from "./model";
import { PropertyMetadata } from "./model";
{{/if}}

// takes a JSON response and deserializes it into the required model objects / types
Expand All @@ -15,9 +17,17 @@ export function deserialize(json: any, type: string): any {

// handle model objects
if (model.models.includes(type)) {
const modelObject = new (<any>model)[type]();
// get the required and optional properties
const properties = (<any>model)[type].propertyTypes;
for (const property of properties) {
const requiredProperties = properties.filter((property: {{#if _options.testRun }}typeof {{/if}}PropertyMetadata ) => property.required);
const optionalProperties = properties.filter((property: {{#if _options.testRun }}typeof {{/if}}PropertyMetadata) => !property.required);
const requiredPropertyValues = requiredProperties.map((property: {{#if _options.testRun }}typeof {{/if}}PropertyMetadata) => deserialize(json[property.name], property.type));

// create the model object
const modelObject = new (<any>model)[type](...requiredPropertyValues);

// set the optional properties
for (const property of optionalProperties) {
if (json[property.name] !== undefined) {
modelObject[property.name] = deserialize(json[property.name], property.type);
}
Expand Down