Skip to content

Commit

Permalink
more optional nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
aress31 committed Dec 23, 2023
1 parent fe59490 commit ec04ede
Showing 1 changed file with 40 additions and 40 deletions.
80 changes: 40 additions & 40 deletions src/main/java/swurg/workers/Worker.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,47 +142,47 @@ private List<HttpParameter> buildHttpRequestParameters(List<Parameter> parameter
Map<String, Schema> schemas) {
List<HttpParameter> httpParameters = new ArrayList<>();

if (parameters != null)
parameters.forEach(parameter -> {
String in = parameter.getIn();
String name = parameter.getName();
Schema schema = parameter.getSchema();
String value = Optional.ofNullable(schema)
.map(Schema::getType)
.orElse(null);

if ("header".equals(in))
httpParameters.add(HttpParameter.cookieParameter(name, value));
else if ("query".equals(in))
httpParameters.add(HttpParameter.urlParameter(name, value));
});

if (requestBody != null) {
requestBody.getContent().entrySet().stream()
.findFirst()
.map(Map.Entry::getValue)
.ifPresent(mediaType -> {
if (mediaType.getSchema().get$ref() != null) {
String href = mediaType.getSchema().get$ref();
String formattedHref = href.substring(href.lastIndexOf("/") + 1);

Schema schema = schemas.get(formattedHref);
Map<String, Schema> properties = schema.getProperties();

if (properties != null) {
properties.forEach((name, propertySchema) -> {
Object example = propertySchema.getExample();
String type = propertySchema.getType();
String value = Optional.ofNullable(example)
.map(Object::toString)
.orElse(type);

httpParameters.add(HttpParameter.bodyParameter(name, value));
});
}
Optional.ofNullable(parameters)
.ifPresent(parameterList -> parameterList.forEach(parameter -> {
String in = parameter.getIn();
String name = parameter.getName();
Schema schema = parameter.getSchema();
String value = Optional.ofNullable(schema)
.map(Schema::getType)
.orElse(null);

if ("header".equals(in)) {
httpParameters.add(HttpParameter.cookieParameter(name, value));
} else if ("query".equals(in)) {
httpParameters.add(HttpParameter.urlParameter(name, value));
}
}));

Optional.ofNullable(requestBody)
.map(RequestBody::getContent)
.flatMap(content -> content.entrySet().stream().findFirst())
.map(Map.Entry::getValue)
.ifPresent(mediaType -> {
if (mediaType.getSchema().get$ref() != null) {
String href = mediaType.getSchema().get$ref();
String formattedHref = href.substring(href.lastIndexOf("/") + 1);

Schema schema = schemas.get(formattedHref);
Map<String, Schema> properties = schema.getProperties();

if (properties != null) {
properties.forEach((name, propertySchema) -> {
Object example = propertySchema.getExample();
String type = propertySchema.getType();
String value = Optional.ofNullable(example)
.map(Object::toString)
.orElse(type);

httpParameters.add(HttpParameter.bodyParameter(name, value));
});
}
});
}
}
});

return httpParameters;
}
Expand Down

0 comments on commit ec04ede

Please sign in to comment.