public AbstractDocumentSource(Log log, ApiSource apiSource) throws MojoFailureException { LOG = log; this.outputPath = apiSource.getOutputPath(); this.templatePath = apiSource.getTemplatePath(); this.swaggerPath = apiSource.getSwaggerDirectory(); this.modelSubstitute = apiSource.getModelSubstitute(); this.jsonExampleValues = apiSource.isJsonExampleValues(); swagger = new Swagger(); if (apiSource.getSchemes() != null) { for (String scheme : apiSource.getSchemes()) { swagger.scheme(Scheme.forValue(scheme)); } } // read description from file if (apiSource.getDescriptionFile() != null) { try { InputStream is = new FileInputStream(apiSource.getDescriptionFile()); apiSource.getInfo().setDescription(IOUtils.toString(is)); is.close(); } catch (IOException e) { throw new MojoFailureException(e.getMessage(), e); } } swagger.setHost(apiSource.getHost()); swagger.setInfo(apiSource.getInfo()); swagger.setBasePath(apiSource.getBasePath()); swagger.setExternalDocs(apiSource.getExternalDocs()); this.apiSource = apiSource; }
@Override public Swagger configure(Swagger swagger) { if (schemes != null) { for (String scheme : schemes) { swagger.scheme(Scheme.forValue(scheme)); } } if (filterClass != null) { try { SwaggerSpecFilter filter = (SwaggerSpecFilter) Class.forName(filterClass).newInstance(); if (filter != null) { FilterFactory.setFilter(filter); } } catch (Exception e) { LOGGER.error("failed to load filter", e); } } return swagger.info(info) .host(host) .basePath(basePath); } }
private void configureServerURL(Swagger swagger) { SplitURL split = URLUtils.splitURL(System.getProperty(SpringCXFProperties.SERVER_URL, "localhost")); swagger.scheme(Scheme.forValue(split.getScheme())); swagger.host(split.getHost() + ":" + split.getPort()); swagger.basePath(System.getProperty(SpringCXFProperties.JAXRS_PATH)); }
@Test public void shouldDetermineHostFromSpecification() { assertThat(determineHost(new Swagger().host("api.example.com").scheme(Scheme.HTTPS))).isEqualTo("https://api.example.com"); assertThat(determineHost(new Swagger().host("api.example.com").scheme(Scheme.HTTP).scheme(Scheme.HTTPS))) .isEqualTo("https://api.example.com"); assertThat(determineHost(new Swagger().host("api.example.com").scheme(Scheme.HTTP))).isEqualTo("http://api.example.com"); }
@Test public void shouldDetermineHostFromSpecification() { assertThat(determineHost(new Swagger().host("api.example.com").scheme(Scheme.HTTPS))).isEqualTo("https://api.example.com"); assertThat(determineHost(new Swagger().host("api.example.com").scheme(Scheme.HTTP).scheme(Scheme.HTTPS))) .isEqualTo("https://api.example.com"); assertThat(determineHost(new Swagger().host("api.example.com").scheme(Scheme.HTTP))).isEqualTo("http://api.example.com"); }
@Test public void shouldDetermineHostFromSpecification() { assertThat(determineHost(new Swagger().host("api.example.com").scheme(Scheme.HTTPS))).isEqualTo("https://api.example.com"); assertThat(determineHost(new Swagger().host("api.example.com").scheme(Scheme.HTTP).scheme(Scheme.HTTPS))) .isEqualTo("https://api.example.com"); assertThat(determineHost(new Swagger().host("api.example.com").scheme(Scheme.HTTP))).isEqualTo("http://api.example.com"); }
@Test public void shouldDetermineHostFromSpecificationUrl() { final URI specificationUrl = URI.create("https://api.example.com/swagger.json"); assertThat(determineHost(new Swagger().vendorExtension(BaseSwaggerConnectorGenerator.URL_EXTENSION, specificationUrl))) .isEqualTo("https://api.example.com"); assertThat( determineHost(new Swagger().vendorExtension(BaseSwaggerConnectorGenerator.URL_EXTENSION, specificationUrl).scheme(Scheme.HTTP))) .isEqualTo("http://api.example.com"); assertThat(determineHost(new Swagger().vendorExtension(BaseSwaggerConnectorGenerator.URL_EXTENSION, specificationUrl) .host("api2.example.com").scheme(Scheme.HTTP))).isEqualTo("http://api2.example.com"); }
@Test public void shouldDetermineHostFromSpecificationUrl() { final URI specificationUrl = URI.create("https://api.example.com/swagger.json"); assertThat(determineHost(new Swagger().vendorExtension(BaseSwaggerConnectorGenerator.URL_EXTENSION, specificationUrl))) .isEqualTo("https://api.example.com"); assertThat( determineHost(new Swagger().vendorExtension(BaseSwaggerConnectorGenerator.URL_EXTENSION, specificationUrl).scheme(Scheme.HTTP))) .isEqualTo("http://api.example.com"); assertThat(determineHost(new Swagger().vendorExtension(BaseSwaggerConnectorGenerator.URL_EXTENSION, specificationUrl) .host("api2.example.com").scheme(Scheme.HTTP))).isEqualTo("http://api2.example.com"); }
@Test public void shouldDetermineFromHostsContainingPorts() { assertThat(determineHost(new Swagger().host("54.152.43.92:8080").scheme(Scheme.HTTPS))).isEqualTo("https://54.152.43.92:8080"); }
@Test public void shouldDetermineHostFromSpecificationUrl() { final URI specificationUrl = URI.create("https://api.example.com/swagger.json"); assertThat(determineHost(new Swagger().vendorExtension(BaseSwaggerConnectorGenerator.URL_EXTENSION, specificationUrl))) .isEqualTo("https://api.example.com"); assertThat( determineHost(new Swagger().vendorExtension(BaseSwaggerConnectorGenerator.URL_EXTENSION, specificationUrl).scheme(Scheme.HTTP))) .isEqualTo("http://api.example.com"); assertThat(determineHost(new Swagger().vendorExtension(BaseSwaggerConnectorGenerator.URL_EXTENSION, specificationUrl) .host("api2.example.com").scheme(Scheme.HTTP))).isEqualTo("http://api2.example.com"); }
@Test public void shouldDetermineFromHostsContainingPorts() { assertThat(determineHost(new Swagger().host("54.152.43.92:8080").scheme(Scheme.HTTPS))).isEqualTo("https://54.152.43.92:8080"); }
@Test public void shouldReturnNullIfNoHostGivenAnywhere() { assertThat(determineHost(new Swagger())).isNull(); assertThat(determineHost(new Swagger().scheme(Scheme.HTTP))).isNull(); assertThat(determineHost(new Swagger().host("host"))).isNull(); }
@Test public void shouldReturnNullIfNoHostGivenAnywhere() { assertThat(determineHost(new Swagger())).isNull(); assertThat(determineHost(new Swagger().scheme(Scheme.HTTP))).isNull(); assertThat(determineHost(new Swagger().host("host"))).isNull(); }
@Override public Swagger configure(Swagger swagger) { if (swagger != null) { Info info = swagger.getInfo(); if (info == null) { swagger.info(new Info()); } swagger.basePath(basePath) .host(host) .getInfo() .title(title) .version(apiVersion); for(String scheme : this.schemes) { swagger.scheme(Scheme.forValue(scheme)); } } return swagger; } }
private void configureSwagger() { swagger = new Swagger(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); Info info = new Info(); Contact swaggerContact = new Contact(); License swaggerLicense = new License(); swaggerLicense.name(this.license) .url(this.licenseUrl); swaggerContact.name(this.contact); info.version(this.version) .description(this.description) .contact(swaggerContact) .license(swaggerLicense) .title(this.title); swagger.setInfo(info); if (this.schemes != null) { for (String scheme : this.schemes) { swagger.scheme(Scheme.forValue(scheme)); } } swagger.setHost(this.host); swagger.setBasePath(this.basePath); } }
@Test public void swaggerSerializeTest() { final Info info = new Info().version("1.0.0").title("Swagger WebServices REST"); Swagger swagger = new Swagger().info(info).securityDefinition("basicAuth", new BasicAuthDefinition()) .scheme(Scheme.HTTP).consumes("application/json").produces("application/json"); final Model patientModel = ModelConverters.getInstance().read(Patient.class).get("Patient"); swagger.addDefinition("Patient", patientModel); final String swaggerJson = Json.pretty(swagger); assertNotNull(swaggerJson); }
@Test public void shouldFailToDetermineIfNoHostGivenAnywhere() { assertThatExceptionOfType(NullPointerException.class).isThrownBy(() -> determineHost(new Swagger())) .withMessageStartingWith("Swagger specification does not provide"); assertThatExceptionOfType(NullPointerException.class).isThrownBy(() -> determineHost(new Swagger().scheme(Scheme.HTTP))) .withMessageStartingWith("Swagger specification does not provide"); assertThatExceptionOfType(NullPointerException.class).isThrownBy(() -> determineHost(new Swagger().host("host"))) .withMessageStartingWith("Swagger specification does not provide"); } }