Skip to content

Added support for openapi-normalizer in the online version. #19336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.Map;

@Setter
Expand All @@ -31,6 +32,8 @@ public class GeneratorInput {
@Getter private Map<String, String> options;
private String openAPIUrl;
@Getter private AuthorizationValue authorizationValue;
//FILTER=operationId:updatePet
@Getter private List<String> openapiNormalizer;

@ApiModelProperty(example = "https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml")
public String getOpenAPIUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ private static String generate(String language, GeneratorInput opts, Type type)
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "The OpenAPI specification supplied was not valid");
}

// if(opts.getOpenapiNormalizer() != null && !opts.getOpenapiNormalizer().isEmpty()){
// for(String ruleString: opts.getOpenapiNormalizer()) {
//
// }
// }

// do not use opts.getOptions().get("outputFolder") as the input can contain ../../
// to access other folders in the server
String destPath = language + "-" + type.getTypeName();
Expand All @@ -131,6 +137,7 @@ private static String generate(String language, GeneratorInput opts, Type type)
CodegenConfig codegenConfig;
try {
codegenConfig = CodegenConfigLoader.forName(language);

} catch (RuntimeException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Unsupported target " + language + " supplied");
}
Expand All @@ -140,6 +147,17 @@ private static String generate(String language, GeneratorInput opts, Type type)
codegenConfig.additionalProperties().put("openAPI", openapi);
}

if(opts.getOpenapiNormalizer() != null && !opts.getOpenapiNormalizer().isEmpty()){
for(String rule: opts.getOpenapiNormalizer()){

String[] ruleOperands = rule.split("=");
if(ruleOperands.length != 2) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "In rule: " + rule + "the operands were not provided in the form of <Rule>=<Value>");
}
codegenConfig.openapiNormalizer().put(ruleOperands[0],ruleOperands[1]);
}
}

codegenConfig.setOutputDir(outputFolder);

clientOptInput.config(codegenConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.util.Assert;

import java.util.concurrent.atomic.AtomicReference;

import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not;
Expand Down Expand Up @@ -133,4 +136,32 @@ public void generateClientWithInvalidOpenAPIUrl() throws Exception {
.content("{\"openAPIUrl\": \"" + invalidOpenAPIUrl + "\"}"))
.andExpect(status().isBadRequest());
}

@Test
public void generateWithOpenAPINormalizer() throws Exception {
String withOpenAPINormalizer = "{\"openAPIUrl\":\"https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml\",\"openapiNormalizer\":[\"FILTER=operationId:updatePet\"],\"options\":{},\"spec\":{}}";
String withoutOpenAPINormalizer = "{\"openAPIUrl\":\"https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml\",\"options\":{},\"spec\":{}}";

String responseOfNormalized = mockMvc.perform(post("http://test.com:1234/api/gen/clients/java")
.contentType(MediaType.APPLICATION_JSON)
.content(withOpenAPINormalizer))
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
String codeOfNormalized = new ObjectMapper().readValue(responseOfNormalized, ResponseCode.class).getCode();
Long lengthOfNormalized = Long.parseLong(mockMvc.perform(get("http://test.com:1234/api/gen/download/" + codeOfNormalized))
.andExpect(content().contentType("application/zip"))
.andExpect(status().isOk()).andReturn().getResponse().getHeader("Content-Length"));

String responseOfNotNormalized = mockMvc.perform(post("http://test.com:1234/api/gen/clients/java")
.contentType(MediaType.APPLICATION_JSON)
.content(withoutOpenAPINormalizer))
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();

String codeOfNotNormalized = new ObjectMapper().readValue(responseOfNotNormalized, ResponseCode.class).getCode();
Long lengthOfNotNormalized = Long.parseLong(mockMvc.perform(get("http://test.com:1234/api/gen/download/" + codeOfNotNormalized))
.andExpect(content().contentType("application/zip"))
.andExpect(status().isOk()).andReturn().getResponse().getHeader("Content-Length"));

Assert.isTrue(lengthOfNormalized <= lengthOfNotNormalized,"Using the normalizer should result in a smaller or equal file size");

}
}
Loading