Skip to content

Commit

Permalink
k6: use spaces instead of tab, remove trailing spaces, update samples
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 committed Aug 22, 2021
1 parent 6844737 commit f7bc2aa
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,98 +82,98 @@ public K6ClientCodegen() {

}

static class Parameter {
String key;
Object value;
boolean hasExample;

public Parameter(String key, Object value) {
this.key = key;
this.value = value;
}

public Parameter(String key, Object exampleValue, boolean hasExample) {
this.key = key;
this.value = exampleValue;
this.hasExample = hasExample;
}

@Override
public int hashCode() {
return key.hashCode();
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
Parameter p = (Parameter) obj;
return key.equals(p.key) && value.equals(p.value) && hasExample == p.hasExample;
}
}
static class Parameter {
String key;
Object value;
boolean hasExample;

public Parameter(String key, Object value) {
this.key = key;
this.value = value;
}

public Parameter(String key, Object exampleValue, boolean hasExample) {
this.key = key;
this.value = exampleValue;
this.hasExample = hasExample;
}

@Override
public int hashCode() {
return key.hashCode();
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
Parameter p = (Parameter) obj;
return key.equals(p.key) && value.equals(p.value) && hasExample == p.hasExample;
}
}

static class ParameterValueLambda implements Mustache.Lambda {
private static final String NO_EXAMPLE_PARAM_VALUE_PREFIX = "TODO_EDIT_THE_";

@Override
public void execute(Template.Fragment fragment, Writer writer) throws IOException {

// default used if no example is provided
String noExampleParamValue = String.join("",
quoteExample(
String.join("", NO_EXAMPLE_PARAM_VALUE_PREFIX, fragment.execute())),
";",
" // specify value as there is no example value for this parameter in OpenAPI spec");

// param has example(s)
if (fragment.context() instanceof K6ClientCodegen.Parameter
&& ((K6ClientCodegen.Parameter) fragment.context()).hasExample) {

Object rawValue = ((K6ClientCodegen.Parameter) fragment.context()).value;
// handle as 'examples'
if (rawValue instanceof Map) {
@SuppressWarnings("unchecked")
Set<String> exampleValues = ((Map<String, Example>) rawValue).values().stream()
.map(x -> quoteExample(
StringEscapeUtils.escapeEcmaScript(
String.valueOf(x.getValue()))))
.collect(Collectors.toCollection(() -> new TreeSet<String>()));
if (!exampleValues.isEmpty()) {

writer.write(String.join("",
Arrays.toString(exampleValues.toArray()),
".shift();",
" // first element from list extracted from 'examples' field defined at the parameter level of OpenAPI spec"));

} else {
writer.write(noExampleParamValue);
}

// handle as (single) 'example'
} else {
writer.write(String.join("",
quoteExample(
StringEscapeUtils.escapeEcmaScript(
String.valueOf(
((K6ClientCodegen.Parameter) fragment.context()).value))),
";",
" // extracted from 'example' field defined at the parameter level of OpenAPI spec"));
}

} else {
writer.write(noExampleParamValue);
}
}

private static String quoteExample(String exampleValue) {
return StringUtils.wrap(exampleValue, "'");
}
}
static class ParameterValueLambda implements Mustache.Lambda {
private static final String NO_EXAMPLE_PARAM_VALUE_PREFIX = "TODO_EDIT_THE_";

@Override
public void execute(Template.Fragment fragment, Writer writer) throws IOException {

// default used if no example is provided
String noExampleParamValue = String.join("",
quoteExample(
String.join("", NO_EXAMPLE_PARAM_VALUE_PREFIX, fragment.execute())),
";",
" // specify value as there is no example value for this parameter in OpenAPI spec");

// param has example(s)
if (fragment.context() instanceof K6ClientCodegen.Parameter
&& ((K6ClientCodegen.Parameter) fragment.context()).hasExample) {

Object rawValue = ((K6ClientCodegen.Parameter) fragment.context()).value;

// handle as 'examples'
if (rawValue instanceof Map) {

@SuppressWarnings("unchecked")
Set<String> exampleValues = ((Map<String, Example>) rawValue).values().stream()
.map(x -> quoteExample(
StringEscapeUtils.escapeEcmaScript(
String.valueOf(x.getValue()))))
.collect(Collectors.toCollection(() -> new TreeSet<String>()));

if (!exampleValues.isEmpty()) {

writer.write(String.join("",
Arrays.toString(exampleValues.toArray()),
".shift();",
" // first element from list extracted from 'examples' field defined at the parameter level of OpenAPI spec"));

} else {
writer.write(noExampleParamValue);
}

// handle as (single) 'example'
} else {
writer.write(String.join("",
quoteExample(
StringEscapeUtils.escapeEcmaScript(
String.valueOf(
((K6ClientCodegen.Parameter) fragment.context()).value))),
";",
" // extracted from 'example' field defined at the parameter level of OpenAPI spec"));
}

} else {
writer.write(noExampleParamValue);
}
}

private static String quoteExample(String exampleValue) {
return StringUtils.wrap(exampleValue, "'");
}
}

