Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit 5ddc92e

Browse files
authored
v2 documentation improvements (#134)
* Adds top link section in endpoint docs, makes reponses h2 * Adds table of contents * Moves TOC to h2 * Adds and uses headerSize parameter * Removes anchorContainsPeriod * Adjusts request body header size when requestbody is refed * Updates request body header size for non refed request bodies * Removes header from request body component module * Moves code sample to bottom, adds h2 section for authorization, adds to toc * Adds description and summary sections * Uses headerSize in parameter docs * Fixes headerSize in parameter_doc.hbs * Has header docs use headerSize * Improves indentation in response bodies * Adjusts paramter schema depths * Removes deep headerSize passing in paramter_doc * dedents schema module in component schema docs * Flows headerSize into schema docs * Removes discription when there re no arguments * Removes auth section if there is no auth * Uses join for parameter schema docs * Adds header for each parameter * Removes redundant data addition * Header indentation updated in components * Adds body and header sections to response * Uses headerSize in response_doc * Fully uses headerSize in response_doc * Uses camelCase name in headings for headers and content * Adds response dscriptions to docs * Uses headerSize in request body doc * Adds request body description to docs * Adds descriptions to responses and request bodies * Fixes links to ref responses * Adds parameter and header descriptions * Adds component schema descriptions * Increases indentation depth in schema docs * Reduces composed indentation * Adds indentation * All sub lists use numbering * Removes empty lines from indented schema doc template * Fixes some tables * Actually indents subschemas * Indentation level corrected * Adds new lines back into schema doc indent template * Uses headerSize.length in schema_doc_indent * Removes trailing # from headings in indent * Adds newline bfore oneOf anyOf allOf Nots * Removes unneeded deeper indentation * Adds 1 level of depth to request body schema * Indents request body schema info under header schema class name * Improves endpoint response indentation * Fixes request body links in endpoint docs * Stops indenting schemas, lists them all at the same level * Inlines all schemas * Adds indentation under schema * Removes RequestBody prefix from request body description * Removes prefixes from response data * Moves header summary higher, adds header details removes response prefixes * Removes prefix from response headers when they are inline * Adds levels to header docs * Lists responses after response summary to keep header levels at h6 max * Removes description prefix from header docs * Updates parameter descriptions * Simplifies parameter and header docs * Simplifies response doc * Simplies request body doc template * Simplifies schema doc * Adds schema writing of anchors with ids containing json path info * Tweaks schema anchors * Moves anchor above heading * Fixes property links * Fixes required an optional schema docs links * Adds content type to schema request body section * Adds newArray and add helpers * Adds identifierPieces to schema rendering * Uses identifierPieces * Generates custom anchor * Adds anchorPiece * Uses anchorPiece in anchors * Sample regen * Removes manual anchor * Uses _helper_header_from_identifier_pieces * Adds Items component schema example * Adds sample schemas with collisions * Fixes doc colision in schema property docs * Simplifies schema doc template * Updates requestBody doc template * Simplifies request body docs * Omits self header prefix in schema and request body docs for inline schemas * Adjusts when suffix is added to links * passes in needed identifierPieces into response headers * Updates header docs * Adds header content type to schema section in docs * Updates most of codegen response docs * Changes content to body * Fixes template warning by passing in includeSuffix * Parameter docs updated, parameter docs have needed params * Adds Schema prefix to component schema h1 * Adds Response prefix to response docs * Adds prefix to other components * Adds headers prefix for response header docs * Adds path to operation docs * Adds path anchor link * Adds http method to operation docs * Operation requestbody heading updated * Removes template setting of var complexTypePrefix * Fixes rending of required and optional input type and accessed types for dict keys * Simplifies dict req and optional input type accessed type template * Fixes template access warning * Updates response docs to handle no resonse body schema use case * response body class updated when no content schemas are defined * Fixes doc links for self reverences * moved dict property schema link location in docs * Adds link to additional properties class in schema docs * Adds example that shows response body links are working * Samples regen * Removes unused templates * params updated * sample regen * Fixes locale invocations
1 parent a2d1df1 commit 5ddc92e

1,055 files changed

Lines changed: 44033 additions & 28317 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/openapi-json-schema-generator/src/main/java/org/openapijsonschematools/codegen/DefaultCodegen.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2569,11 +2569,14 @@ protected CodegenKey getOperationId(Operation operation, String path, String htt
25692569
operationId = String.join(removeOperationIdPrefixDelimiter, Arrays.copyOfRange(components, component_number, components.length));
25702570
}
25712571
}
2572+
String camelCase = toModelName(operationId);
2573+
String anchorPiece = camelCase.toLowerCase(Locale.ROOT);
25722574
return new CodegenKey(
25732575
operationId,
25742576
isValid(operationId),
25752577
getOperationIdSnakeCase(operationId),
2576-
toModelName(operationId)
2578+
camelCase,
2579+
anchorPiece
25772580
);
25782581
}
25792582

