Skip to content

New Dynamic Model pattern to support type-checked additional properties #23

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

Merged
merged 2 commits into from
Apr 16, 2019

Conversation

padamstx
Copy link
Member

Why:
The SDK code generator is being updated to improve the pattern used for
dynamic models (i.e. models that support "additionalProperties").

What:
The changes in this PR can be summarized as follows:

  1. An enhanced DynamicModel class which is now parameterized with the
    additionalProperties value type and includes a map for them
  2. A new DynamicModelTypeAdapterFactory class which is registered with Gson
    to perform serialization and deserialization of ALL generated DynamicModel
    subclasses.
  3. GsonSerializationHelper is now deprecated as it's no longer needed.
  4. Tests related to the above functional changes.

Why:
The SDK code generator is being updated to improve the pattern used for
dynamic models (i.e. models that support "additionalProperties").

What:
The changes in this PR can be summarized as follows:
1) An enhanced DynamicModel class which is now parameterized with the
additionalProperties value type and includes a map for them
2) A new DynamicModelTypeAdapterFactory class which is registered with Gson
to perform serialization and deserialization of ALL generated DynamicModel
subclasses.
3) GsonSerializationHelper is now deprecated as it's no longer needed.
4) Tests related to the above functional changes.
@padamstx padamstx force-pushed the dynmodel-renovation branch from e0747dd to 4116361 Compare April 13, 2019 19:39
@padamstx padamstx requested review from mkistler and lpatino10 April 13, 2019 19:40
@padamstx
Copy link
Member Author

Note that this PR will need to be managed together with a corresponding PR in the openapi-sdkgen project (which will be delivered "real soon now" :) ). The codegen PR will change the way we generate dynamic models so they look like the ModelAPXXX classes that are included in this PR.

@padamstx
Copy link
Member Author

Note: this PR will need to be approved and merged into the master branch, then a new tagged release will need to be created before the corresponding openapi-sdkgen changes can be submitted in a PR. This is because the code generator has a build-time dependency on the java core (we have unit tests that verify that certain generated models will compile cleanly) and the codegen PR will need to include a change to its pom.xml that points to the new release of the java core package that includes these DynamicModel changes.

@padamstx padamstx self-assigned this Apr 14, 2019
@padamstx padamstx added the ready-for-review PR is ready for a review label Apr 14, 2019
Copy link
Contributor

@mkistler mkistler left a comment

Choose a reason for hiding this comment

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

Looks good, but I left a few questions for you.


/**
* Returns the TypeToken which describes the type of additional properties stored in the map.
* @return The TypeToken which descriptes the map value type
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo: "descriptes"

Copy link
Member Author

Choose a reason for hiding this comment

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

done

/**
* An adapter for serializing/deserializing instances of type T, where T represents a generated dynamic model class
* which is a subclass of DynamicModel. Subclasses of DynamicModel will have zero or more explicitly-defined fields
* plus an inherited map to store addtional (arbitrary) properties.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think "inherited" is not accurate -- the map is a proper member variable if I understand the code.

Typo: "addtional"

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, technically the generated model class does inherit the map... from DynamicModel :)
But I changed the wording slightly.

* the name of the property to set
* @param value
* the value of the property to be set
* @return the previous value of the property, or null if the property was not previously set
Copy link
Contributor

Choose a reason for hiding this comment

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

The convention for all other setters in model classes is to return void. Is there good reason to break with that convention here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, the reason I followed the "Map.put" model is so that I could easily tell if we have a duplicate property while deserializing a JSON string (by checking to see if the result of the set operation is non-null). Technically, I don't need to do it this way... i could first check to see if a value already exists for a given key, but the Map "put" method (which is really just wrapped by DynamicModel.setProperty) does that all in one step so thought it would be better to just follow that same pattern with the setProperty method.

@@ -0,0 +1,85 @@
/*
* Copyright (C) 2011 Google Inc.
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want an IBM Copyright on top of Googles?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, will add that

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll also add the Google copyright prolog to the DynamicModelTypeAdapterFactory class since some of that code was modeled after a couple of the Gson internal type adapters.

@padamstx
Copy link
Member Author

padamstx commented Apr 15, 2019

I pushed another commit to address the review comments

@padamstx padamstx requested a review from mkistler April 15, 2019 16:05
@padamstx
Copy link
Member Author

cc: @germanattanasio

Copy link
Contributor

@mkistler mkistler left a comment

Choose a reason for hiding this comment

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

Looks good! 👍

@padamstx
Copy link
Member Author

cc: @ricellis
Tagging Richard for awareness...

Copy link
Contributor

@lpatino10 lpatino10 left a comment

Choose a reason for hiding this comment

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

Looks REALLY good to me and I always love your tests. They're super thorough.

Thanks for this!

@padamstx padamstx merged commit e8a2aa8 into master Apr 16, 2019
@padamstx padamstx deleted the dynmodel-renovation branch June 16, 2019 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-for-review PR is ready for a review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants