Skip to content

Commit

Permalink
fix for #67
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszm committed Jan 2, 2024
1 parent 93fb379 commit 0fd94c7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
8 changes: 6 additions & 2 deletions cli/src/main/java/com/mrv/yangtools/codegen/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static void main(String[] args) {
}

void init() throws FileNotFoundException {
if (output != null && output.trim().length() > 0) {
if (output != null && !output.trim().isEmpty()) {
out = new FileOutputStream(output);
}
}
Expand Down Expand Up @@ -165,7 +165,11 @@ void generate() throws IOException, ReactorException {
generator.appendPostProcessor(new SingleParentInheritenceModel());
}

generator.appendPostProcessor(new Rfc4080PayloadWrapper());
if(!odlPathFormat) {
//TODO check if something similar is needed for ODL paths as well
generator.appendPostProcessor(new Rfc4080PayloadWrapper());
}

generator.appendPostProcessor(new RemoveUnusedDefinitions());

generator.generate(new OutputStreamWriter(out));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ private void generate(RpcDefinition rpc) {

private void generate(DataSchemaNode node, final int depth) {
if(depth == 0) {
log.debug("Maxmium depth level reached, skipping {} and it's childs", node.getPath());
log.debug("Maximum depth level reached, skipping {} and it's childs", node.getPath());
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ public void accept(Swagger target) {

protected abstract Map<String, String> prepareForReplacement(Swagger swagger);

private void fixModel(String name, Model m, Map<String, String> replacements) {
private Optional<Model> fixModel(String name, Model m, Map<String, String> replacements) {
ModelImpl fixProperties = null;

if(m instanceof RefModel) {
fixRefModel((RefModel) m, replacements);
return;
return Optional.ofNullable(fixRefModel((RefModel) m, replacements));
}

if(m instanceof ModelImpl) {
Expand All @@ -69,12 +68,15 @@ private void fixModel(String name, Model m, Map<String, String> replacements) {
.findFirst().orElse(null);
}

if(fixProperties == null) return;
if(fixProperties == null) {
return Optional.empty();
}

if(fixProperties.getProperties() == null) {
if(fixProperties.getEnum() == null) {
log.warn("Empty model in {}", name);
}
return;
return Optional.empty();
}
fixProperties.getProperties().forEach((key, value) -> {
if (value instanceof RefProperty) {
Expand All @@ -91,6 +93,7 @@ private void fixModel(String name, Model m, Map<String, String> replacements) {
}
});

return Optional.of(fixProperties);
}
private boolean fixProperty(RefProperty p, Map<String, String> replacements) {
if(replacements.containsKey(p.getSimpleRef())) {
Expand All @@ -113,10 +116,13 @@ private void fixComposedModel(ComposedModel m, Map<String, String> replacements)
});
}

private void fixRefModel(RefModel model, Map<String, String> replacements) {
private Model fixRefModel(RefModel model, Map<String, String> replacements) {
if(replacements.containsKey(model.getSimpleRef())) {
model.set$ref("#/definitions/" + replacements.get(model.getSimpleRef()));

return model;
}
return null;
}

private void fixOperation(Operation operation, Map<String, String> replacements) {
Expand All @@ -138,13 +144,35 @@ private void fixParameter(Parameter p, Map<String, String> replacements) {
if(!(p instanceof BodyParameter)) return;
BodyParameter bp = (BodyParameter) p;

fixModel(null, bp.getSchema(), replacements);
fixModel(null, bp.getSchema(), replacements)
.ifPresent(m -> {
bp.setSchema(m);
var nType = toTypeName(m.getReference());
var rep = replacements.entrySet().stream()
.filter(e -> e.getValue().equals(nType))
.findFirst();
rep.ifPresent(e -> {
var nDesc = bp.getDescription().replace(e.getKey(), e.getValue());
bp.setDescription(nDesc);
});

});
}

private void fixResponse(Response r, Map<String, String> replacements) {
Model model = r.getResponseSchema();
if(model != null) {
fixModel(null, model, replacements);
fixModel(null, model, replacements)
.ifPresent(m -> {
r.setResponseSchema(m);
r.setDescription(toTypeName(m.getReference()));
});
}
}

private static String toTypeName(String ref) {
if(ref == null) return null;
var seg = ref.split("/");
return seg[seg.length - 1];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @author bartosz.michalik@amartus.com
*/
public class Rfc4080PayloadWrapper extends PayloadWrapperProcessor {
//FIXME use only with proper path. watch out for variables etc.
@Override
protected String toProperty(String path) {
String[] split = path.split("/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void accept(Swagger swagger) {

}

private class Worker {
private static class Worker {
private final Map<String, TypeNode> hierarchy;
private final Swagger swagger;
private Set<String> toUnpack;
Expand Down

0 comments on commit 0fd94c7

Please sign in to comment.