Repository: python-poetry/poetry

Issue:
1.0.0b5 removes my private source from the lockfile
<!--
  Hi there! Thank you for discovering and submitting an issue.

  Before you submit this; let's make sure of a few things.
  Please make sure the following boxes are ticked if they are correct.
  If not, please try and fulfill these first.
-->

<!-- Checked checkbox should look like this: [x] -->
- [x] I am on the [latest](https://github.com/sdispater/poetry/releases/latest) Poetry version.
- [x] I have searched the [issues](https://github.com/sdispater/poetry/issues) of this repo and believe that this is not a duplicate.
- [x] If an exception occurs when executing a command, I executed it again in debug mode (`-vvv` option).

<!--
  Once those are done, if you're able to fill in the following list with your information,
  it'd be very helpful to whoever handles the issue.
-->

- **OS version and name**: macOS 10.14.6 (18G103)
- **Poetry version**: <!-- 1.0.0b5
- **Link of a [Gist](https://gist.github.com/) with the contents of your pyproject.toml file**: <!-- Gist Link Here --> https://gist.github.com/JBKahn/4bbb040c0181d974e0b71407ad764b6c


## Issue
When moving from `1.0.0b4` to `1.0.0b5` `poetry lock` is removing the 
```
[package.source]
reference = "private"
type = "legacy"
url = "https://private.url"
```



Discussion:
An example of such a package:
```python
Pdb) local_repo._packages[24].package.__dict__
{'_pretty_name': 'python-wave-common', '_name': 'python-wave-common', '_version': <Version 1.1.0>, '_pretty_version': '1.1.0', 'description': 'Your project description goes here', '_authors': [], '_maintainers': [], 'homepage': None, 'repository_url': None, 'documentation_url': None, 'keywords': [], '_license': None, 'readme': None, 'source_name': '', 'source_type': '', 'source_reference': 'private', 'source_url': 'https://private.url', 'requires': [<Dependency six (>=1.10.0)>], 'dev_requires': [], 'extras': {'test': [<Dependency pytest (3.1.3)>, <Dependency tox (2.7.0)>]}, 'requires_extras': [], 'category': 'main', 'files': [{'file': 'python_wave_common-1.1.0.tar.gz', 'hash': 'md5:some_hash'}], 'optional': False, 'classifiers': [], '_python_versions': '*', '_python_constraint': <VersionRange (*)>, '_python_marker': <AnyMarker>, 'platform': None, 'marker': <AnyMarker>, 'root_dir': None, 'develop': True}
```

This is another one that has the same issue. `source_type` isn't set which I think is why it isn't being sent.
[This](https://github.com/sdispater/poetry/compare/1.0.0b4...1.0.0b5#diff-82b23d5737d621bcdd9009dfa78a88f2L317) line seems to the one that "broke" that for us in this PR: https://github.com/sdispater/poetry/pull/1563
Some downstream checks seem to require that `source_type` is set, unsure how to set it manually anywhere. @sdispater didn't fully understand the change in your previous PR.
I've just had a developer waste a day trying to work out why their Docker build wasn't working. Turns out it was this bug.
We are experiencing a similar problem. We have a package with a custom pypi source (`custom_pkg = {version = "5.0.0", source = "custom_pypi"`).
When using `poetry lock` everything is updated as expected, but when updating a single package (e.g. ` poetry update --lock another_pkg`) the custom_pypi entry is removed from the lockfile and `custom_pkg` becomes `{file = "…..tar.gz", hash = "sha256:…"}` in the lockfile.

Tested this with poetry versions `1.0.0b4` and `1.0.0b7`.

Resolving patch:
diff --git a/poetry/packages/locker.py b/poetry/packages/locker.py
--- a/poetry/packages/locker.py
+++ b/poetry/packages/locker.py
@@ -139,7 +139,7 @@ def locked_repository(
                 package.add_dependency(dep_name, constraint)
 
             if "source" in info:
-                package.source_type = info["source"]["type"]
+                package.source_type = info["source"].get("type", "")
                 package.source_url = info["source"]["url"]
                 package.source_reference = info["source"]["reference"]
 
@@ -294,11 +294,12 @@ def _dump_package(self, package):  # type: (poetry.packages.Package) -> dict
 
             data["dependencies"] = dependencies
 
-        if package.source_type:
+        if package.source_url:
             data["source"] = {
-                "type": package.source_type,
                 "url": package.source_url,
                 "reference": package.source_reference,
             }
+            if package.source_type:
+                data["source"]["type"] = package.source_type
 
         return data


Test patch:
diff --git a/tests/packages/test_locker.py b/tests/packages/test_locker.py
--- a/tests/packages/test_locker.py
+++ b/tests/packages/test_locker.py
@@ -212,3 +212,37 @@ def test_reading_lock_file_should_raise_an_error_on_invalid_data(locker):
         _ = locker.lock_data
 
     assert "Unable to read the lock file" in str(e.value)
+
+
+def test_locking_legacy_repository_package_should_include_source_section(root, locker):
+    package_a = get_package("A", "1.0.0")
+    package_a.source_url = "https://foo.bar"
+    package_a.source_reference = "legacy"
+    packages = [package_a]
+
+    locker.set_lock_data(root, packages)
+
+    with locker.lock.open(encoding="utf-8") as f:
+        content = f.read()
+
+    expected = """[[package]]
+category = "main"
+description = ""
+name = "A"
+optional = false
+python-versions = "*"
+version = "1.0.0"
+
+[package.source]
+reference = "legacy"
+url = "https://foo.bar"
+
+[metadata]
+content-hash = "115cf985d932e9bf5f540555bbdd75decbb62cac81e399375fc19f6277f8c1d8"
+python-versions = "*"
+
+[metadata.files]
+A = []
+"""
+
+    assert expected == content