Skip to content

Commit f2d4850

Browse files
authored
fix: issue/19 (#21)
* First commit with samples. * Work in progress. * Work in progress. * First commit with code worth sharing. * Fixed merge messages in README. * fix: issue/19 : Fixed bugs rendering enums.
1 parent cfaa347 commit f2d4850

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

filters/all.js

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function indent1(numTabs) {
3636
}
3737
filter.indent1 = indent1;
3838

39-
function indent2(numTabs) {
39+
function indent2(numTabs) {
4040
return indent(numTabs + 1);
4141
}
4242
filter.indent2 = indent2;
@@ -115,7 +115,7 @@ function payloadClass(operation) {
115115
if (ret.includes("anonymous-schema")) {
116116
ret = "Payload";
117117
}
118-
return ret;
118+
return _.upperFirst(ret);
119119
}
120120
filter.payloadClass = payloadClass;
121121

@@ -151,23 +151,30 @@ function determineType(name, property) {
151151
ret.properties = property.properties(); // can change if it's an embedded type.
152152
ret.pythonName = templateUtil.getIdentifierName(name);
153153
//console.log(name + ": " + type);
154+
//console.log(property);
154155

155-
if (type === undefined) {
156-
if (property.enum()) {
157-
ret.type = _.upperFirst(ret.pythonName);
158-
ret.pythonType = ret.type;
159-
ret.generalType = 'enum';
160-
ret.enum = property.enum();
161-
//console.log("enum is " + dump(ret.enum))
162-
} else {
163-
// check to see if it's a ref to another schema.
164-
ret.type = property.ext('x-parser-schema-id');
165-
ret.generalType = 'objectRef';
166-
ret.pythonType = ret.type
156+
if (property.enum()) {
157+
ret.type = _.upperFirst(ret.pythonName);
158+
ret.pythonType = ret.type;
159+
ret.generalType = 'enum';
160+
ret.enum = property.enum();
161+
let e = ret.enum;
167162

168-
if (!ret.type) {
169-
throw new Error("Can't determine the type of property " + name);
170-
}
163+
164+
for (var i = 0; i < e.length; i++) {
165+
let v = e[i].replace(/\W/g, '')
166+
e[i] = v;
167+
//console.log(v);
168+
}
169+
//console.log("enum is " + t);
170+
} else if (type === undefined) {
171+
// check to see if it's a ref to another schema.
172+
ret.type = property.ext('x-parser-schema-id');
173+
ret.generalType = 'objectRef';
174+
ret.pythonType = ret.type
175+
176+
if (!ret.type) {
177+
throw new Error("Can't determine the type of property " + name);
171178
}
172179
} else if (type === 'array') {
173180
ret.generalType = 'array';
@@ -185,7 +192,7 @@ function determineType(name, property) {
185192
itemsType = _.upperFirst(ret.pythonName);
186193
ret.properties = items.properties();
187194
} else {
188-
itemsType = typeMap.get(itemsType);
195+
itemsType = typeMap[itemsType];
189196
}
190197
}
191198
if (!itemsType) {
@@ -236,7 +243,7 @@ function getImports(schema) {
236243
//console.log(getMethods(schema))
237244
//console.log(schema);
238245
var properties = schema.properties();
239-
246+
240247

241248
if (schema.type() === 'array') {
242249
properties = schema.items().properties();
@@ -254,7 +261,7 @@ function getImports(schema) {
254261
if (type === 'array') {
255262
let itemsType = property.items();
256263
let ref = itemsType.ext('x-parser-schema-id');
257-
if (ref) {
264+
if (ref && !ref.includes('anonymous')) {
258265
let importName = _.lowerFirst(ref);
259266
ret += `from ${importName} import ${ref}\n`
260267
}
@@ -263,12 +270,14 @@ function getImports(schema) {
263270
}
264271
} else {
265272
let ref = property.ext('x-parser-schema-id');
273+
//console.log(`undefined type, ref is ${ref}`);
266274
if (ref && !ref.includes('anonymous')) {
267275
let importName = _.lowerFirst(ref);
268276
ret += `from ${importName} import ${ref}\n`
269277
}
270278
}
271279
}
280+
//console.log(`getImports returing ${ret}`);
272281
return ret;
273282
}
274283
filter.getImports = getImports;
@@ -342,7 +351,7 @@ function getFirstPublisherMessenger([params, asyncapi]) {
342351
messenger.payloadClass = filter.payloadClass(pub);
343352
//console.log("getFirstPublisherMessenger messenger.payloadClass: " + messenger.payloadClass);
344353
//console.log("getFirstPublisherMessenger messenger.name: " + messenger.name);
345-
ret = messenger;
354+
ret = messenger;
346355
break;
347356
}
348357
}

template/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{% if asyncapi.components() -%}
99
{% for schemaName, schema in asyncapi.components().schemas() -%}
1010
{% set moduleName = schemaName | lowerFirst -%}
11-
from {{ moduleName }} import {{ schemaName }}
11+
from {{ moduleName }} import {{ schemaName | upperFirst }}
1212
{% endfor -%}
1313
{% else -%}
1414
from payload import Payload

0 commit comments

Comments
 (0)