Skip to content

Commit

Permalink
/stoicflame#32 Added support the xml namespace prefix.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee committed Aug 25, 2020
1 parent 37f55e2 commit f8b0120
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ public class ObjectTypeRenderer {
private final EnunciateLogger logger;
private final DataTypeReferenceRenderer datatypeRefRenderer;
private final Set<String> passThroughAnnotations;
private final Map<String, String> namespacePrefixMap;

private final boolean removeObjectPrefix;
private final boolean disableExamples;

public ObjectTypeRenderer(EnunciateLogger enunciateLogger, DataTypeReferenceRenderer datatypeRefRenderer, Set<String> passThroughAnnotations, boolean removeObjectPrefix, boolean disableExamples) {
public ObjectTypeRenderer(EnunciateLogger enunciateLogger, DataTypeReferenceRenderer datatypeRefRenderer, Set<String> passThroughAnnotations, Map<String, String> namespacePrefixMap, boolean removeObjectPrefix, boolean disableExamples) {
this.logger = enunciateLogger;
this.datatypeRefRenderer = datatypeRefRenderer;
this.passThroughAnnotations = passThroughAnnotations;
this.namespacePrefixMap = namespacePrefixMap;
this.removeObjectPrefix = removeObjectPrefix;
this.disableExamples = disableExamples;
}
Expand Down Expand Up @@ -365,7 +367,12 @@ private void addOptionalXml(IndentationPrinter ip, DataType datatype) {
Namespace namespace = datatype.getNamespace();
String xmlNamespace = getNonNullAndNonEmpty(namespace != null ? namespace.getUri() : null);

logger.debug("optionalXml for " + datatype.getLabel() + " : " + xmlName + " / " + xmlNamespace);
String namespacePrefix = null;
if (xmlNamespace != null && this.namespacePrefixMap != null && !this.namespacePrefixMap.isEmpty()) {
namespacePrefix = getNonNullAndNonEmpty(this.namespacePrefixMap.get(xmlNamespace));
}

logger.debug("optionalXml for " + datatype.getLabel() + " : " + xmlName + " / " + xmlNamespace + " / "+ namespacePrefix);

if (xmlName != null || xmlNamespace != null) {
ip.add("xml:");
Expand All @@ -376,6 +383,9 @@ private void addOptionalXml(IndentationPrinter ip, DataType datatype) {
if (xmlNamespace != null) {
ip.add("namespace: ", xmlNamespace);
}
if (namespacePrefix != null) {
ip.add("prefix: ", namespacePrefix);
}
ip.prevLevel();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void writeTo(File srcDir) throws IOException {
protected void writeToFolder(File dir) throws IOException {
EnunciateLogger logger = enunciate.getLogger();
DataTypeReferenceRenderer dataTypeReferenceRenderer = new DataTypeReferenceRenderer(logger, doRemoveObjectPrefix());
ObjectTypeRenderer objectTypeRenderer = new ObjectTypeRenderer(logger, dataTypeReferenceRenderer, getPassThroughAnnotations(), doRemoveObjectPrefix(), disableExamples());
ObjectTypeRenderer objectTypeRenderer = new ObjectTypeRenderer(logger, dataTypeReferenceRenderer, getPassThroughAnnotations(), getNamespacePrefixMap(), doRemoveObjectPrefix(), disableExamples());

OperationIds operationIds = new OperationIds(logger, enunciateModel);

Expand Down Expand Up @@ -198,6 +198,17 @@ private boolean disableExamples() {
private boolean doRemoveObjectPrefix() {
return Boolean.parseBoolean(config.getString("[@removeObjectPrefix]"));
}

/**
* Get namespace prefix map from enunciate configuration, return empty map if configuration is invalid.
* @return namespacePrefixMap
*/
private Map<String, String> getNamespacePrefixMap(){
if(enunciate.getConfiguration() == null){
return Collections.emptyMap();
}
return enunciate.getConfiguration().getNamespaces();
}
}

protected String getHost() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package dk.jyskebank.tools.enunciate.modules.openapi;

import com.webcohesion.enunciate.api.InterfaceDescriptionFile;
import com.webcohesion.enunciate.api.datatype.BaseType;
import com.webcohesion.enunciate.api.datatype.DataType;
import com.webcohesion.enunciate.api.datatype.DataTypeReference;
import com.webcohesion.enunciate.api.datatype.Namespace;
import com.webcohesion.enunciate.modules.jaxb.api.impl.ComplexDataTypeImpl;
import dk.jyskebank.tools.enunciate.modules.openapi.yaml.IndentationPrinter;
import org.junit.jupiter.api.Test;

Expand All @@ -19,12 +22,15 @@ class ObjectTypeRendererTest {


public static final String CONCRETE_TYPE_LABEL = "ConcreteX";
public static final String CONCRETE_TYPE_XML_NAME = "xml_xmlns_concreteX";
public static final String CONCRETE_TYPE_XML_URI = "http://www.w3.org/2000/xmlns/";
public static final String CONCRETE_TYPE_XML_PREFIX = "xmlns";

@Test
void verifyRenderAbstractType() {

ObjectTypeRenderer objectTypeRenderer = new ObjectTypeRenderer(new OutputLogger(), null
, null, false, false);
, null, null, false, false);

IndentationPrinter ip = TestHelper.getIndentationPrinter();
DataType abstractType = getAbstractTypeWithTwoSubtypes();
Expand All @@ -39,7 +45,7 @@ void verifyRenderAbstractType() {
@Test
void verifyRenderConcreteType() {
ObjectTypeRenderer objectTypeRenderer = new ObjectTypeRenderer(new OutputLogger(), null
, null, false, false);
, null, null, false, false);

IndentationPrinter ip = TestHelper.getIndentationPrinter();
DataType concreteType = getConcreteType();
Expand All @@ -51,6 +57,82 @@ void verifyRenderConcreteType() {

}

@Test
void verifyRenderConcreteTypeNamespacePrefix(){
ObjectTypeRenderer objectTypeRenderer = new ObjectTypeRenderer(new OutputLogger(), null
, null, Collections.singletonMap(CONCRETE_TYPE_XML_URI,CONCRETE_TYPE_XML_PREFIX), false, false);

IndentationPrinter ip = TestHelper.getIndentationPrinter();
DataType concreteType = getConcreteTypeWithNamespace();

objectTypeRenderer.render(ip, concreteType, false);

final String expected = createExpectedResultForConcreteTypeWithNamespacePrefix();
assertEquals(expected, ip.toString());
}

@Test
void verifyRenderConcreteTypeWithoutNamespacePrefix(){
ObjectTypeRenderer objectTypeRenderer = new ObjectTypeRenderer(new OutputLogger(), null
, null, null, false, false);

IndentationPrinter ip = TestHelper.getIndentationPrinter();
DataType concreteType = getConcreteTypeWithNamespace();

objectTypeRenderer.render(ip, concreteType, false);

final String expected = createExpectedResultForConcreteTypeWithoutNamespacePrefix();
assertEquals(expected, ip.toString());
}

private ComplexDataTypeImpl getConcreteTypeWithNamespace() {
ComplexDataTypeImpl mockedDataType = mock(ComplexDataTypeImpl.class);
when(mockedDataType.isAbstract()).thenReturn(Boolean.FALSE);

when(mockedDataType.getLabel()).thenReturn(CONCRETE_TYPE_LABEL);
when(mockedDataType.getXmlName()).thenReturn(CONCRETE_TYPE_XML_NAME);
when(mockedDataType.getBaseType()).thenReturn(BaseType.string);

Namespace customNamespace = new Namespace() {
@Override
public String getUri() {
return CONCRETE_TYPE_XML_URI;
}
@Override
public InterfaceDescriptionFile getSchemaFile() {
return null;
}
@Override
public List<? extends DataType> getTypes() {
return null;
}
};
when(mockedDataType.getNamespace()).thenReturn(customNamespace);

return mockedDataType;
}

private String createExpectedResultForConcreteTypeWithNamespacePrefix() {
String DOS_NEWLINE = "\r\n";
return String.format("title: \"%s\"%s%s type: string%s%s xml:%s%s name: %s%s%s namespace: %s%s%s prefix: %s",
CONCRETE_TYPE_LABEL, DOS_NEWLINE, TestHelper.INITIAL_INDENTATION,
DOS_NEWLINE, TestHelper.INITIAL_INDENTATION,
DOS_NEWLINE, TestHelper.INITIAL_INDENTATION,
CONCRETE_TYPE_XML_NAME, DOS_NEWLINE, TestHelper.INITIAL_INDENTATION,
CONCRETE_TYPE_XML_URI, DOS_NEWLINE, TestHelper.INITIAL_INDENTATION,
CONCRETE_TYPE_XML_PREFIX, DOS_NEWLINE, TestHelper.INITIAL_INDENTATION);
}

private String createExpectedResultForConcreteTypeWithoutNamespacePrefix() {
String DOS_NEWLINE = "\r\n";
return String.format("title: \"%s\"%s%s type: string%s%s xml:%s%s name: %s%s%s namespace: %s",
CONCRETE_TYPE_LABEL, DOS_NEWLINE, TestHelper.INITIAL_INDENTATION,
DOS_NEWLINE, TestHelper.INITIAL_INDENTATION,
DOS_NEWLINE, TestHelper.INITIAL_INDENTATION,
CONCRETE_TYPE_XML_NAME, DOS_NEWLINE, TestHelper.INITIAL_INDENTATION,
CONCRETE_TYPE_XML_URI, DOS_NEWLINE, TestHelper.INITIAL_INDENTATION);
}

private String createExpectedResultForConcreteType() {
String DOS_NEWLINE = "\r\n";
return String.format("title: \"%s\"%s%s type: string",
Expand Down

0 comments on commit f8b0120

Please sign in to comment.