@@ -2634,11 +2637,14 @@ public CodegenOperation fromOperation(String path,
26342637
} else {
26352638
usedPath = path;
26362639
}
2640+
String camelCase = toModelName(path);
2641+
String anchorPiece = camelCase.toLowerCase(Locale.ROOT);
26372642
CodegenKey pathKey = new CodegenKey(
26382643
usedPath,
26392644
false, // false because paths have lots of invalid characters
26402645
toPathFilename(path),
2641-
toModelName(path)
2646+
camelCase,
2647+
anchorPiece
26422648
);
26432649
String sourceJsonPath = "#/paths/" + ModelUtils.encodeSlashes(pathKey.original) + "/" + httpMethod;
26442650

@@ -2809,12 +2815,14 @@ public CodegenOperation fromOperation(String path,
28092815
}
28102816
}
28112817
}
2812-
2818+
String camelCaseMethod = org.openapijsonschematools.codegen.utils.StringUtils.camelize(httpMethod);
2819+
String anchorPieceMethod = camelCase.toLowerCase(Locale.ROOT);
28132820
CodegenKey httpMethodKey = new CodegenKey(
28142821
httpMethod,
28152822
true,
28162823
org.openapijsonschematools.codegen.utils.StringUtils.underscore(httpMethod),
2817-
org.openapijsonschematools.codegen.utils.StringUtils.camelize(httpMethod)
2824+
camelCaseMethod,
2825+
anchorPieceMethod
28182826
);
28192827

