Skip to content

Commit 2156c5c

Browse files
author
svolkov
committed
fix: generating invalid code in composed schema contexts (trying to format type's content)
1 parent 8a2b61a commit 2156c5c

File tree

5 files changed

+504
-115
lines changed

5 files changed

+504
-115
lines changed

src/schema.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,20 @@ const getComplexType = (schema) => {
132132
throw new Error("Uknown complex type");
133133
};
134134

135+
const attachParsedRef = (originalSchema, parsedSchema) => {
136+
if (originalSchema) {
137+
originalSchema.$parsed = parsedSchema;
138+
}
139+
140+
return parsedSchema;
141+
};
142+
135143
const schemaParsers = {
136144
enum: (schema, typeName) => {
137145
const type = getType(schema);
138146
const isIntegerEnum = type === types.number;
139-
return {
147+
148+
return attachParsedRef(schema, {
140149
$parsedSchema: true,
141150
schemaType: "enum",
142151
type: isIntegerEnum ? "intEnum" : "enum",
@@ -148,7 +157,7 @@ const schemaParsers = {
148157
type,
149158
value: isIntegerEnum ? `${key}` : `"${key}"`,
150159
})),
151-
};
160+
});
152161
},
153162
object: (schema, typeName) => {
154163
if (_.isArray(schema.required) && schema.properties) {
@@ -162,7 +171,7 @@ const schemaParsers = {
162171
const properties = _.get(schema, "properties");
163172
const typeContent = getObjectTypeContent(properties);
164173

165-
return {
174+
return attachParsedRef(schema, {
166175
$parsedSchema: true,
167176
schemaType: "object",
168177
type: "object",
@@ -171,20 +180,20 @@ const schemaParsers = {
171180
description: formatDescription(schema.description),
172181
allFieldsAreOptional: !_.some(_.values(typeContent), (part) => part.isRequired),
173182
content: typeContent,
174-
};
183+
});
175184
},
176185
complex: (schema, typeName) => {
177186
const complexType = getComplexType(schema);
178187

179-
return {
188+
return attachParsedRef(schema, {
180189
$parsedSchema: true,
181190
schemaType: "complex",
182191
type: "type",
183192
typeIdentifier: "type",
184193
name: typeName,
185194
description: formatDescription(schema.description),
186195
content: complexSchemaParsers[complexType](schema),
187-
};
196+
});
188197
},
189198
primitive: (schema, typeName) => {
190199
let contentType = null;
@@ -195,7 +204,7 @@ const schemaParsers = {
195204
contentType = `Record<string, ${fieldType}>`;
196205
}
197206

198-
return {
207+
return attachParsedRef(schema, {
199208
$parsedSchema: true,
200209
schemaType: "primitive",
201210
type: "primitive",
@@ -204,7 +213,7 @@ const schemaParsers = {
204213
description: formatDescription(description),
205214
// TODO: probably it should be refactored. `type === 'null'` is not flexible
206215
content: type === "null" ? type : contentType || getType(schema),
207-
};
216+
});
208217
},
209218
};
210219

@@ -227,9 +236,9 @@ const parseSchema = (rawSchema, typeName, formattersMap) => {
227236
return rawSchema;
228237
}
229238

230-
if (rawSchema.$parsedSchema) {
231-
schemaType = rawSchema.schemaType;
232-
parsedSchema = rawSchema;
239+
if (rawSchema.$parsed) {
240+
schemaType = rawSchema.$parsed.schemaType;
241+
parsedSchema = rawSchema.$parsed;
233242
} else {
234243
const fixedRawSchema = checkAndFixSchema(rawSchema);
235244
schemaType = getInternalSchemaType(fixedRawSchema);

src/typeFormatters.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ const formatters = {
2828
return `${commonText}${result}`;
2929
}).join(""),
3030
type: (content) => {
31-
if (content.includes(" & ")) {
32-
return content.split(" & ").map(checkAndRenameModelName).join(" & ");
33-
}
31+
// const separators = [" & ", " | "];
3432

35-
if (content.includes(" | ")) {
36-
return content.split(" | ").map(checkAndRenameModelName).join(" | ");
37-
}
33+
// for (const separator of separators) {
34+
// if (content.includes(separator)) {
35+
// return content.split(separator).map(checkAndRenameModelName).join(separator);
36+
// }
37+
// }
3838

3939
return content;
4040
},

0 commit comments

Comments
 (0)