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

* Added support for openapi-normalizer in the online version with a minimal test.

* removed comments and formatted code
This commit is contained in:
GeorgeFkd 2024-08-12 12:49:34 +03:00 committed by GitHub
parent fdd2dc9651
commit 2107e9ef8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 0 deletions

View File

@ -23,6 +23,7 @@ import io.swagger.v3.parser.core.models.AuthorizationValue;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
import java.util.Map;
@Setter
@ -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() {

View File

@ -118,6 +118,7 @@ public class Generator {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "The OpenAPI specification supplied was not valid");
}
// do not use opts.getOptions().get("outputFolder") as the input can contain ../../
// to access other folders in the server
String destPath = language + "-" + type.getTypeName();
@ -140,6 +141,16 @@ public class Generator {
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);

View File

@ -10,6 +10,7 @@ import org.springframework.http.HttpHeaders;
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 static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not;
@ -133,4 +134,32 @@ public class GenApiControllerTest {
.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");
}
}