-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
320 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
*.yaml | ||
.idea/ | ||
**/*.iml | ||
target/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
/* | ||
* Copyright (c) 2018 Amartus. All rights reserved. | ||
* Copyright (c) 2018-2022 Amartus. All rights reserved. | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v1.0 which accompanies this distribution, | ||
* and is available at http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Damian Mrozowicz <[email protected]> | ||
* Bartosz Michalik <[email protected]> | ||
*/ | ||
|
||
package com.mrv.yangtools.codegen; | ||
|
@@ -23,6 +24,8 @@ | |
import com.mrv.yangtools.common.SwaggerUtils; | ||
import io.swagger.models.Info; | ||
import io.swagger.models.Swagger; | ||
import io.swagger.util.Json; | ||
import io.swagger.util.Yaml; | ||
import org.opendaylight.yangtools.yang.model.api.*; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
@@ -200,11 +203,11 @@ public SwaggerGenerator elements(Elements... elements) { | |
public SwaggerGenerator format(Format f) { | ||
switch(f) { | ||
case YAML: | ||
mapper = new ObjectMapper(new YAMLFactory()); | ||
mapper = Yaml.mapper(); | ||
break; | ||
case JSON: | ||
default: | ||
mapper = new ObjectMapper(new JsonFactory()); | ||
mapper = Json.mapper(); | ||
} | ||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); | ||
return this; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ | |
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Processor that allows replacing one definitions with another in any swagger. | ||
* Processor that allows replacing one definition with another one in any swagger. | ||
* This implementation is simple and limited only to the type definitions that are aggregators of references. | ||
* @author [email protected] | ||
*/ | ||
|
@@ -51,13 +51,19 @@ public void accept(Swagger target) { | |
|
||
private void fixModel(String name, Model m, Map<String, String> replacements) { | ||
ModelImpl fixProperties = null; | ||
|
||
if(m instanceof RefModel) { | ||
fixRefModel((RefModel) m, replacements); | ||
return; | ||
} | ||
|
||
if(m instanceof ModelImpl) { | ||
fixProperties = (ModelImpl) m; | ||
} | ||
|
||
if(m instanceof ComposedModel) { | ||
ComposedModel cm = (ComposedModel) m; | ||
fixComposedModel(name, cm, replacements); | ||
fixComposedModel(cm, replacements); | ||
fixProperties = cm.getAllOf().stream() | ||
.filter(c -> c instanceof ModelImpl).map(c -> (ModelImpl)c) | ||
.findFirst().orElse(null); | ||
|
@@ -88,13 +94,13 @@ private void fixModel(String name, Model m, Map<String, String> replacements) { | |
} | ||
private boolean fixProperty(RefProperty p, Map<String, String> replacements) { | ||
if(replacements.containsKey(p.getSimpleRef())) { | ||
p.set$ref(replacements.get(p.getSimpleRef())); | ||
p.set$ref("#/definitions/" + replacements.get(p.getSimpleRef())); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private void fixComposedModel(String name, ComposedModel m, Map<String, String> replacements) { | ||
private void fixComposedModel(ComposedModel m, Map<String, String> replacements) { | ||
Set<RefModel> toReplace = m.getAllOf().stream().filter(c -> c instanceof RefModel).map(cm -> (RefModel) cm) | ||
.filter(rm -> replacements.containsKey(rm.getSimpleRef())).collect(Collectors.toSet()); | ||
toReplace.forEach(r -> { | ||
|
@@ -107,6 +113,11 @@ private void fixComposedModel(String name, ComposedModel m, Map<String, String> | |
}); | ||
} | ||
|
||
private void fixRefModel(RefModel model, Map<String, String> replacements) { | ||
if(replacements.containsKey(model.getSimpleRef())) { | ||
model.set$ref("#/definitions/" + replacements.get(model.getSimpleRef())); | ||
} | ||
} | ||
|
||
private void fixOperation(Operation operation, Map<String, String> replacements) { | ||
operation.getResponses().values() | ||
|
@@ -126,26 +137,14 @@ private void fixOperation(Operation operation, Map<String, String> replacements) | |
private void fixParameter(Parameter p, Map<String, String> replacements) { | ||
if(!(p instanceof BodyParameter)) return; | ||
BodyParameter bp = (BodyParameter) p; | ||
if(!(bp.getSchema() instanceof RefModel)) return; | ||
RefModel ref = (RefModel) bp.getSchema(); | ||
if(replacements.containsKey(ref.getSimpleRef())) { | ||
String replacement = replacements.get(ref.getSimpleRef()); | ||
bp.setDescription(bp.getDescription().replace(ref.getSimpleRef(), replacement)); | ||
bp.setSchema(new RefModel(replacement)); | ||
} | ||
|
||
fixModel(null, bp.getSchema(), replacements); | ||
} | ||
|
||
private void fixResponse(Response r, Map<String, String> replacements) { | ||
if(! (r.getSchema() instanceof RefProperty)) return; | ||
RefProperty schema = (RefProperty) r.getSchema(); | ||
if(replacements.containsKey(schema.getSimpleRef())) { | ||
String replacement = replacements.get(schema.getSimpleRef()); | ||
if(r.getDescription() != null) | ||
r.setDescription(r.getDescription().replace(schema.getSimpleRef(), replacement)); | ||
schema.setDescription(replacement); | ||
r.setSchema(new RefProperty(replacement)); | ||
Model model = r.getResponseSchema(); | ||
if(model != null) { | ||
fixModel(null, model, replacements); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.