28202828
// move "required" parameters in front of "optional" parameters
@@ -4522,6 +4530,7 @@ public CodegenKey getKey(String key, String expectedComponentType) {
45224530
boolean isValid = isValid(usedKey);
45234531
String snakeCaseName = null;
45244532
String camelCaseName = null;
4533+
String anchorPiece = null;
45254534
switch (expectedComponentType) {
45264535
case "schemas":
45274536
snakeCaseName = toModelFilename(usedKey);
@@ -4544,22 +4553,29 @@ public CodegenKey getKey(String key, String expectedComponentType) {
45444553
camelCaseName = getCamelCaseResponse(usedKey);
45454554
break;
45464555
}
4556+
if (camelCaseName != null) {
4557+
anchorPiece = camelCaseName.toLowerCase(Locale.ROOT);
4558+
}
45474559
return new CodegenKey(
45484560
usedKey,
45494561
isValid,
45504562
snakeCaseName,
4551-
camelCaseName
4563+
camelCaseName,
4564+
anchorPiece
45524565
);
45534566
}
45544567

45554568
public CodegenKey getKey(String key) {
45564569
String usedKey = handleSpecialCharacters(key);
45574570
boolean isValid = isValid(usedKey);
4571+
String camelCase = toModelName(usedKey);
4572+
String anchorPiece = camelCase.toLowerCase(Locale.ROOT);
45584573
return new CodegenKey(
45594574
usedKey,
45604575
isValid,
45614576
toModelFilename(usedKey),
4562-
toModelName(usedKey)
4577+
camelCase,
4578+
anchorPiece
45634579
);
45644580
}
45654581

modules/openapi-json-schema-generator/src/main/java/org/openapijsonschematools/codegen/DefaultGenerator.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ private void generateModelDocumentation(List<File> files, Map<String, Object> mo
395395
String docExtension = config.getDocExtension();
396396
String suffix = docExtension != null ? docExtension : config.modelDocTemplateFiles().get(templateName);
397397
String filename = config.modelDocFileFolder() + File.separator + config.toModelDocFilename(modelName) + suffix;
398+
modelData.put("headerSize", "#");
398399

399400
File written = processTemplateToFile(modelData, templateName, filename, generateModelDocumentation, CodegenConstants.MODEL_DOCS);
400401
if (written != null) {
@@ -572,16 +573,20 @@ void generatePaths(List<File> files, Map<String, List<CodegenOperation>> operati
572573

573574
}
574575
}
576+
// operation docs
575577
for (String templateFile: config.pathEndpointDocTemplateFiles()) {
576578
for (Map.Entry<String, CodegenTag> entry: co.tags.entrySet()) {
577579
CodegenTag tag = entry.getValue();
578580
Map<String, Object> endpointInfo = new HashMap<>();
579-
List<CodegenOperation> operation = Arrays.asList(co);
580-
endpointInfo.put("operation", operation);
581+
endpointInfo.put("operation", co);
581582
endpointInfo.put("packageName", packageName);
582583
endpointInfo.put("apiPackage", config.apiPackage());
583584
endpointInfo.put("basePath", basePath);
584585
endpointInfo.put("tag", tag);
586+
endpointInfo.put("headerSize", "#");
587+
endpointInfo.put("complexTypePrefix", "../../../components/schema/");
588+
endpointInfo.put("identifierPieces", Collections.unmodifiableList(new ArrayList<>()));
589+
endpointInfo.put("identifierToHeadingQty", new HashMap<>());
585590
outputFilename = filenameFromRoot(Arrays.asList("docs", config.apiPackage(), "tags", tag.moduleName, co.operationId.snakeCase + ".md"));
586591
apiDocFiles.add(Arrays.asList(endpointInfo, templateFile, outputFilename));
587592
}
@@ -800,6 +805,9 @@ private TreeMap<String, CodegenResponse> generateResponses(List<File> files) {
800805
Map<String, Object> templateData = new HashMap<>();
801806
templateData.put("packageName", config.packageName());
802807
templateData.put("complexTypePrefix", "../../components/schema/");
808+
templateData.put("headerSize", "#");
809+
templateData.put("identifierPieces", Collections.unmodifiableList(new ArrayList<>()));
810+
templateData.put("identifierToHeadingQty", new HashMap<>());
803811
templateData.put("response", response);
804812
try {
805813
File written = processTemplateToFile(templateData, templateName, filename, generateResponseDocumentation, CodegenConstants.REQUEST_BODY_DOCS);
@@ -821,7 +829,6 @@ private void generateRequestBody(List<File> files, CodegenRequestBody requestBod
821829
Map<String, Object> templateData = new HashMap<>();
822830
templateData.put("packageName", config.packageName());
823831
templateData.put("requestBody", requestBody);
824-
templateData.put("docRoot", "../../");
825832
Boolean generateRequestBodies = Boolean.TRUE;
826833
Map<String, String> templateInfo = config.jsonPathTemplateFiles().get(CodegenConstants.JSON_PATH_LOCATION_TYPE.REQUEST_BODY);
827834
if (templateInfo != null && !templateInfo.isEmpty()) {
@@ -852,7 +859,10 @@ private void generateRequestBody(List<File> files, CodegenRequestBody requestBod
852859
return;
853860
}
854861
// doc generation
855-
Boolean generateRequestBodyDocumentaion = Boolean.TRUE;
862+
Boolean generateRequestBodyDocumentation = Boolean.TRUE;
863+
templateData.put("headerSize", "#");
864+
templateData.put("identifierPieces", Collections.unmodifiableList(new ArrayList<>()));
865+
templateData.put("identifierToHeadingQty", new HashMap<>());
856866
String componentName = jsonPath.substring(jsonPath.lastIndexOf("/") + 1);
857867
for (Map.Entry<String, String> entry: config.requestBodyDocTemplateFiles().entrySet()) {
858868
String templateName = entry.getKey();
@@ -862,7 +872,7 @@ private void generateRequestBody(List<File> files, CodegenRequestBody requestBod
862872

863873
templateData.put("complexTypePrefix", "../../components/schema/");
864874
try {
865-
File written = processTemplateToFile(templateData, templateName, filename, generateRequestBodyDocumentaion, CodegenConstants.REQUEST_BODY_DOCS);
875+
File written = processTemplateToFile(templateData, templateName, filename, generateRequestBodyDocumentation, CodegenConstants.REQUEST_BODY_DOCS);
866876
if (written != null) {
867877
files.add(written);
868878
if (config.isEnablePostProcessFile() && !dryRun) {
@@ -962,6 +972,9 @@ private TreeMap<String, CodegenParameter> generateParameters(List<File> files) {
962972
Map<String, Object> templateData = new HashMap<>();
963973
templateData.put("packageName", config.packageName());
964974
templateData.put("parameter", parameter);
975+
templateData.put("headerSize", "#");
976+
templateData.put("identifierPieces", Collections.unmodifiableList(new ArrayList<>()));
977+
templateData.put("identifierToHeadingQty", new HashMap<>());
965978
templateData.put("complexTypePrefix", "../../components/schema/");
966979

967980
try {
@@ -1066,6 +1079,7 @@ private TreeMap<String, CodegenHeader> generateHeaders(List<File> files) {
10661079

10671080
generateHeader(files, header, sourceJsonPath);
10681081

1082+
// documentation
10691083
Boolean generateHeaderDocs = Boolean.TRUE;
10701084
for (Map.Entry<String, String> headerDocInfo : config.headerDocTemplateFiles().entrySet()) {
10711085
String templateName = headerDocInfo.getKey();
@@ -1075,8 +1089,12 @@ private TreeMap<String, CodegenHeader> generateHeaders(List<File> files) {
10751089
Map<String, Object> templateData = new HashMap<>();
10761090
templateData.put("packageName", config.packageName());
10771091
templateData.put("header", header);
1092+
templateData.put("headerSize", "#");
10781093
templateData.put("complexTypePrefix", "../../components/schema/");
10791094
templateData.put("docRoot", "../../");
1095+
templateData.put("identifierPieces", Collections.unmodifiableList(new ArrayList<>()));
1096+
templateData.put("identifierToHeadingQty", new HashMap<>());
1097+
10801098

10811099
try {
10821100
File written = processTemplateToFile(templateData, templateName, filename, generateHeaderDocs, CodegenConstants.HEADER_DOCS, fileFolder);
@@ -1161,6 +1179,8 @@ protected TreeMap<String, CodegenSchema> generateSchemas(List<File> files) {
11611179
generateModelTests(files, schemaData, componentName);
11621180

11631181
// to generate model documentation files
1182+
schemaData.put("identifierPieces", Collections.unmodifiableList(new ArrayList<>()));
1183+
schemaData.put("identifierToHeadingQty", new HashMap<>());
11641184
generateModelDocumentation(files, schemaData, componentName);
11651185

11661186
} catch (Exception e) {

modules/openapi-json-schema-generator/src/main/java/org/openapijsonschematools/codegen/model/CodegenKey.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ public class CodegenKey {
77
public final boolean isValid;
88
public final String snakeCase;
99
public final String camelCase;
10+
public final String anchorPiece;
1011

11-
public CodegenKey(String original, boolean isValid, String snakeCase, String camelCase) {
12+
public CodegenKey(String original, boolean isValid, String snakeCase, String camelCase, String anchorPiece) {
1213
this.original = original;
1314
this.isValid = isValid;
1415
this.snakeCase = snakeCase;
1516
this.camelCase = camelCase;
17+
this.anchorPiece = anchorPiece;
1618
}
1719

1820
@Override
@@ -23,11 +25,12 @@ public boolean equals(Object o) {
2325
return Objects.equals(original, that.original) &&
2426
Objects.equals(isValid, that.isValid) &&
2527
Objects.equals(snakeCase, that.snakeCase) &&
26-
Objects.equals(camelCase, that.camelCase);
28+
Objects.equals(camelCase, that.camelCase) &&
29+
Objects.equals(anchorPiece, that.anchorPiece);
2730
}
2831

2932
@Override
3033
public int hashCode() {
31-
return Objects.hash(original, isValid, snakeCase, camelCase);
34+
return Objects.hash(original, isValid, snakeCase, camelCase, anchorPiece);
3235
}
3336
}

modules/openapi-json-schema-generator/src/main/java/org/openapijsonschematools/codegen/model/CodegenOperation.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
public class CodegenOperation {
2727
public final Boolean deprecated;
2828
public final boolean hasErrorResponseObject; // if 4xx, 5xx responses have at least one error object defined
29-
public final String summary, unescapedNotes, notes;
29+
public final String summary, unescapedDescription, description;
3030
public final CodegenKey httpMethod;
3131
public final CodegenKey path;
3232
public final LinkedHashSet<String> produces;
@@ -51,12 +51,12 @@ public class CodegenOperation {
5151
public final Map<String, Object> vendorExtensions;
5252
public final CodegenKey operationId;
5353

54-
public CodegenOperation(Boolean deprecated, boolean hasErrorResponseObject, String summary, String unescapedNotes, String notes, CodegenKey httpMethod, CodegenKey path, LinkedHashSet<String> produces, List<CodegenServer> servers, CodegenRequestBody requestBody, List<CodegenParameter> allParams, List<CodegenParameter> pathParams, List<CodegenParameter> queryParams, List<CodegenParameter> headerParams, List<CodegenParameter> cookieParams, boolean hasRequiredParamOrBody, boolean hasOptionalParamOrBody, List<CodegenSecurity> authMethods, Map<String, CodegenTag> tags, TreeMap<String, CodegenResponse> responses, TreeMap<Integer, CodegenResponse> statusCodeResponses, TreeMap<Integer, CodegenResponse> wildcardCodeResponses, TreeMap<String, CodegenResponse> nonDefaultResponses, CodegenResponse defaultResponse, List<CodegenCallback> callbacks, ExternalDocumentation externalDocs, Map<String, Object> vendorExtensions, CodegenKey operationId) {
54+
public CodegenOperation(Boolean deprecated, boolean hasErrorResponseObject, String summary, String unescapedDescription, String description, CodegenKey httpMethod, CodegenKey path, LinkedHashSet<String> produces, List<CodegenServer> servers, CodegenRequestBody requestBody, List<CodegenParameter> allParams, List<CodegenParameter> pathParams, List<CodegenParameter> queryParams, List<CodegenParameter> headerParams, List<CodegenParameter> cookieParams, boolean hasRequiredParamOrBody, boolean hasOptionalParamOrBody, List<CodegenSecurity> authMethods, Map<String, CodegenTag> tags, TreeMap<String, CodegenResponse> responses, TreeMap<Integer, CodegenResponse> statusCodeResponses, TreeMap<Integer, CodegenResponse> wildcardCodeResponses, TreeMap<String, CodegenResponse> nonDefaultResponses, CodegenResponse defaultResponse, List<CodegenCallback> callbacks, ExternalDocumentation externalDocs, Map<String, Object> vendorExtensions, CodegenKey operationId) {
5555
this.deprecated = deprecated;
5656
this.hasErrorResponseObject = hasErrorResponseObject;
5757
this.summary = summary;
58-
this.unescapedNotes = unescapedNotes;
59-
this.notes = notes;
58+
this.unescapedDescription = unescapedDescription;
59+
this.description = description;
6060
this.httpMethod = httpMethod;
6161
this.path = path;
6262
this.produces = produces;
@@ -131,8 +131,8 @@ public String toString() {
131131
sb.append(", operationId='").append(operationId).append('\'');
132132
sb.append(", httpMethod='").append(httpMethod).append('\'');
133133
sb.append(", summary='").append(summary).append('\'');
134-
sb.append(", unescapedNotes='").append(unescapedNotes).append('\'');
135-
sb.append(", notes='").append(notes).append('\'');
134+
sb.append(", unescapedNotes='").append(unescapedDescription).append('\'');
135+
sb.append(", notes='").append(description).append('\'');
136136
sb.append(", defaultResponse='").append(defaultResponse).append('\'');
137137
sb.append(", produces=").append(produces);
138138
sb.append(", servers=").append(servers);
@@ -167,8 +167,8 @@ public boolean equals(Object o) {
167167
Objects.equals(operationId, that.operationId) &&
168168
Objects.equals(httpMethod, that.httpMethod) &&
169169
Objects.equals(summary, that.summary) &&
170-
Objects.equals(unescapedNotes, that.unescapedNotes) &&
171-
Objects.equals(notes, that.notes) &&
170+
Objects.equals(unescapedDescription, that.unescapedDescription) &&
171+
Objects.equals(description, that.description) &&
172172
Objects.equals(defaultResponse, that.defaultResponse) &&
173173
Objects.equals(produces, that.produces) &&
174174
Objects.equals(servers, that.servers) &&
@@ -195,7 +195,7 @@ public boolean equals(Object o) {
195195
public int hashCode() {
196196

197197
return Objects.hash(deprecated, path, operationId, httpMethod,
198-
summary, unescapedNotes, notes, defaultResponse,
198+
summary, unescapedDescription, description, defaultResponse,
199199
produces, servers, requestBody, allParams,
200200
pathParams, queryParams, headerParams, cookieParams, hasRequiredParamOrBody, hasOptionalParamOrBody,
201201
authMethods, tags, responses, callbacks, externalDocs,

modules/openapi-json-schema-generator/src/main/java/org/openapijsonschematools/codegen/model/CodegenResponse.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,41 @@ public CodegenResponse(CodegenKey jsonPathPiece, Map<String, CodegenHeader> head
4141
this.componentModule = componentModule;
4242
}
4343

44+
/**
45+
* Used by templates to only render body details if there is a schema defined for a content type
46+
* @return true if there is an inline header
47+
*/
48+
public boolean hasContentSchema() {
49+
if (content == null) {
50+
return false;
51+
}
52+
for (CodegenMediaType mediaType: content.values()) {
53+
if (mediaType == null) {
54+
continue;
55+
}
56+
if (mediaType.schema != null) {
57+
return true;
58+
}
59+
}
60+
return false;
61+
}
62+
63+
/**
64+
* Used by templates to only render header details if there is an inline header
65+
* @return true if there is an inline header
66+
*/
67+
public boolean hasInlineHeader() {
68+
if (headers == null) {
69+
return false;
70+
}
71+
for (CodegenHeader header: headers.values()) {
72+
if (header.refInfo == null) {
73+
return true;
74+
}
75+
}
76+
return false;
77+
}
78+
4479
@Override
4580
public int hashCode() {
4681
return Objects.hash(jsonPathPiece, description,

modules/openapi-json-schema-generator/src/main/java/org/openapijsonschematools/codegen/templating/HandlebarsEngineAdapter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.github.jknack.handlebars.io.TemplateSource;
3333
import org.openapijsonschematools.codegen.api.AbstractTemplatingEngineAdapter;
3434
import org.openapijsonschematools.codegen.api.TemplatingExecutor;
35+
import org.openapijsonschematools.codegen.templating.handlebars.CustomHelpers;
3536
import org.slf4j.Logger;
3637
import org.slf4j.LoggerFactory;
3738

@@ -87,7 +88,7 @@ public TemplateSource sourceAt(String location) {
8788
StringHelpers.register(handlebars);
8889
handlebars.registerHelpers(ConditionalHelpers.class);
8990
handlebars.registerHelpers(org.openapijsonschematools.codegen.templating.handlebars.StringHelpers.class);
90-
handlebars.registerHelpers(org.openapijsonschematools.codegen.templating.handlebars.ContainsHelper.class);
91+
handlebars.registerHelpers(CustomHelpers.class);
9192
handlebars.setInfiniteLoops(infiniteLoops);
9293
handlebars.setPrettyPrint(prettyPrint);
9394
Template tmpl = handlebars.compile(templateFile);

0 commit comments

Comments
 (0)