Skip to content

Commit

Permalink
Added a parseOption for keeping up with backward compatibility
Browse files Browse the repository at this point in the history
Added 2 parseOptions considering the current behaviour when resolve set to true -
1> resolveRequestBody - default is false
2> resolveResponses - default is true

These 2 options will be applicable only when resolve is true.
  • Loading branch information
mma5997 authored and frantuma committed Apr 27, 2022
1 parent 81e79e2 commit 639bafb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ modules/swagger-parser/src/test/resources/relative-file-references/yaml
**/test-output/*
dependency-reduced-pom.xml
*.pyc
/bin/
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public class ParseOptions {
private boolean validateExternalRefs = false;
private boolean validateInternalRefs = true;
private boolean legacyYamlDeserialization = false;

private boolean resolveRequestBody = false;
private boolean resolveResponses = true;

public boolean isResolve() {
return resolve;
}
Expand All @@ -36,6 +38,30 @@ public boolean isResolveFully() {
public void setResolveFully(boolean resolveFully) {
this.resolveFully = resolveFully;
}

public boolean isResolveRequestBody() {
return resolveRequestBody;
}

/**
* If set to true, will help resolving the requestBody as inline, provided resolve is also set to true.
* Default is false because of the existing behaviour.
*/
public void setResolveRequestBody(boolean resolveRequestBody) {
this.resolveRequestBody = resolveRequestBody;
}

public boolean isResolveResponses() {
return resolveResponses;
}

/**
* If set to true, will help resolving the responses as inline, provided resolve is also set to true.
* Default is true because of the existing behaviour.
*/
public void setResolveResponses(boolean resolveResponses) {
this.resolveResponses = resolveResponses;
}

public boolean isFlatten() { return flatten; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@ public class ResolverCache {
private Map<String, String> externalFileCache = new HashMap<>();
private List<String> referencedModelKeys = new ArrayList<>();
private Set<String> resolveValidationMessages;

private final ParseOptions parseOptions;


/*
/*
a map that stores original external references, and their associated renamed references
*/
private Map<String, String> renameCache = new HashMap<>();
Expand Down Expand Up @@ -394,4 +393,8 @@ public Map<String, String> getExternalFileCache() {
public Map<String, String> getRenameCache() {
return Collections.unmodifiableMap(renameCache);
}

public ParseOptions getParseOptions() {
return parseOptions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public OperationProcessor(ResolverCache cache, OpenAPI openAPI) {
this.responseProcessor = new ResponseProcessor(cache,openAPI);
this.requestBodyProcessor = new RequestBodyProcessor(cache,openAPI);
this.externalRefProcessor = new ExternalRefProcessor(cache, openAPI);

this.cache = cache;
}

Expand All @@ -42,8 +41,8 @@ public void processOperation(Operation operation) {
}
RequestBody requestBody = operation.getRequestBody();
if(requestBody != null) {
//This part allows paser to put requestBody schema inline without the resolveFully option set to true
if (requestBody.get$ref() != null) {
//This part allows paser to put requestBody inline without the resolveFully option set to true
if (requestBody.get$ref() != null && cache != null && cache.getParseOptions() != null && cache.getParseOptions().isResolveRequestBody()) {
requestBodyProcessor.processRequestBody(requestBody);
RefFormat refFormat = computeRefFormat(requestBody.get$ref());
RequestBody resolvedRequestBody = cache.loadRef(requestBody.get$ref(), refFormat, RequestBody.class);
Expand All @@ -62,8 +61,8 @@ public void processOperation(Operation operation) {
for (String responseCode : responses.keySet()) {
ApiResponse response = responses.get(responseCode);
if(response != null) {
//This part allows parser to put response schema inline without the resolveFully option set to true
if (response.get$ref() != null) {
//This part allows parser to put response inline without the resolveFully option set to true
if (response.get$ref() != null && cache != null && cache.getParseOptions() != null && cache.getParseOptions().isResolveResponses()) {

responseProcessor.processResponse(response);

Expand Down

0 comments on commit 639bafb

Please sign in to comment.