Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for IntOrString in Java #30

Merged
merged 5 commits into from
Nov 3, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions openapi/client-generator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ kubeclient::generator::generate_client() {
-e CLEANUP_DIRS="${CLEANUP_DIRS_STRING}" \
-e KUBERNETES_BRANCH="${KUBERNETES_BRANCH}" \
-e CLIENT_VERSION="${CLIENT_VERSION}" \
-e CLIENT_LANGUAGE="${CLIENT_LANGUAGE}" \
-e PACKAGE_NAME="${PACKAGE_NAME}" \
-e SWAGGER_CODEGEN_COMMIT="${SWAGGER_CODEGEN_COMMIT}" \
-v "${output_dir}:/output_dir" \
Expand Down
3 changes: 2 additions & 1 deletion openapi/generate_client_in_container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ set -o pipefail
: "${CLEANUP_DIRS?Must set CLEANUP_DIRS env var}"
: "${KUBERNETES_BRANCH?Must set KUBERNETES_BRANCH env var}"
: "${CLIENT_VERSION?Must set CLIENT_VERSION env var}"
: "${CLIENT_LANGUAGE?Must set CLIENT_LANGUAGE env var}"
: "${PACKAGE_NAME?Must set PACKAGE_NAME env var}"
: "${SWAGGER_CODEGEN_COMMIT?Must set SWAGGER_CODEGEN_COMMIT env var}"

Expand Down Expand Up @@ -78,7 +79,7 @@ popd
mkdir -p "${output_dir}"

echo "--- Downloading and pre-processing OpenAPI spec"
python "${SCRIPT_ROOT}/preprocess_spec.py" "${KUBERNETES_BRANCH}" "${output_dir}/swagger.json"
python "${SCRIPT_ROOT}/preprocess_spec.py" "${CLIENT_LANGUAGE}" "${KUBERNETES_BRANCH}" "${output_dir}/swagger.json"

echo "--- Cleaning up previously generated folders"
for i in ${CLEANUP_DIRS}; do
Expand Down
2 changes: 2 additions & 0 deletions openapi/java.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
<dateLibrary>joda</dateLibrary>
<useRxJava>false</useRxJava>
<library>okhttp-gson</library>
<type-mappings>intstr.IntOrString=IntOrString</type-mappings>
<import-mappings>IntOrString=io.kubernetes.client.custom.IntOrString</import-mappings>
</configOptions>
<output>${generator.output.path}</output>
</configuration>
Expand Down
52 changes: 32 additions & 20 deletions openapi/preprocess_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def add_custom_objects_spec(spec):
return spec


def process_swagger(spec):
def process_swagger(spec, client_language):
spec = add_custom_objects_spec(spec)

apply_func_to_spec_operations(spec, strip_tags_from_operation_id)
Expand All @@ -127,10 +127,15 @@ def process_swagger(spec):

remove_model_prefixes(spec)

inline_primitive_models(spec)
inline_primitive_models(spec, preserved_primitives_for_language(client_language))

return spec

def preserved_primitives_for_language(client_language):
if client_language == "java":
return ["intstr.IntOrString"]
else:
return []

def rename_model(spec, old_name, new_name):
if new_name in spec['definitions']:
Expand Down Expand Up @@ -180,7 +185,7 @@ def remove_deprecated_models(spec):
models = {}
for k, v in spec['definitions'].items():
if is_model_deprecated(v):
print("Removing deprecated model %s" %k)
print("Removing deprecated model %s" % k)
else:
models[k] = v
spec['definitions'] = models
Expand Down Expand Up @@ -252,41 +257,48 @@ def find_replace_ref_recursive(root, ref_name, replace_map):
find_replace_ref_recursive(v, ref_name, replace_map)


def inline_primitive_models(spec):
def inline_primitive_models(spec, excluded_primitives):
to_remove_models = []
for k, v in spec['definitions'].items():
if "properties" not in v:
if k == "intstr.IntOrString":
v["type"] = "object"
if "type" not in v:
v["type"] = "object"
print("Making model `%s` inline as %s..." % (k, v["type"]))
find_replace_ref_recursive(spec, "#/definitions/" + k, v)
to_remove_models.append(k)
if k not in excluded_primitives:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think I'd prefer

if k in excluded_primitives:
    continue
...

if "properties" not in v:
if k == "intstr.IntOrString":
v["type"] = "object"
if "type" not in v:
v["type"] = "object"
print("Making model `%s` inline as %s..." % (k, v["type"]))
find_replace_ref_recursive(spec, "#/definitions/" + k, v)
to_remove_models.append(k)

for k in to_remove_models:
del spec['definitions'][k]

def write_json(filename, object):
with open(filename, 'w') as out:
json.dump(object, out, sort_keys=False, indent=2,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I don't really love this line breaking, can you get it all on one line?

separators=(',', ': '), ensure_ascii=True)



def main():
if len(sys.argv) != 3:
print("Usage:\n\n\tpython preprocess_spec.py kuberneres_branch " \
if len(sys.argv) != 4:
print("Usage:\n\n\tpython preprocess_spec.py client_language kuberneres_branch " \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may as well fix the 'kuberneres_branch' typo while you're in here...

"output_spec_path")
return 1
client_language = sys.argv[1]
spec_url = 'https://raw.githubusercontent.com/kubernetes/kubernetes/' \
'%s/api/openapi-spec/swagger.json' % sys.argv[1]
output_path = sys.argv[2]
'%s/api/openapi-spec/swagger.json' % sys.argv[2]
output_path = sys.argv[3]

pool = urllib3.PoolManager()
with pool.request('GET', spec_url, preload_content=False) as response:
if response.status != 200:
print("Error downloading spec file. Reason: %s" % response.reason)
return 1
in_spec = json.load(response, object_pairs_hook=OrderedDict)
out_spec = process_swagger(in_spec)
with open(output_path, 'w') as out:
json.dump(out_spec, out, sort_keys=False, indent=2,
separators=(',', ': '), ensure_ascii=True)
write_json(output_path + ".unprocessed", in_spec)
out_spec = process_swagger(in_spec, client_language)
write_json(output_path, out_spec)
return 0


Expand Down