Java Code Examples for io.swagger.util.Json#mapper()

The following examples show how to use io.swagger.util.Json#mapper() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpringMvcTest.java    From swagger-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void testGeneratedDocWithJsonExampleValues() throws Exception {

    List<ApiSource> apisources = (List<ApiSource>) getVariableValueFromObject(mojo, "apiSources");
    ApiSource apiSource = apisources.get(0);
    // Force serialization of example values as json raw values
    apiSource.setJsonExampleValues(true);
    // exclude part of the model when not compliant with jev option (e.g. example expressed as plain string)
    apiSource.setApiModelPropertyExclusions(Collections.singletonList("exclude-when-jev-option-set"));

    mojo.execute();

    // check generated swagger json file
    ObjectMapper mapper = Json.mapper();
    JsonNode actualJson = mapper.readTree(new File(swaggerOutputDir, "swagger.json"));
    JsonNode expectJson = mapper.readTree(this.getClass().getResourceAsStream("/options/jsonExampleValues/expected/swagger-spring.json"));

    JsonNode actualUserNode = actualJson.path("definitions").path("User");
    JsonNode expectUserNode = expectJson.path("definitions").path("User");

    assertJsonEquals(expectUserNode, actualUserNode, Configuration.empty().when(IGNORING_ARRAY_ORDER));
}
 
Example 2
Source File: SpringMvcTest.java    From swagger-maven-plugin with Apache License 2.0 6 votes vote down vote up
private void assertGeneratedSwaggerSpecYaml(String description) throws MojoExecutionException, MojoFailureException, IOException {
    mojo.getApiSources().get(0).setOutputFormats("yaml");
    mojo.execute();

    DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
    options.setPrettyFlow(true);
    Yaml yaml = new Yaml(options);
    String actualYaml = yaml.dump(yaml.load(FileUtils.readFileToString(new File(swaggerOutputDir, "swagger.yaml"))));
    String expectYaml = yaml.dump(yaml.load(this.getClass().getResourceAsStream("/expectedOutput/swagger-spring.yaml")));

    ObjectMapper mapper = Json.mapper();
    JsonNode actualJson = mapper.readTree(YamlToJson(actualYaml));
    JsonNode expectJson = mapper.readTree(YamlToJson(expectYaml));

    changeDescription(expectJson, description);
    assertJsonEquals(expectJson, actualJson, Configuration.empty().when(IGNORING_ARRAY_ORDER));
}
 
Example 3
Source File: JaxrsReaderTest.java    From swagger-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void createCommonParameters() throws Exception {
    reader = new JaxrsReader(new Swagger(), Mockito.mock(Log.class));
    Swagger result = reader.read(CommonParametersApi.class);
    Parameter headerParam = result.getParameter("headerParam");
    assertTrue(headerParam instanceof HeaderParameter);
    Parameter queryParam = result.getParameter("queryParam");
    assertTrue(queryParam instanceof QueryParameter);

    result = reader.read(ReferenceCommonParametersApi.class);
    Operation get = result.getPath("/apath").getGet();
    List<Parameter> parameters = get.getParameters();
    for (Parameter parameter : parameters) {
        assertTrue(parameter instanceof RefParameter);
    }

    ObjectMapper mapper = Json.mapper();
    ObjectWriter jsonWriter = mapper.writer(new DefaultPrettyPrinter());
    String json = jsonWriter.writeValueAsString(result);
    JsonNode expectJson = mapper.readTree(this.getClass().getResourceAsStream("/expectedOutput/swagger-common-parameters.json"));
    JsonAssert.assertJsonEquals(expectJson, json);
}
 
Example 4
Source File: TestGenerate.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private void doTestGenerateModel() throws NoSuchMethodException {
    Method someMethod = TestGenerate.class.getMethod("someMethod", SomeParameter.class);
    Type type = someMethod.getGenericParameterTypes()[0];

    ModelResolver converter = new ModelResolver(Json.mapper());
    ModelConverterContextImpl context = new ModelConverterContextImpl(converter);
    Property property = context.resolveProperty(type, null);
    Assert.assertTrue(property instanceof RefProperty);
    Assert.assertEquals("#/definitions/SomeParameter", ((RefProperty) property).get$ref());
}
 
Example 5
Source File: SwaggerService.java    From proteus with Apache License 2.0 5 votes vote down vote up
public SwaggerService()
{
	mapper = Json.mapper();

	writer = mapper.writerWithDefaultPrettyPrinter();
	writer = writer.without(SerializationFeature.WRITE_NULL_MAP_VALUES);
	yamlMapper = Yaml.mapper();
}
 
Example 6
Source File: EnumModuleExtTest.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void testEnumModule() throws JsonProcessingException {
  ObjectMapper mapper = Json.mapper();
  mapper.registerModule(new SimpleModule());

  String serializeValue = mapper.writeValueAsString(TestEnum.AB);
  assertEquals("\"AB\"", serializeValue);
  serializeValue = mapper.writeValueAsString(TestEnum.C_D);
  assertEquals("\"C-D\"", serializeValue);
  serializeValue = mapper.writeValueAsString(TestEnum.E_F);
  assertEquals("\"E.F\"", serializeValue);
  serializeValue = mapper.writeValueAsString(TestEnum.HI);
  assertEquals("\"HI\"", serializeValue);
}
 
