fixes #111 : Enum definition type values not rendered
getAllProperties logic as been factored in ModelUtils
This commit is contained in:
@@ -327,7 +327,7 @@ public class Swagger2MarkupConfigBuilder {
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies maximum depth level for inline object schema displaying (0 = no inline schemasEnabled).
|
||||
* Specifies maximum depth level for inline object schema displaying (0 = no inline schemas).
|
||||
*
|
||||
* @param inlineSchemaDepthLevel number of recursion levels for inline schemasEnabled display
|
||||
* @return this builder
|
||||
|
||||
@@ -16,19 +16,17 @@
|
||||
package io.github.swagger2markup.internal.document.builder;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import io.github.swagger2markup.Swagger2MarkupExtensionRegistry;
|
||||
import io.github.swagger2markup.markup.builder.MarkupDocBuilder;
|
||||
import io.github.swagger2markup.Swagger2MarkupConverter;
|
||||
import io.github.swagger2markup.Swagger2MarkupExtensionRegistry;
|
||||
import io.github.swagger2markup.internal.document.MarkupDocument;
|
||||
import io.github.swagger2markup.internal.type.ObjectType;
|
||||
import io.github.swagger2markup.internal.type.RefType;
|
||||
import io.github.swagger2markup.internal.type.Type;
|
||||
import io.github.swagger2markup.internal.utils.ModelUtils;
|
||||
import io.github.swagger2markup.markup.builder.MarkupDocBuilder;
|
||||
import io.github.swagger2markup.spi.DefinitionsDocumentExtension;
|
||||
import io.swagger.models.ComposedModel;
|
||||
import io.swagger.models.Model;
|
||||
import io.swagger.models.RefModel;
|
||||
import io.swagger.models.properties.Property;
|
||||
import io.swagger.models.refs.RefFormat;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
@@ -56,6 +54,7 @@ public class DefinitionsDocumentBuilder extends MarkupDocumentBuilder {
|
||||
|
||||
private static final String DEFINITIONS_ANCHOR = "definitions";
|
||||
private final String DEFINITIONS;
|
||||
private final String TYPE_COLUMN;
|
||||
private static final List<String> IGNORED_DEFINITIONS = Collections.singletonList("Void");
|
||||
private static final String DESCRIPTION_FILE_NAME = "description";
|
||||
|
||||
@@ -64,6 +63,7 @@ public class DefinitionsDocumentBuilder extends MarkupDocumentBuilder {
|
||||
|
||||
ResourceBundle labels = ResourceBundle.getBundle("io/github/swagger2markup/lang/labels", config.getOutputLanguage().toLocale());
|
||||
DEFINITIONS = labels.getString("definitions");
|
||||
TYPE_COLUMN = labels.getString("type_column");
|
||||
|
||||
if (config.isDefinitionDescriptionsEnabled()) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
@@ -93,24 +93,23 @@ public class DefinitionsDocumentBuilder extends MarkupDocumentBuilder {
|
||||
*/
|
||||
@Override
|
||||
public MarkupDocument build() {
|
||||
Map<String, Model> definitions = globalContext.getSwagger().getDefinitions();
|
||||
if (MapUtils.isNotEmpty(definitions)) {
|
||||
if (MapUtils.isNotEmpty(globalContext.getSwagger().getDefinitions())) {
|
||||
applyDefinitionsDocumentExtension(new Context(Position.DOCUMENT_BEFORE, this.markupDocBuilder));
|
||||
buildDefinitionsTitle(DEFINITIONS);
|
||||
applyDefinitionsDocumentExtension(new Context(Position.DOCUMENT_BEGIN, this.markupDocBuilder));
|
||||
buildDefinitionsSection(definitions);
|
||||
buildDefinitionsSection();
|
||||
applyDefinitionsDocumentExtension(new Context(Position.DOCUMENT_END, this.markupDocBuilder));
|
||||
}
|
||||
return new MarkupDocument(markupDocBuilder);
|
||||
}
|
||||
|
||||
private void buildDefinitionsSection(Map<String, Model> definitions) {
|
||||
Set<String> definitionNames = toKeySet(definitions, config.getDefinitionOrdering());
|
||||
private void buildDefinitionsSection() {
|
||||
Set<String> definitionNames = toKeySet(globalContext.getSwagger().getDefinitions(), config.getDefinitionOrdering());
|
||||
for (String definitionName : definitionNames) {
|
||||
Model model = definitions.get(definitionName);
|
||||
Model model = globalContext.getSwagger().getDefinitions().get(definitionName);
|
||||
if (isNotBlank(definitionName)) {
|
||||
if (checkThatDefinitionIsNotInIgnoreList(definitionName)) {
|
||||
buildDefinition(definitions, definitionName, model);
|
||||
buildDefinition(definitionName, model);
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("Definition processed: {}", definitionName);
|
||||
}
|
||||
@@ -154,15 +153,14 @@ public class DefinitionsDocumentBuilder extends MarkupDocumentBuilder {
|
||||
/**
|
||||
* Generate definition files depending on the generation mode
|
||||
*
|
||||
* @param definitions all available definitions to be able to verify references
|
||||
* @param definitionName definition name to process
|
||||
* @param model definition model to process
|
||||
*/
|
||||
private void buildDefinition(Map<String, Model> definitions, String definitionName, Model model) {
|
||||
private void buildDefinition(String definitionName, Model model) {
|
||||
|
||||
if (config.isSeparatedDefinitionsEnabled()) {
|
||||
MarkupDocBuilder defDocBuilder = this.markupDocBuilder.copy(false);
|
||||
buildDefinition(definitions, definitionName, model, defDocBuilder);
|
||||
buildDefinition(definitionName, model, defDocBuilder);
|
||||
Path definitionFile = outputPath.resolve(resolveDefinitionDocument(definitionName));
|
||||
defDocBuilder.writeToFileWithoutExtension(definitionFile, StandardCharsets.UTF_8);
|
||||
if (logger.isInfoEnabled()) {
|
||||
@@ -172,7 +170,7 @@ public class DefinitionsDocumentBuilder extends MarkupDocumentBuilder {
|
||||
definitionRef(definitionName, this.markupDocBuilder);
|
||||
|
||||
} else {
|
||||
buildDefinition(definitions, definitionName, model, this.markupDocBuilder);
|
||||
buildDefinition(definitionName, model, this.markupDocBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,11 +191,11 @@ public class DefinitionsDocumentBuilder extends MarkupDocumentBuilder {
|
||||
* @param model the Swagger Model of the definition
|
||||
* @param docBuilder the docbuilder do use for output
|
||||
*/
|
||||
private void buildDefinition(Map<String, Model> definitions, String definitionName, Model model, MarkupDocBuilder docBuilder) {
|
||||
private void buildDefinition(String definitionName, Model model, MarkupDocBuilder docBuilder) {
|
||||
buildDefinitionTitle(definitionName, definitionName, docBuilder);
|
||||
applyDefinitionsDocumentExtension(new Context(Position.DEFINITION_BEGIN, docBuilder, definitionName, model));
|
||||
buildDescriptionParagraph(definitionName, model, docBuilder);
|
||||
inlineDefinitions(propertiesSection(definitions, definitionName, model, docBuilder), definitionName, config.getInlineSchemaDepthLevel(), docBuilder);
|
||||
inlineDefinitions(typeSection(definitionName, model, docBuilder), definitionName, config.getInlineSchemaDepthLevel(), docBuilder);
|
||||
applyDefinitionsDocumentExtension(new Context(Position.DEFINITION_END, docBuilder, definitionName, model));
|
||||
}
|
||||
|
||||
@@ -248,50 +246,29 @@ public class DefinitionsDocumentBuilder extends MarkupDocumentBuilder {
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the properties of a definition and inline schemas.
|
||||
* Builds the type informations of a definition
|
||||
*
|
||||
* @param definitions all available definitions
|
||||
* @param definitionName name of the definition to display
|
||||
* @param model model of the definition to display
|
||||
* @param docBuilder the docbuilder do use for output
|
||||
* @return a list of inlined types.
|
||||
*/
|
||||
private List<ObjectType> propertiesSection(Map<String, Model> definitions, String definitionName, Model model, MarkupDocBuilder docBuilder) {
|
||||
Map<String, Property> properties = getAllProperties(definitions, model);
|
||||
ObjectType type = new ObjectType(definitionName, properties);
|
||||
private List<ObjectType> typeSection(String definitionName, Model model, MarkupDocBuilder docBuilder) {
|
||||
List<ObjectType> localDefinitions = new ArrayList<>();
|
||||
Type modelType = ModelUtils.getType(model, globalContext.getSwagger().getDefinitions(), new DefinitionDocumentResolverFromDefinition());
|
||||
|
||||
return buildPropertiesTable(type, definitionName, 1, new PropertyDescriptor(type), new DefinitionDocumentResolverFromDefinition(), docBuilder);
|
||||
}
|
||||
if (modelType instanceof ObjectType || modelType instanceof RefType) {
|
||||
localDefinitions.addAll(buildPropertiesTable(ModelUtils.getTypeProperties(modelType), definitionName, 1, new PropertyDescriptor(modelType), new DefinitionDocumentResolverFromDefinition(), docBuilder));
|
||||
} else if (modelType != null) {
|
||||
MarkupDocBuilder typeInfos = docBuilder.copy(false);
|
||||
typeInfos.italicText(TYPE_COLUMN).textLine(": " + modelType.displaySchema(docBuilder));
|
||||
|
||||
private Map<String, Property> getAllProperties(Map<String, Model> definitions, Model model) {
|
||||
if (model instanceof RefModel) {
|
||||
RefModel refModel = (RefModel) model;
|
||||
String ref;
|
||||
if (refModel.getRefFormat().equals(RefFormat.INTERNAL)) {
|
||||
ref = refModel.getSimpleRef();
|
||||
} else {
|
||||
ref = model.getReference();
|
||||
}
|
||||
return definitions.containsKey(ref)
|
||||
? getAllProperties(definitions, definitions.get(ref))
|
||||
: null;
|
||||
} else if (model instanceof ComposedModel) {
|
||||
ComposedModel composedModel = (ComposedModel) model;
|
||||
Map<String, Property> allProperties = new HashMap<>();
|
||||
if (composedModel.getAllOf() != null) {
|
||||
for (Model innerModel : composedModel.getAllOf()) {
|
||||
Map<String, Property> innerProperties = getAllProperties(definitions, innerModel);
|
||||
if (innerProperties != null) {
|
||||
allProperties.putAll(innerProperties);
|
||||
}
|
||||
}
|
||||
}
|
||||
return ImmutableMap.copyOf(allProperties);
|
||||
} else {
|
||||
return model.getProperties();
|
||||
docBuilder.paragraph(typeInfos.toString());
|
||||
}
|
||||
}
|
||||
|
||||
return localDefinitions;
|
||||
}
|
||||
|
||||
private void buildDescriptionParagraph(String definitionName, Model model, MarkupDocBuilder docBuilder) {
|
||||
if (config.isDefinitionDescriptionsEnabled()) {
|
||||
Optional<String> description = handWrittenDefinitionDescription(normalizeName(definitionName), DESCRIPTION_FILE_NAME);
|
||||
@@ -365,7 +342,7 @@ public class DefinitionsDocumentBuilder extends MarkupDocumentBuilder {
|
||||
if (CollectionUtils.isNotEmpty(definitions)) {
|
||||
for (ObjectType definition : definitions) {
|
||||
addInlineDefinitionTitle(definition.getName(), definition.getUniqueName(), docBuilder);
|
||||
List<ObjectType> localDefinitions = buildPropertiesTable(definition, uniquePrefix, depth, new DefinitionPropertyDescriptor(definition), new DefinitionDocumentResolverFromDefinition(), docBuilder);
|
||||
List<ObjectType> localDefinitions = buildPropertiesTable(definition.getProperties(), uniquePrefix, depth, new DefinitionPropertyDescriptor(definition), new DefinitionDocumentResolverFromDefinition(), docBuilder);
|
||||
for (ObjectType localDefinition : localDefinitions)
|
||||
inlineDefinitions(Collections.singletonList(localDefinition), uniquePrefix, depth - 1, docBuilder);
|
||||
}
|
||||
|
||||
@@ -15,19 +15,19 @@
|
||||
*/
|
||||
package io.github.swagger2markup.internal.document.builder;
|
||||
|
||||
import io.github.swagger2markup.Swagger2MarkupExtensionRegistry;
|
||||
import io.github.swagger2markup.markup.builder.MarkupDocBuilder;
|
||||
import io.github.swagger2markup.markup.builder.MarkupDocBuilders;
|
||||
import io.github.swagger2markup.markup.builder.MarkupLanguage;
|
||||
import io.github.swagger2markup.markup.builder.MarkupTableColumn;
|
||||
import io.github.swagger2markup.Swagger2MarkupConfig;
|
||||
import io.github.swagger2markup.Swagger2MarkupConverter;
|
||||
import io.github.swagger2markup.Swagger2MarkupExtensionRegistry;
|
||||
import io.github.swagger2markup.internal.document.MarkupDocument;
|
||||
import io.github.swagger2markup.internal.type.DefinitionDocumentResolver;
|
||||
import io.github.swagger2markup.internal.type.ObjectType;
|
||||
import io.github.swagger2markup.internal.type.RefType;
|
||||
import io.github.swagger2markup.internal.type.Type;
|
||||
import io.github.swagger2markup.internal.utils.PropertyUtils;
|
||||
import io.github.swagger2markup.markup.builder.MarkupDocBuilder;
|
||||
import io.github.swagger2markup.markup.builder.MarkupDocBuilders;
|
||||
import io.github.swagger2markup.markup.builder.MarkupLanguage;
|
||||
import io.github.swagger2markup.markup.builder.MarkupTableColumn;
|
||||
import io.github.swagger2markup.utils.IOUtils;
|
||||
import io.swagger.models.properties.Property;
|
||||
import io.swagger.util.Json;
|
||||
@@ -103,9 +103,9 @@ public abstract class MarkupDocumentBuilder {
|
||||
public abstract MarkupDocument build() throws IOException;
|
||||
|
||||
/**
|
||||
* Build a generic property table for any ObjectType
|
||||
* Build a generic property table
|
||||
*
|
||||
* @param type to display
|
||||
* @param properties properties to display
|
||||
* @param uniquePrefix unique prefix to prepend to inline object names to enforce unicity
|
||||
* @param depth current inline schema object depth
|
||||
* @param propertyDescriptor property descriptor to apply to properties
|
||||
@@ -113,7 +113,7 @@ public abstract class MarkupDocumentBuilder {
|
||||
* @param docBuilder the docbuilder do use for output
|
||||
* @return a list of inline schemas referenced by some properties, for later display
|
||||
*/
|
||||
protected List<ObjectType> buildPropertiesTable(ObjectType type, String uniquePrefix, int depth, PropertyDescriptor propertyDescriptor, DefinitionDocumentResolver definitionDocumentResolver, MarkupDocBuilder docBuilder) {
|
||||
protected List<ObjectType> buildPropertiesTable(Map<String, Property> properties, String uniquePrefix, int depth, PropertyDescriptor propertyDescriptor, DefinitionDocumentResolver definitionDocumentResolver, MarkupDocBuilder docBuilder) {
|
||||
List<ObjectType> localDefinitions = new ArrayList<>();
|
||||
List<List<String>> cells = new ArrayList<>();
|
||||
List<MarkupTableColumn> cols = Arrays.asList(
|
||||
@@ -123,10 +123,10 @@ public abstract class MarkupDocumentBuilder {
|
||||
new MarkupTableColumn(SCHEMA_COLUMN).withWidthRatio(1).withMarkupSpecifiers(MarkupLanguage.ASCIIDOC, ".^1"),
|
||||
new MarkupTableColumn(DEFAULT_COLUMN).withWidthRatio(1).withMarkupSpecifiers(MarkupLanguage.ASCIIDOC, ".^1"),
|
||||
new MarkupTableColumn(EXAMPLE_COLUMN).withWidthRatio(1).withMarkupSpecifiers(MarkupLanguage.ASCIIDOC, ".^1"));
|
||||
if (MapUtils.isNotEmpty(type.getProperties())) {
|
||||
Set<String> propertyNames = toKeySet(type.getProperties(), config.getPropertyOrdering());
|
||||
if (MapUtils.isNotEmpty(properties)) {
|
||||
Set<String> propertyNames = toKeySet(properties, config.getPropertyOrdering());
|
||||
for (String propertyName : propertyNames) {
|
||||
Property property = type.getProperties().get(propertyName);
|
||||
Property property = properties.get(propertyName);
|
||||
Type propertyType = PropertyUtils.getType(property, definitionDocumentResolver);
|
||||
if (depth > 0 && propertyType instanceof ObjectType) {
|
||||
if (MapUtils.isNotEmpty(((ObjectType) propertyType).getProperties())) {
|
||||
@@ -134,7 +134,7 @@ public abstract class MarkupDocumentBuilder {
|
||||
propertyType.setUniqueName(uniquePrefix + " " + propertyName);
|
||||
localDefinitions.add((ObjectType) propertyType);
|
||||
|
||||
propertyType = new RefType(propertyType);
|
||||
propertyType = new RefType((ObjectType) propertyType);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,10 +18,9 @@ package io.github.swagger2markup.internal.document.builder;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.Multimap;
|
||||
import io.github.swagger2markup.Swagger2MarkupExtensionRegistry;
|
||||
import io.github.swagger2markup.markup.builder.*;
|
||||
import io.github.swagger2markup.GroupBy;
|
||||
import io.github.swagger2markup.Swagger2MarkupConverter;
|
||||
import io.github.swagger2markup.Swagger2MarkupExtensionRegistry;
|
||||
import io.github.swagger2markup.internal.document.MarkupDocument;
|
||||
import io.github.swagger2markup.internal.type.ObjectType;
|
||||
import io.github.swagger2markup.internal.type.RefType;
|
||||
@@ -30,6 +29,7 @@ import io.github.swagger2markup.internal.utils.ExamplesUtil;
|
||||
import io.github.swagger2markup.internal.utils.ParameterUtils;
|
||||
import io.github.swagger2markup.internal.utils.PropertyUtils;
|
||||
import io.github.swagger2markup.internal.utils.TagUtils;
|
||||
import io.github.swagger2markup.markup.builder.*;
|
||||
import io.github.swagger2markup.model.PathOperation;
|
||||
import io.github.swagger2markup.spi.PathsDocumentExtension;
|
||||
import io.swagger.models.*;
|
||||
@@ -470,7 +470,7 @@ public class PathsDocumentBuilder extends MarkupDocumentBuilder {
|
||||
new MarkupTableColumn(DEFAULT_COLUMN).withWidthRatio(1).withMarkupSpecifiers(MarkupLanguage.ASCIIDOC, ".^1"));
|
||||
for (Parameter parameter : parameters) {
|
||||
if (filterParameter(parameter)) {
|
||||
Type type = ParameterUtils.getType(parameter, new DefinitionDocumentResolverFromOperation());
|
||||
Type type = ParameterUtils.getType(parameter, globalContext.getSwagger().getDefinitions(), new DefinitionDocumentResolverFromOperation());
|
||||
|
||||
if (config.getInlineSchemaDepthLevel() > 0 && type instanceof ObjectType) {
|
||||
if (MapUtils.isNotEmpty(((ObjectType) type).getProperties())) {
|
||||
@@ -479,7 +479,7 @@ public class PathsDocumentBuilder extends MarkupDocumentBuilder {
|
||||
type.setName(localTypeName);
|
||||
type.setUniqueName(operation.getId() + " " + localTypeName);
|
||||
localDefinitions.add((ObjectType) type);
|
||||
type = new RefType(type);
|
||||
type = new RefType((ObjectType) type);
|
||||
}
|
||||
}
|
||||
String parameterType = WordUtils.capitalize(parameter.getIn());
|
||||
@@ -516,14 +516,14 @@ public class PathsDocumentBuilder extends MarkupDocumentBuilder {
|
||||
if (CollectionUtils.isNotEmpty(parameters)) {
|
||||
for (Parameter parameter : parameters) {
|
||||
if (StringUtils.equals(parameter.getIn(), "body")) {
|
||||
Type type = ParameterUtils.getType(parameter, new DefinitionDocumentResolverFromOperation());
|
||||
Type type = ParameterUtils.getType(parameter, globalContext.getSwagger().getDefinitions(), new DefinitionDocumentResolverFromOperation());
|
||||
|
||||
buildSectionTitle(BODY_PARAMETER, docBuilder);
|
||||
if (isNotBlank(parameter.getDescription())) {
|
||||
buildDescriptionParagraph(parameter.getDescription(), docBuilder);
|
||||
}
|
||||
|
||||
MarkupDocBuilder typeInfos = MarkupDocBuilders.documentBuilder(config.getMarkupLanguage(), config.getLineSeparator());
|
||||
MarkupDocBuilder typeInfos = docBuilder.copy(false);
|
||||
typeInfos.italicText(REQUIRED_COLUMN).textLine(": " + parameter.getRequired());
|
||||
typeInfos.italicText(NAME_COLUMN).textLine(": " + parameter.getName());
|
||||
if (!(type instanceof ObjectType)) {
|
||||
@@ -533,7 +533,7 @@ public class PathsDocumentBuilder extends MarkupDocumentBuilder {
|
||||
} else {
|
||||
docBuilder.paragraph(typeInfos.toString());
|
||||
|
||||
localDefinitions.addAll(buildPropertiesTable((ObjectType) type, operation.getId(), config.getInlineSchemaDepthLevel(), new PropertyDescriptor(type), new DefinitionDocumentResolverFromOperation(), docBuilder));
|
||||
localDefinitions.addAll(buildPropertiesTable(((ObjectType) type).getProperties(), operation.getId(), config.getInlineSchemaDepthLevel(), new PropertyDescriptor(type), new DefinitionDocumentResolverFromOperation(), docBuilder));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -721,7 +721,7 @@ public class PathsDocumentBuilder extends MarkupDocumentBuilder {
|
||||
type.setName(localTypeName);
|
||||
type.setUniqueName(operation.getId() + " " + localTypeName);
|
||||
localDefinitions.add((ObjectType) type);
|
||||
type = new RefType(type);
|
||||
type = new RefType((ObjectType) type);
|
||||
}
|
||||
}
|
||||
cells.add(Arrays.asList(responseName, swaggerMarkupDescription(response.getDescription()), type.displaySchema(markupDocBuilder)));
|
||||
@@ -781,7 +781,7 @@ public class PathsDocumentBuilder extends MarkupDocumentBuilder {
|
||||
for (ObjectType definition : definitions) {
|
||||
addInlineDefinitionTitle(definition.getName(), definition.getUniqueName(), docBuilder);
|
||||
|
||||
List<ObjectType> localDefinitions = buildPropertiesTable(definition, uniquePrefix, depth, new PropertyDescriptor(definition), new DefinitionDocumentResolverFromOperation(), docBuilder);
|
||||
List<ObjectType> localDefinitions = buildPropertiesTable(definition.getProperties(), uniquePrefix, depth, new PropertyDescriptor(definition), new DefinitionDocumentResolverFromOperation(), docBuilder);
|
||||
for (ObjectType localDefinition : localDefinitions)
|
||||
inlineDefinitions(Collections.singletonList(localDefinition), uniquePrefix, depth - 1, docBuilder);
|
||||
}
|
||||
|
||||
@@ -43,4 +43,20 @@ public class ArrayType extends Type {
|
||||
collectionFormat = this.collectionFormat + " ";
|
||||
return String.format("%s%s array", collectionFormat, ofType.displaySchema(docBuilder));
|
||||
}
|
||||
|
||||
public String getCollectionFormat() {
|
||||
return collectionFormat;
|
||||
}
|
||||
|
||||
public void setCollectionFormat(String collectionFormat) {
|
||||
this.collectionFormat = collectionFormat;
|
||||
}
|
||||
|
||||
public Type getOfType() {
|
||||
return ofType;
|
||||
}
|
||||
|
||||
public void setOfType(Type ofType) {
|
||||
this.ofType = ofType;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,4 +43,12 @@ public class BasicType extends Type {
|
||||
else
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public String getFormat() {
|
||||
return format;
|
||||
}
|
||||
|
||||
public void setFormat(String format) {
|
||||
this.format = format;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,4 +38,12 @@ public class EnumType extends Type {
|
||||
public String displaySchema(MarkupDocBuilder docBuilder) {
|
||||
return String.format("enum (%s)", join(values, ", "));
|
||||
}
|
||||
|
||||
public List<String> getValues() {
|
||||
return values;
|
||||
}
|
||||
|
||||
public void setValues(List<String> values) {
|
||||
this.values = values;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,4 +35,16 @@ public class MapType extends Type {
|
||||
public String displaySchema(MarkupDocBuilder docBuilder) {
|
||||
return String.format("<%s,%s> map", keyType.displaySchema(docBuilder), valueType.displaySchema(docBuilder));
|
||||
}
|
||||
|
||||
public Type getKeyType() {
|
||||
return keyType;
|
||||
}
|
||||
|
||||
public Type getValueType() {
|
||||
return valueType;
|
||||
}
|
||||
|
||||
public void setValueType(Type valueType) {
|
||||
this.valueType = valueType;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,14 +24,16 @@ import io.github.swagger2markup.markup.builder.MarkupDocBuilder;
|
||||
public class RefType extends Type {
|
||||
|
||||
private String document;
|
||||
private Type refType;
|
||||
|
||||
public RefType(String document, String name) {
|
||||
super(name);
|
||||
public RefType(String document, String name, String uniqueName, Type refType) {
|
||||
super(name, uniqueName);
|
||||
this.document = document;
|
||||
this.refType = refType;
|
||||
}
|
||||
|
||||
public RefType(Type type) {
|
||||
super(type.name, type.uniqueName);
|
||||
public RefType(Type refType) {
|
||||
this(null, refType.name, refType.uniqueName, refType);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -46,4 +48,12 @@ public class RefType extends Type {
|
||||
public void setDocument(String document) {
|
||||
this.document = document;
|
||||
}
|
||||
|
||||
public Type getRefType() {
|
||||
return refType;
|
||||
}
|
||||
|
||||
public void setRefType(Type refType) {
|
||||
this.refType = refType;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,37 +16,81 @@
|
||||
package io.github.swagger2markup.internal.utils;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import io.github.swagger2markup.internal.type.*;
|
||||
import io.swagger.models.ArrayModel;
|
||||
import io.swagger.models.Model;
|
||||
import io.swagger.models.ModelImpl;
|
||||
import io.swagger.models.RefModel;
|
||||
import io.swagger.models.*;
|
||||
import io.swagger.models.properties.Property;
|
||||
import io.swagger.models.refs.RefFormat;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public final class ModelUtils {
|
||||
|
||||
public static Map<String, Property> getTypeProperties(Type type) {
|
||||
Map<String, Property> properties = null;
|
||||
|
||||
if (type instanceof ObjectType) {
|
||||
properties = ((ObjectType) type).getProperties();
|
||||
} else if (type instanceof RefType) {
|
||||
properties = getTypeProperties(((RefType) type).getRefType());
|
||||
}
|
||||
|
||||
if (properties != null)
|
||||
return ImmutableMap.copyOf(properties);
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the type of a model, or otherwise null
|
||||
*
|
||||
* @param model the model
|
||||
* @param model the model
|
||||
* @param definitionDocumentResolver the definition document resolver
|
||||
* @return the type of the model, or otherwise null
|
||||
*/
|
||||
public static Type getType(Model model, Function<String, String> definitionDocumentResolver) {
|
||||
public static Type getType(Model model, Map<String, Model> definitions, Function<String, String> definitionDocumentResolver) {
|
||||
Validate.notNull(model, "model must not be null!");
|
||||
if (model instanceof ModelImpl) {
|
||||
ModelImpl modelImpl = (ModelImpl) model;
|
||||
|
||||
if (modelImpl.getAdditionalProperties() != null)
|
||||
return new MapType(null, PropertyUtils.getType(modelImpl.getAdditionalProperties(), definitionDocumentResolver));
|
||||
else
|
||||
else if (modelImpl.getEnum() != null)
|
||||
return new EnumType(null, modelImpl.getEnum());
|
||||
else if (modelImpl.getProperties() != null)
|
||||
return new ObjectType(null, model.getProperties());
|
||||
else
|
||||
return new BasicType(((ModelImpl) model).getType());
|
||||
} else if (model instanceof ComposedModel) {
|
||||
ComposedModel composedModel = (ComposedModel) model;
|
||||
Map<String, Property> allProperties = new HashMap<>();
|
||||
if (composedModel.getAllOf() != null) {
|
||||
for (Model innerModel : composedModel.getAllOf()) {
|
||||
Type innerModelType = getType(innerModel, definitions, definitionDocumentResolver);
|
||||
Map<String, Property> innerModelProperties = getTypeProperties(innerModelType);
|
||||
|
||||
if (innerModelProperties != null)
|
||||
allProperties.putAll(innerModelProperties);
|
||||
}
|
||||
}
|
||||
return new ObjectType(null, allProperties);
|
||||
} else if (model instanceof RefModel) {
|
||||
String simpleRef = ((RefModel) model).getSimpleRef();
|
||||
return new RefType(definitionDocumentResolver.apply(simpleRef), simpleRef);
|
||||
RefModel refModel = (RefModel) model;
|
||||
String refName = refModel.getRefFormat().equals(RefFormat.INTERNAL) ? refModel.getSimpleRef() : refModel.getReference();
|
||||
|
||||
Type refType = new ObjectType(refName, null);
|
||||
if (definitions.containsKey(refName))
|
||||
refType = getType(definitions.get(refName), definitions, definitionDocumentResolver);
|
||||
|
||||
return new RefType(definitionDocumentResolver.apply(refName), refName, refName, refType);
|
||||
} else if (model instanceof ArrayModel) {
|
||||
ArrayModel arrayModel = ((ArrayModel) model);
|
||||
|
||||
return new ArrayType(null, PropertyUtils.getType(arrayModel.getItems(), definitionDocumentResolver));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.apache.commons.lang3.StringUtils.defaultString;
|
||||
|
||||
@@ -39,14 +40,14 @@ public final class ParameterUtils {
|
||||
* @param definitionDocumentResolver the defintion document resolver
|
||||
* @return the type of the parameter, or otherwise null
|
||||
*/
|
||||
public static Type getType(Parameter parameter, Function<String, String> definitionDocumentResolver){
|
||||
public static Type getType(Parameter parameter, Map<String, Model> definitions, Function<String, String> definitionDocumentResolver){
|
||||
Validate.notNull(parameter, "parameter must not be null!");
|
||||
Type type = null;
|
||||
if(parameter instanceof BodyParameter){
|
||||
BodyParameter bodyParameter = (BodyParameter)parameter;
|
||||
Model model = bodyParameter.getSchema();
|
||||
if(model != null){
|
||||
type = ModelUtils.getType(model, definitionDocumentResolver);
|
||||
type = ModelUtils.getType(model, definitions, definitionDocumentResolver);
|
||||
}else{
|
||||
type = new BasicType("string");
|
||||
}
|
||||
@@ -67,8 +68,9 @@ public final class ParameterUtils {
|
||||
}
|
||||
}
|
||||
else if(parameter instanceof RefParameter){
|
||||
String simpleRef = ((RefParameter)parameter).getSimpleRef();
|
||||
type = new RefType(definitionDocumentResolver.apply(simpleRef), simpleRef);
|
||||
String refName = ((RefParameter)parameter).getSimpleRef();
|
||||
|
||||
type = new RefType(definitionDocumentResolver.apply(refName), refName, refName, new ObjectType(refName, null /* FIXME, not used for now */));
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ public final class PropertyUtils {
|
||||
if (refProperty.getRefFormat() == RefFormat.RELATIVE)
|
||||
type = new ObjectType(null, null); // FIXME : Workaround for https://github.com/swagger-api/swagger-parser/issues/177
|
||||
else
|
||||
type = new RefType(definitionDocumentResolver.apply(refProperty.getSimpleRef()), refProperty.getSimpleRef());
|
||||
type = new RefType(definitionDocumentResolver.apply(refProperty.getSimpleRef()), refProperty.getSimpleRef(), refProperty.getSimpleRef(), null /* FIXME, not used for now */);
|
||||
} else if (property instanceof ArrayProperty) {
|
||||
ArrayProperty arrayProperty = (ArrayProperty) property;
|
||||
Property items = arrayProperty.getItems();
|
||||
|
||||
@@ -383,4 +383,28 @@ public class AsciidocConverterTest {
|
||||
Path expectedFilesDirectory = Paths.get(AsciidocConverterTest.class.getResource("/expected/asciidoc/maps").toURI());
|
||||
DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "testSwagger2AsciiDocConversionWithMaps.html");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSwagger2AsciiDocConversionWithEnums() throws IOException, URISyntaxException {
|
||||
//Given
|
||||
Path file = Paths.get(AsciidocConverterTest.class.getResource("/json/swagger_enums.json").toURI());
|
||||
Path outputDirectory = Paths.get("build/test/asciidoc/enums");
|
||||
FileUtils.deleteQuietly(outputDirectory.toFile());
|
||||
|
||||
//When
|
||||
Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
|
||||
.withInlineSchemaDepthLevel(5)
|
||||
.build();
|
||||
Swagger2MarkupConverter.from(file)
|
||||
.withConfig(config)
|
||||
.build()
|
||||
.toFolder(outputDirectory);
|
||||
|
||||
//Then
|
||||
String[] files = outputDirectory.toFile().list();
|
||||
assertThat(files).hasSize(4).containsAll(expectedFiles);
|
||||
|
||||
Path expectedFilesDirectory = Paths.get(AsciidocConverterTest.class.getResource("/expected/asciidoc/enums").toURI());
|
||||
DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "testSwagger2AsciiDocConversionWithEnums.html");
|
||||
}
|
||||
}
|
||||
|
||||
19
src/test/resources/expected/asciidoc/enums/definitions.adoc
Normal file
19
src/test/resources/expected/asciidoc/enums/definitions.adoc
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
[[_definitions]]
|
||||
== Definitions
|
||||
|
||||
[[_enumstring]]
|
||||
=== EnumString
|
||||
Enum string
|
||||
|
||||
_Type_: enum (ADDED, REMOVED, CHANGED)
|
||||
|
||||
|
||||
[[_simplestring]]
|
||||
=== SimpleString
|
||||
Simple string
|
||||
|
||||
_Type_: string
|
||||
|
||||
|
||||
|
||||
15
src/test/resources/expected/asciidoc/enums/overview.adoc
Normal file
15
src/test/resources/expected/asciidoc/enums/overview.adoc
Normal file
@@ -0,0 +1,15 @@
|
||||
= Title
|
||||
|
||||
|
||||
[[_overview]]
|
||||
== Overview
|
||||
Description
|
||||
|
||||
|
||||
=== Version information
|
||||
Version : Developer build
|
||||
|
||||
=== URI scheme
|
||||
Host : localhost:8080
|
||||
|
||||
|
||||
56
src/test/resources/expected/asciidoc/enums/paths.adoc
Normal file
56
src/test/resources/expected/asciidoc/enums/paths.adoc
Normal file
@@ -0,0 +1,56 @@
|
||||
|
||||
[[_paths]]
|
||||
== Paths
|
||||
|
||||
[[_createstate]]
|
||||
=== Create state
|
||||
----
|
||||
POST /state
|
||||
----
|
||||
|
||||
|
||||
==== Description
|
||||
Return state
|
||||
|
||||
|
||||
==== Parameters
|
||||
|
||||
[options="header", cols=".^1h,.^1h,.^6,.^1,.^1,.^1"]
|
||||
|===
|
||||
|Type|Name|Description|Required|Schema|Default
|
||||
|Path|oldState|Old State as raw string|true|enum (ADDED, REMOVED, CHANGED)|
|
||||
|Body|StateModel|State as enum in object|false|<<_createstate_statemodel,StateModel>>|
|
||||
|===
|
||||
|
||||
[[_createstate_statemodel]]
|
||||
*StateModel*
|
||||
|
||||
[options="header", cols=".^1h,.^6,.^1,.^1,.^1,.^1"]
|
||||
|===
|
||||
|Name|Description|Required|Schema|Default|Example
|
||||
|value|State value|false|enum (ADDED, REMOVED, CHANGED)||
|
||||
|===
|
||||
|
||||
|
||||
==== Responses
|
||||
|
||||
===== HTTP Code 200
|
||||
|
||||
[options="header", cols=".^1h,.^3,.^1"]
|
||||
|===
|
||||
|HTTP Code|Description|Schema
|
||||
|200|OK|enum (ADDED, REMOVED, CHANGED)
|
||||
|===
|
||||
|
||||
|
||||
==== Consumes
|
||||
|
||||
* application/json
|
||||
|
||||
|
||||
==== Produces
|
||||
|
||||
* */*
|
||||
|
||||
|
||||
|
||||
2
src/test/resources/expected/asciidoc/enums/security.adoc
Normal file
2
src/test/resources/expected/asciidoc/enums/security.adoc
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
|
||||
85
src/test/resources/json/swagger_enums.json
Normal file
85
src/test/resources/json/swagger_enums.json
Normal file
@@ -0,0 +1,85 @@
|
||||
{
|
||||
"swagger": "2.0",
|
||||
"info": {
|
||||
"description": "Description",
|
||||
"version": "Developer build",
|
||||
"title": "Title"
|
||||
},
|
||||
"host": "localhost:8080",
|
||||
"paths": {
|
||||
"/state": {
|
||||
"post": {
|
||||
"summary": "Create state",
|
||||
"description": "Return state",
|
||||
"operationId": "createState",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"*/*"
|
||||
],
|
||||
"parameters": [
|
||||
{
|
||||
"in": "path",
|
||||
"name": "oldState",
|
||||
"description": "Old State as raw string",
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"ADDED",
|
||||
"REMOVED",
|
||||
"CHANGED"
|
||||
]
|
||||
},
|
||||
{
|
||||
"in": "body",
|
||||
"name": "StateModel",
|
||||
"description": "State as enum in object",
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"value": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"ADDED",
|
||||
"REMOVED",
|
||||
"CHANGED"
|
||||
],
|
||||
"description": "State value"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"description": "State response as raw string",
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"ADDED",
|
||||
"REMOVED",
|
||||
"CHANGED"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"definitions": {
|
||||
"SimpleString": {
|
||||
"type": "string",
|
||||
"description": "Simple string"
|
||||
},
|
||||
"EnumString": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"ADDED",
|
||||
"REMOVED",
|
||||
"CHANGED"
|
||||
],
|
||||
"description": "Enum string"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user