Skip to content

Commit

Permalink
Component - Allow undefined values for integers and numbers if input …
Browse files Browse the repository at this point in the history
…not required

Remove newlines to reduce noise in mr

Logical grouping
  • Loading branch information
jasoncomes committed Aug 5, 2024
1 parent 97f31cb commit a781ddc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prismatic-io/prism",
"version": "7.0.2",
"version": "7.0.3",
"description": "Build, deploy, and support integrations in Prismatic from the comfort of your command line",
"keywords": ["prismatic", "cli"],
"homepage": "https://prismatic.io",
Expand Down
42 changes: 23 additions & 19 deletions src/generate/formats/readers/openapi/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ const toInputType: {
type: InputFieldType;
cleanFn: string;
cleanReturnType: string;
coalesceFalsyValues: boolean;
allowOptional: boolean;
};
} = {
string: {
type: "string",
cleanFn: "toString",
cleanReturnType: "string | undefined",
coalesceFalsyValues: true,
cleanReturnType: "string",
allowOptional: true,
},
integer: {
type: "string",
cleanFn: "toNumber",
cleanReturnType: "number",
coalesceFalsyValues: false,
allowOptional: true,
},
number: {
type: "string",
cleanFn: "toNumber",
cleanReturnType: "number",
coalesceFalsyValues: false,
allowOptional: true,
},
boolean: {
type: "boolean",
cleanFn: "toBool",
cleanReturnType: "boolean",
coalesceFalsyValues: false,
allowOptional: false,
},
};

Expand Down Expand Up @@ -71,29 +71,31 @@ const buildInput = (parameter: OpenAPI.Parameter, seenKeys: Set<string>): Input
}

const schemaType = parameter.schema?.type;
const { type, cleanFn, cleanReturnType, coalesceFalsyValues } =
const { type, cleanFn, cleanReturnType, allowOptional } =
toInputType[schemaType] ?? toInputType.string;
const coalescePart = coalesceFalsyValues ? " || undefined" : "";

const required = parameter.required || !allowOptional;
const clean = required
? `(value): ${cleanReturnType} => util.types.${cleanFn}(value)`
: `(value): ${cleanReturnType} | undefined => value !== undefined && value !== null ? util.types.${cleanFn}(value) : undefined`;

const { name: paramKey } = parameter;
const key = seenKeys.has(cleanIdentifier(paramKey))
? cleanIdentifier(`other ${paramKey}`)
: cleanIdentifier(paramKey);
seenKeys.add(key);

const model = getInputModel(parameter.schema);

const input = stripUndefined<Input>({
upstreamKey: paramKey,
key,
label: startCase(paramKey),
type,
required: parameter.required,
required,
comments: parameter.description,
default: parameter.schema?.default,
example: parameter.schema?.example,
model,
clean: `(value): ${cleanReturnType} => util.types.${cleanFn}(value)${coalescePart}`,
model: getInputModel(parameter.schema),
clean,
});
return input;
};
Expand All @@ -118,11 +120,13 @@ const buildBodyInputs = (
.filter(([, prop]) => !prop.readOnly) // Don't create inputs for readonly properties
.map<Input>(([propKey, prop]) => {
const schemaType = prop?.type;
const { type, cleanFn, cleanReturnType, coalesceFalsyValues } =
const { type, cleanFn, cleanReturnType, allowOptional } =
toInputType[schemaType as string] ?? toInputType.string;
const coalescePart = coalesceFalsyValues ? " || undefined" : "";

const model = getInputModel(prop);
const required = requiredKeys.has(propKey) || !allowOptional;
const clean = required
? `(value): ${cleanReturnType} => util.types.${cleanFn}(value)`
: `(value): ${cleanReturnType} | undefined => value !== undefined && value !== null ? util.types.${cleanFn}(value) : undefined`;

const key = seenKeys.has(cleanIdentifier(propKey))
? cleanIdentifier(`other ${propKey}`)
Expand All @@ -134,12 +138,12 @@ const buildBodyInputs = (
key,
label: startCase(propKey),
type,
required: requiredKeys.has(propKey),
required,
comments: prop.description,
default: prop.default,
example: prop.example,
model,
clean: `(value): ${cleanReturnType} => util.types.${cleanFn}(value)${coalescePart}`,
model: getInputModel(prop),
clean,
});
});
};
Expand Down

0 comments on commit a781ddc

Please sign in to comment.