Skip to content

Commit

Permalink
Merge pull request #199 from bitgopatmcl/elide-empty-descriptions-whe…
Browse files Browse the repository at this point in the history
…re-possible

fix: elide empty descriptions when allowed by the spec
  • Loading branch information
ericcrosson-bitgo authored Aug 25, 2022
2 parents 796d7fe + 399f3c6 commit 98b92b9
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 76 deletions.
1 change: 0 additions & 1 deletion packages/openapi-generator/corpus/test-array-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const Routes = h.apiSpec({
"/test": {
"get": {
"summary": "MyRoute",
"description": "",
"parameters": [],
"responses": {
"200": {
Expand Down
1 change: 0 additions & 1 deletion packages/openapi-generator/corpus/test-boolean-literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const Routes = h.apiSpec({
"/test": {
"get": {
"summary": "MyRoute",
"description": "",
"parameters": [],
"responses": {
"200": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export const Routes = h.apiSpec({
"/test": {
"get": {
"summary": "MyRoute",
"description": "",
"parameters": [],
"responses": {
"200": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const Routes = h.apiSpec({
"/test": {
"get": {
"summary": "MyRoute",
"description": "",
"parameters": [],
"responses": {
"200": {
Expand Down
2 changes: 0 additions & 2 deletions packages/openapi-generator/corpus/test-multi-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export const Routes = h.apiSpec({
"/test/{id}/first": {
"get": {
"summary": "FirstRoute",
"description": "",
"parameters": [
{
"name": "horse",
Expand Down Expand Up @@ -115,7 +114,6 @@ export const Routes = h.apiSpec({
"/test/{id}/second": {
"get": {
"summary": "SecondRoute",
"description": "",
"parameters": [
{
"name": "horse",
Expand Down
1 change: 0 additions & 1 deletion packages/openapi-generator/corpus/test-multi-union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const Routes = h.apiSpec({
"/test": {
"get": {
"summary": "MyRoute",
"description": "",
"parameters": [],
"responses": {
"200": {
Expand Down
1 change: 0 additions & 1 deletion packages/openapi-generator/corpus/test-null-param.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const Routes = h.apiSpec({
"/test": {
"get": {
"summary": "MyRoute",
"description": "",
"parameters": [],
"responses": {
"200": {
Expand Down
3 changes: 0 additions & 3 deletions packages/openapi-generator/corpus/test-optional-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ export const Routes = h.apiSpec({
"/test": {
"get": {
"summary": "MyRoute",
"description": "",
"parameters": [
{
"name": "req",
"description": "",
"schema": {
"type": "string"
},
Expand All @@ -51,7 +49,6 @@ export const Routes = h.apiSpec({
},
{
"name": "opt",
"description": "",
"schema": {
"type": "string"
},
Expand Down
1 change: 0 additions & 1 deletion packages/openapi-generator/corpus/test-record-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const Routes = h.apiSpec({
"/test": {
"get": {
"summary": "MyRoute",
"description": "",
"parameters": [],
"responses": {
"200": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export const Routes = h.apiSpec({
"/test/{id}": {
"get": {
"summary": "FirstRoute",
"description": "",
"parameters": [
{
"name": "id",
Expand All @@ -86,7 +85,6 @@ export const Routes = h.apiSpec({
},
"post": {
"summary": "SecondRoute",
"description": "",
"parameters": [
{
"name": "id",
Expand Down
1 change: 0 additions & 1 deletion packages/openapi-generator/corpus/test-string-union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const Routes = h.apiSpec({
"/test": {
"get": {
"summary": "MyRoute",
"description": "",
"parameters": [],
"responses": {
"200": {
Expand Down
1 change: 0 additions & 1 deletion packages/openapi-generator/corpus/test-unknown-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export const Routes = h.apiSpec({
"/test/{id}": {
"post": {
"summary": "MyRoute",
"description": "",
"parameters": [
{
"name": "id",
Expand Down
1 change: 0 additions & 1 deletion packages/openapi-generator/corpus/test-version-tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export const Routes = h.apiSpec({
"/test": {
"get": {
"summary": "MyRoute",
"description": "",
"parameters": [],
"responses": {
"200": {
Expand Down
108 changes: 53 additions & 55 deletions packages/openapi-generator/src/project.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { flow, pipe } from 'fp-ts/function';
import { pipe } from 'fp-ts/function';
import * as A from 'fp-ts/Alt';
import * as RA from 'fp-ts/ReadonlyArray';
import * as E from 'fp-ts/Either';
Expand Down Expand Up @@ -55,65 +55,63 @@ const routesForSymbol =
variableDeclarationOfSymbol(sym),
E.chain(schemaForApiSpec(memo)),
E.map(
flow(
RA.reduce(
{},
(
paths: PathSpec,
{
path,
method,
query,
params,
body,
responses,
summary,
description,
isPrivate,
},
) =>
isPrivate && !includeInternal
? paths
: {
...paths,
[path]: {
...paths[path],
[method]: {
summary,
description,
...(isPrivate ? { 'x-internal': true } : {}),
parameters: [
...(query as ParameterList),
...(params as ParameterList),
],
responses: responses.reduce<OpenAPIV3_1.ResponsesObject>(
(acc, { code, schema: { schema } }) => ({
...acc,
[code]: {
description: '',
content: {
'application/json': { schema },
},
RA.reduce(
{},
(
paths: PathSpec,
{
path,
method,
query,
params,
body,
responses,
summary,
description,
isPrivate,
},
) =>
isPrivate && !includeInternal
? paths
: {
...paths,
[path]: {
...paths[path],
[method]: {
summary,
...(description !== undefined ? { description } : {}),
...(isPrivate ? { 'x-internal': true } : {}),
parameters: [
...(query as ParameterList),
...(params as ParameterList),
],
responses: responses.reduce<OpenAPIV3_1.ResponsesObject>(
(acc, { code, schema: { schema } }) => ({
...acc,
[code]: {
description: '', // DISCUSS: This field actually is required, is there a better default for this?
content: {
'application/json': { schema },
},
}),
{},
),
...(body !== undefined
? {
requestBody: {
content: {
'application/json': {
schema: body.schema,
},
},
}),
{},
),
...(body !== undefined
? {
requestBody: {
content: {
'application/json': {
schema: body.schema,
},
required: body.required,
},
}
: {}),
},
required: body.required,
},
}
: {}),
},
},
),
},
),
),
);
Expand Down
10 changes: 6 additions & 4 deletions packages/openapi-generator/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ const parametersFromCodecOutputSym = (paramIn: 'query' | 'path') =>
schema,
required,
in: paramIn,
description: description[description.length - 1] ?? '',
...(description.length > 0
? { description: description[description.length - 1] }
: {}),
})),
),
),
Expand Down Expand Up @@ -150,15 +152,15 @@ const routeSummary = (init: Node) => {
}
};

const routeDescription = (init: Node) => {
const routeDescription = (init: Node): { description?: string; isPrivate: boolean } => {
return pipe(
E.fromNullable('No symbol for initializer')(init.getSymbol()),
E.chain((sym) =>
E.fromNullable('No value declaration')(
(sym.getAliasedSymbol() ?? sym).getValueDeclaration(),
),
),
E.chain((decl) => {
E.chain((decl): E.Either<string, { description?: string; isPrivate: boolean }> => {
let current: Node | undefined = decl;
while (current !== undefined) {
const comments = current.getLeadingCommentRanges();
Expand All @@ -182,7 +184,7 @@ const routeDescription = (init: Node) => {
}
return E.left('no comment found');
}),
E.getOrElse(() => ({ description: '', isPrivate: false })),
E.getOrElseW(() => ({ isPrivate: false })),
);
};

Expand Down

0 comments on commit 98b92b9

Please sign in to comment.