forked from loafle/openapi-generator-original
[cwiki] Added Enum documentation and fix newline problem for description (#8338)
* create enum documentation in model * add newline to fix layout when a markdown table follows a markdown list * avoid escaping of attributes to make sure documentation is displayed as in the spec
This commit is contained in:
parent
4947634a24
commit
367ba3a2cf
@ -22,6 +22,7 @@ import io.swagger.v3.oas.models.media.Schema;
|
|||||||
import org.openapitools.codegen.*;
|
import org.openapitools.codegen.*;
|
||||||
import org.openapitools.codegen.meta.features.*;
|
import org.openapitools.codegen.meta.features.*;
|
||||||
import org.openapitools.codegen.utils.ModelUtils;
|
import org.openapitools.codegen.utils.ModelUtils;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@ -123,6 +124,11 @@ public class ConfluenceWikiCodegen extends DefaultCodegen implements CodegenConf
|
|||||||
return objs;
|
return objs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
|
||||||
|
return postProcessModelsEnum(objs);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String escapeQuotationMark(String input) {
|
public String escapeQuotationMark(String input) {
|
||||||
// just return the original string
|
// just return the original string
|
||||||
@ -134,4 +140,14 @@ public class ConfluenceWikiCodegen extends DefaultCodegen implements CodegenConf
|
|||||||
// just return the original string
|
// just return the original string
|
||||||
return input;
|
return input;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String escapeText(String input) {
|
||||||
|
if (input == null) {
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
// chomp tailing newline because it breaks the tables and keep all other sign to show documentation properly
|
||||||
|
return StringUtils.chomp(input);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,10 +75,14 @@ h2. Models
|
|||||||
{anchor:{{classname}}ModelAnchor}
|
{anchor:{{classname}}ModelAnchor}
|
||||||
h3. {{classname}}
|
h3. {{classname}}
|
||||||
|
|
||||||
{{description}}
|
{{{description}}}
|
||||||
|
|
||||||
||Field Name||Required||Type||Description||Enum||
|
{{#isEnum}} ||Name||Value||Description||
|
||||||
|
{{#allowableValues}} {{#enumVars}} |{{{name}}} |{{{value}}} |{{{enumDescription}}} |
|
||||||
|
{{/enumVars}}
|
||||||
|
{{/allowableValues}} {{/isEnum}}
|
||||||
|
{{^isEnum}}||Field Name||Required||Type||Description||Enum||
|
||||||
{{#vars}} |{{baseName}} |{{#required}}(/){{/required}}{{^required}}(x){{/required}} |{noformat:nopanel=true}{{{dataType}}}{noformat} |{{description}} | {{#isEnum}} {{_enum}} {{/isEnum}} |
|
{{#vars}} |{{baseName}} |{{#required}}(/){{/required}}{{^required}}(x){{/required}} |{noformat:nopanel=true}{{{dataType}}}{noformat} |{{description}} | {{#isEnum}} {{_enum}} {{/isEnum}} |
|
||||||
{{/vars}}
|
{{/vars}} {{/isEnum}}
|
||||||
{{/model}}
|
{{/model}}
|
||||||
{{/models}}
|
{{/models}}
|
||||||
|
@ -0,0 +1,198 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
|
||||||
|
* Copyright 2018 SmartBear Software
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.openapitools.codegen.confluencewiki;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.models.Components;
|
||||||
|
import io.swagger.v3.oas.models.OpenAPI;
|
||||||
|
import io.swagger.v3.oas.models.media.*;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.openapitools.codegen.CodegenModel;
|
||||||
|
import org.openapitools.codegen.CodegenProperty;
|
||||||
|
import org.openapitools.codegen.DefaultCodegen;
|
||||||
|
import org.openapitools.codegen.TestUtils;
|
||||||
|
import org.openapitools.codegen.languages.ConfluenceWikiCodegen;
|
||||||
|
import org.testng.Assert;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class ConfluenceWikiTest {
|
||||||
|
|
||||||
|
@Test(description = "convert a model with an enum")
|
||||||
|
public void converterTest() {
|
||||||
|
final StringSchema enumSchema = new StringSchema();
|
||||||
|
enumSchema.setEnum(Arrays.asList("VALUE1", "VALUE2", "VALUE3"));
|
||||||
|
final Schema model = new Schema().type("object").addProperties("name", enumSchema);
|
||||||
|
|
||||||
|
final ConfluenceWikiCodegen codegen = new ConfluenceWikiCodegen();
|
||||||
|
OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
|
||||||
|
codegen.setOpenAPI(openAPI);
|
||||||
|
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||||
|
|
||||||
|
Assert.assertEquals(cm.vars.size(), 1);
|
||||||
|
|
||||||
|
final CodegenProperty enumVar = cm.vars.get(0);
|
||||||
|
Assert.assertEquals(enumVar.baseName, "name");
|
||||||
|
Assert.assertEquals(enumVar.dataType, "String");
|
||||||
|
Assert.assertEquals(enumVar.datatypeWithEnum, "NameEnum");
|
||||||
|
Assert.assertEquals(enumVar.name, "name");
|
||||||
|
Assert.assertEquals(enumVar.defaultValue, "null");
|
||||||
|
Assert.assertEquals(enumVar.baseType, "string");
|
||||||
|
Assert.assertTrue(enumVar.isEnum);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(description = "convert a model with an enum inside a list")
|
||||||
|
public void converterInArrayTest() {
|
||||||
|
final ArraySchema enumSchema = new ArraySchema().items(
|
||||||
|
new StringSchema().addEnumItem("Aaaa").addEnumItem("Bbbb"));
|
||||||
|
final Schema model = new Schema().type("object").addProperties("name", enumSchema);
|
||||||
|
|
||||||
|
final DefaultCodegen codegen = new ConfluenceWikiCodegen();
|
||||||
|
OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
|
||||||
|
codegen.setOpenAPI(openAPI);
|
||||||
|
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||||
|
|
||||||
|
Assert.assertEquals(cm.vars.size(), 1);
|
||||||
|
|
||||||
|
final CodegenProperty enumVar = cm.vars.get(0);
|
||||||
|
Assert.assertEquals(enumVar.baseName, "name");
|
||||||
|
Assert.assertEquals(enumVar.dataType, "array[String]");
|
||||||
|
Assert.assertEquals(enumVar.datatypeWithEnum, "array[String]");
|
||||||
|
Assert.assertEquals(enumVar.name, "name");
|
||||||
|
Assert.assertEquals(enumVar.defaultValue, "null");
|
||||||
|
Assert.assertEquals(enumVar.baseType, "array");
|
||||||
|
Assert.assertTrue(enumVar.isEnum);
|
||||||
|
|
||||||
|
Assert.assertEquals(enumVar.mostInnerItems.baseName, "name");
|
||||||
|
Assert.assertEquals(enumVar.mostInnerItems.dataType, "String");
|
||||||
|
Assert.assertEquals(enumVar.mostInnerItems.datatypeWithEnum, "NameEnum");
|
||||||
|
Assert.assertEquals(enumVar.mostInnerItems.name, "name");
|
||||||
|
Assert.assertEquals(enumVar.mostInnerItems.defaultValue, "null");
|
||||||
|
Assert.assertEquals(enumVar.mostInnerItems.baseType, "string");
|
||||||
|
|
||||||
|
Assert.assertEquals(enumVar.mostInnerItems.baseType, enumVar.items.baseType);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(description = "convert a model with an enum inside a list")
|
||||||
|
public void converterInArrayInArrayTest() {
|
||||||
|
final ArraySchema enumSchema = new ArraySchema().items(
|
||||||
|
new ArraySchema().items(
|
||||||
|
new StringSchema().addEnumItem("Aaaa").addEnumItem("Bbbb")));
|
||||||
|
final Schema model = new Schema().type("object").addProperties("name", enumSchema);
|
||||||
|
|
||||||
|
final DefaultCodegen codegen = new ConfluenceWikiCodegen();
|
||||||
|
OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
|
||||||
|
codegen.setOpenAPI(openAPI);
|
||||||
|
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||||
|
|
||||||
|
Assert.assertEquals(cm.vars.size(), 1);
|
||||||
|
|
||||||
|
final CodegenProperty enumVar = cm.vars.get(0);
|
||||||
|
Assert.assertEquals(enumVar.baseName, "name");
|
||||||
|
Assert.assertEquals(enumVar.dataType, "array[array[String]]");
|
||||||
|
Assert.assertEquals(enumVar.datatypeWithEnum, "array[array[String]]");
|
||||||
|
Assert.assertEquals(enumVar.name, "name");
|
||||||
|
Assert.assertEquals(enumVar.defaultValue, "null");
|
||||||
|
Assert.assertEquals(enumVar.baseType, "array");
|
||||||
|
Assert.assertTrue(enumVar.isEnum);
|
||||||
|
|
||||||
|
Assert.assertEquals(enumVar.mostInnerItems.baseName, "name");
|
||||||
|
Assert.assertEquals(enumVar.mostInnerItems.dataType, "String");
|
||||||
|
Assert.assertEquals(enumVar.mostInnerItems.datatypeWithEnum, "NameEnum");
|
||||||
|
Assert.assertEquals(enumVar.mostInnerItems.name, "name");
|
||||||
|
Assert.assertEquals(enumVar.mostInnerItems.defaultValue, "null");
|
||||||
|
Assert.assertEquals(enumVar.mostInnerItems.baseType, "string");
|
||||||
|
|
||||||
|
Assert.assertEquals(enumVar.mostInnerItems.baseType, enumVar.items.items.baseType);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(description = "not override identical parent enums")
|
||||||
|
public void overrideEnumTest() {
|
||||||
|
final StringSchema identicalEnumProperty = new StringSchema();
|
||||||
|
identicalEnumProperty.setEnum(Arrays.asList("VALUE1", "VALUE2", "VALUE3"));
|
||||||
|
|
||||||
|
final StringSchema subEnumProperty = new StringSchema();
|
||||||
|
subEnumProperty.setEnum(Arrays.asList("SUB1", "SUB2", "SUB3"));
|
||||||
|
|
||||||
|
// Add one enum property to the parent
|
||||||
|
final Map<String, Schema> parentProperties = new HashMap<>();
|
||||||
|
parentProperties.put("sharedThing", identicalEnumProperty);
|
||||||
|
|
||||||
|
// Add TWO enums to the subType model; one of which is identical to the one in parent class
|
||||||
|
final Map<String, Schema> subProperties = new HashMap<>();
|
||||||
|
subProperties.put("unsharedThing", subEnumProperty);
|
||||||
|
|
||||||
|
final Schema parentModel = new Schema();
|
||||||
|
parentModel.setProperties(parentProperties);
|
||||||
|
parentModel.name("parentModel");
|
||||||
|
|
||||||
|
Discriminator discriminator = new Discriminator().mapping("name", StringUtils.EMPTY);
|
||||||
|
discriminator.setPropertyName("model_type");
|
||||||
|
parentModel.setDiscriminator(discriminator);
|
||||||
|
|
||||||
|
final ComposedSchema composedSchema = new ComposedSchema()
|
||||||
|
.addAllOfItem(new Schema().$ref(parentModel.getName()));
|
||||||
|
composedSchema.setName("sample");
|
||||||
|
|
||||||
|
final ConfluenceWikiCodegen codegen = new ConfluenceWikiCodegen();
|
||||||
|
OpenAPI openAPI = TestUtils.createOpenAPI();
|
||||||
|
openAPI.setComponents(new Components()
|
||||||
|
.addSchemas(parentModel.getName(), parentModel)
|
||||||
|
.addSchemas(composedSchema.getName(), composedSchema)
|
||||||
|
);
|
||||||
|
|
||||||
|
codegen.setOpenAPI(openAPI);
|
||||||
|
final CodegenModel cm = codegen.fromModel("sample", composedSchema);
|
||||||
|
|
||||||
|
Assert.assertEquals(cm.name, "sample");
|
||||||
|
Assert.assertEquals(cm.classname, "Sample");
|
||||||
|
Assert.assertEquals(cm.parent, "ParentModel");
|
||||||
|
Assert.assertTrue(cm.imports.contains("ParentModel"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEnumTestSchema() {
|
||||||
|
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml");
|
||||||
|
ConfluenceWikiCodegen codegen = new ConfluenceWikiCodegen();
|
||||||
|
codegen.setOpenAPI(openAPI);
|
||||||
|
|
||||||
|
Schema enumTest = openAPI.getComponents().getSchemas().get("Enum_Test");
|
||||||
|
Assert.assertNotNull(enumTest);
|
||||||
|
CodegenModel cm = codegen.fromModel("Enum_Test", enumTest);
|
||||||
|
|
||||||
|
Assert.assertEquals(cm.getVars().size(), 8);
|
||||||
|
CodegenProperty cp0 = cm.getVars().get(0);
|
||||||
|
Assert.assertEquals(cp0.dataType, "String");
|
||||||
|
CodegenProperty cp1 = cm.getVars().get(1);
|
||||||
|
Assert.assertEquals(cp1.dataType, "String");
|
||||||
|
CodegenProperty cp2 = cm.getVars().get(2);
|
||||||
|
Assert.assertEquals(cp2.dataType, "Integer");
|
||||||
|
CodegenProperty cp3 = cm.getVars().get(3);
|
||||||
|
Assert.assertEquals(cp3.dataType, "Double");
|
||||||
|
CodegenProperty cp4 = cm.getVars().get(4);
|
||||||
|
Assert.assertEquals(cp4.dataType, "OuterEnum");
|
||||||
|
CodegenProperty cp5 = cm.getVars().get(5);
|
||||||
|
Assert.assertEquals(cp5.dataType, "OuterEnumInteger");
|
||||||
|
CodegenProperty cp6 = cm.getVars().get(6);
|
||||||
|
Assert.assertEquals(cp6.dataType, "OuterEnumDefaultValue");
|
||||||
|
CodegenProperty cp7 = cm.getVars().get(7);
|
||||||
|
Assert.assertEquals(cp7.dataType, "OuterEnumIntegerDefaultValue");
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user