Skip to content

Commit

Permalink
feat: handle multiline description
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Jul 2, 2023
1 parent f71bfa9 commit 4f8d0ac
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ cli.command("<input>", "path/url to OpenAPI/Swagger document as json/yaml")
"When true, will make all properties of an object required by default (rather than the current opposite), unless an explicitly `required` array is set"
)
.option("--with-deprecated", "when true, will keep deprecated endpoints in the api output")
.option("--with-description", "when true, will add z.describe(xxx)")
.option("--with-description", "when true, will add z.describe(xxx), 'multiline' will keep newlines as is")
.option(
"--group-strategy",
"groups endpoints by a given strategy, possible values are: 'none' | 'tag' | 'method' | 'tag-file' | 'method-file'"
Expand Down
5 changes: 2 additions & 3 deletions lib/src/getZodiosEndpointDefinitionList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ export const getZodiosEndpointDefinitionList = (doc: OpenAPIObject, options?: Te
);
}


// this fallback is needed to autofix openapi docs that put the $ref in the wrong place
// (it should be in the mediaTypeObject.schema, not in the mediaTypeObject itself)
// https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#style-values (just above this anchor)
Expand All @@ -238,8 +237,8 @@ export const getZodiosEndpointDefinitionList = (doc: OpenAPIObject, options?: Te
: paramItem.schema;
}

if (options?.withDescription && paramSchema) {
(paramSchema as SchemaObject).description = (paramItem.description ?? "")?.replace("\n", "");
if (options?.withDescription && paramSchema && paramItem.description) {
(paramSchema as SchemaObject).description = paramItem.description;
}

// resolve ref if needed, and fallback to default (unknown) value if needed
Expand Down
6 changes: 4 additions & 2 deletions lib/src/openApiToZod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,10 @@ export const getZodChain = ({ schema, meta, options }: ZodChainArgs) => {
.with("array", () => chains.push(getZodChainableArrayValidations(schema)))
.otherwise(() => void 0);

if (typeof schema.description === "string" && schema.description !== "" && options?.withDescription) {
chains.push(`describe("${schema.description}")`);
const desc = schema.description;
if (options?.withDescription && typeof desc === "string" && desc !== "") {
const result = options?.withDescription === "multiline" ? desc : desc?.replaceAll(/\n\s*/g, " ");
chains.push(`describe(\`${result}\`)`);
}

const output = chains
Expand Down
8 changes: 6 additions & 2 deletions lib/src/template-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,12 @@ export type TemplateContextOptions = {
defaultStatusBehavior?: "spec-compliant" | "auto-correct";
willSuppressWarnings?: boolean;
/**
* when true, will add z.describe(xxx)
* adds z.describe(xxx).
*
* - when true, will truncate newlines and trailing spaces into a single space.
* - when "multiline", will not truncate newlines.
*
* @see https://github.com/astahmer/openapi-zod-client/pull/143
*/
withDescription?: boolean;
withDescription?: boolean | "multiline";
};

0 comments on commit 4f8d0ac

Please sign in to comment.