Example 7
Source File: SpringMvcSkipInheritedTest.java    From swagger-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testAssertGeneratedSwaggerSpecJson() throws MojoExecutionException, MojoFailureException, IOException {
    mojo.execute();
    ObjectMapper mapper = Json.mapper();
    JsonNode actualJson = mapper.readTree(new File(swaggerOutputDir, "swagger.json"));
    JsonNode expectJson = mapper.readTree(this.getClass().getResourceAsStream("/expectedOutput/swagger-spring-skip-inherited.json"));

    assertJsonEquals(expectJson, actualJson, Configuration.empty().when(IGNORING_ARRAY_ORDER));
}
 
Example 8
Source File: SpringMvcTest.java    From swagger-maven-plugin with Apache License 2.0 5 votes vote down vote up
private void assertGeneratedSwaggerSpecJson(String description) throws MojoExecutionException, MojoFailureException, IOException {
    mojo.execute();
    ObjectMapper mapper = Json.mapper();
    JsonNode actualJson = mapper.readTree(new File(swaggerOutputDir, "swagger.json"));
    JsonNode expectJson = mapper.readTree(this.getClass().getResourceAsStream("/expectedOutput/swagger-spring.json"));

    changeDescription(expectJson, description);
    assertJsonEquals(expectJson, actualJson, Configuration.empty().when(IGNORING_ARRAY_ORDER));
}
 
Example 9
Source File: StringWrapperModelTest.java    From swagger-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testAssertGeneratedSwaggerSpecJson() throws MojoExecutionException, MojoFailureException, IOException {
    mojo.execute();
    ObjectMapper mapper = Json.mapper();
    JsonNode actualJson = mapper.readTree(new File(swaggerOutputDir, "swagger.json"));
    JsonNode expectJson = mapper.readTree(this.getClass().getResourceAsStream("/expectedOutput/swagger-spring-string-wrapper-model.json"));

    assertJsonEquals(expectJson, actualJson, Configuration.empty().when(IGNORING_ARRAY_ORDER));
}
 
Example 10
Source File: SpringMvcEnhancedOperationIdTest.java    From swagger-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testAssertGeneratedSwaggerSpecJson() throws MojoExecutionException, MojoFailureException, IOException {
    mojo.execute();
    ObjectMapper mapper = Json.mapper();
    JsonNode actualJson = mapper.readTree(new File(swaggerOutputDir, "swagger.json"));
    JsonNode expectJson = mapper.readTree(this.getClass().getResourceAsStream("/expectedOutput/swagger-spring-enhanced-operation-id.json"));

    assertJsonEquals(expectJson, actualJson, Configuration.empty().when(IGNORING_ARRAY_ORDER));
}
 
Example 11
Source File: DefaultModelResolveObjectMapperProvider.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Override
public ObjectMapper getMapper() {
  ObjectMapper mapper = Json.mapper();
  mapper.registerModule(new EnumModuleExt());
  return mapper;
}
 
Example 12
Source File: AbstractDocumentSource.java    From swagger-maven-plugin with Apache License 2.0 4 votes vote down vote up
public void loadModelModifier() throws GenerateException, IOException {
    ObjectMapper objectMapper = Json.mapper();
    if (apiSource.isUseJAXBAnnotationProcessor()) {
        JaxbAnnotationModule jaxbAnnotationModule = new JaxbAnnotationModule();
        if (apiSource.isUseJAXBAnnotationProcessorAsPrimary()) {
            jaxbAnnotationModule.setPriority(Priority.PRIMARY);
        } else {
            jaxbAnnotationModule.setPriority(Priority.SECONDARY);
        }
        objectMapper.registerModule(jaxbAnnotationModule);

        // to support @ApiModel on class level.
        // must be registered only if we use JaxbAnnotationModule before. Why?
        objectMapper.registerModule(new EnhancedSwaggerModule());
    }
    ModelModifier modelModifier = new ModelModifier(objectMapper);

    List<String> apiModelPropertyAccessExclusions = apiSource.getApiModelPropertyAccessExclusions();
    if (apiModelPropertyAccessExclusions != null && !apiModelPropertyAccessExclusions.isEmpty()) {
        modelModifier.setApiModelPropertyAccessExclusions(apiModelPropertyAccessExclusions);
    }

    if (modelSubstitute != null) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(this.modelSubstitute)));
            String line = reader.readLine();
            while (line != null) {
                String[] classes = line.split(":");
                if (classes.length != 2) {
                    throw new GenerateException("Bad format of override model file, it should be ${actualClassName}:${expectClassName}");
                }
                modelModifier.addModelSubstitute(classes[0].trim(), classes[1].trim());
                line = reader.readLine();
            }
        } catch (IOException e) {
            throw new GenerateException(e);
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }

    ModelConverters.getInstance().addConverter(modelModifier);
}