static class HTTPBody {
List<Parameter> parameters;
Expand Down Expand Up @@ -484,23 +484,23 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
case "query":
if (parameter.getIn().equals("query"))
queryParams.add(new Parameter(parameter.getName(), getTemplateVariable(parameter.getName())));
if (!pathVariables.containsKey(path)) {
// use 'example' field defined at the parameter level of OpenAPI spec
if (Objects.nonNull(parameter.getExample())) {
variables.add(new Parameter(toVarName(parameter.getName()),
parameter.getExample(), true));

// use 'examples' field defined at the parameter level of OpenAPI spec
} else if (Objects.nonNull(parameter.getExamples())) {
variables.add(new Parameter(toVarName(parameter.getName()),
parameter.getExamples(), true));

// no example provided, generated script will contain placeholder value
} else {
variables.add(new Parameter(toVarName(parameter.getName()),
parameter.getName().toUpperCase(Locale.ROOT)));
}
}
if (!pathVariables.containsKey(path)) {
// use 'example' field defined at the parameter level of OpenAPI spec
if (Objects.nonNull(parameter.getExample())) {
variables.add(new Parameter(toVarName(parameter.getName()),
parameter.getExample(), true));

// use 'examples' field defined at the parameter level of OpenAPI spec
} else if (Objects.nonNull(parameter.getExamples())) {
variables.add(new Parameter(toVarName(parameter.getName()),
parameter.getExamples(), true));

// no example provided, generated script will contain placeholder value
} else {
variables.add(new Parameter(toVarName(parameter.getName()),
parameter.getName().toUpperCase(Locale.ROOT)));
}
}
break;
default:
break;
Expand Down Expand Up @@ -745,8 +745,8 @@ private static String getAccept(OpenAPI openAPI, Operation operation) {
return accepts;
}

@Override
protected Builder<String, Lambda> addMustacheLambdas() {
return super.addMustacheLambdas().put("handleParamValue", new ParameterValueLambda());
}
@Override
protected Builder<String, Lambda> addMustacheLambdas() {
return super.addMustacheLambdas().put("handleParamValue", new ParameterValueLambda());
}
}
2 changes: 1 addition & 1 deletion samples/client/petstore/k6/.openapi-generator/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.2.0-SNAPSHOT
5.3.0-SNAPSHOT
2 changes: 1 addition & 1 deletion samples/client/petstore/k6/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* NOTE: This class is auto generated by OpenAPI Generator.
* https://github.com/OpenAPITools/openapi-generator
*
* OpenAPI generator version: 5.2.0-SNAPSHOT
* OpenAPI generator version: 5.3.0-SNAPSHOT
*/


Expand Down

0 comments on commit f7bc2aa

Please sign in to comment.