-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
[Spring][server] oneOf Interface for Spring Boot #10392
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
Changes from all commits
6a8769e
049359c
49f13eb
1a5a484
66a09d2
48a1a4a
a811330
299cdb3
461cbc8
dd0118d
26a95f6
9af7db6
fbfd57d
bfab304
29003d8
d864831
6d54799
c6a5c6b
d96cd2e
8f09f52
0d31df4
397e711
a922217
21a98e5
1b5964e
05105fa
c89a28b
4808e87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1319,6 +1319,16 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) { | |
| } | ||
| } | ||
|
|
||
| // add implements for serializable/parcelable to all models | ||
| List<Map<String, Object>> models = (List<Map<String, Object>>) objs.get("models"); | ||
| for (Map<String, Object> mo : models) { | ||
| CodegenModel cm = (CodegenModel) mo.get("model"); | ||
| if (this.serializableModel) { | ||
| cm.getVendorExtensions().putIfAbsent("x-implements", new ArrayList<String>()); | ||
| ((ArrayList<String>) cm.getVendorExtensions().get("x-implements")).add("Serializable"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. x-implements Serializable is at least duplicated in JavaClientCodeGen. |
||
| } | ||
| } | ||
|
|
||
| return postProcessModelsEnum(objs); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -233,7 +233,7 @@ public void processOpts() { | |
| } | ||
|
|
||
| super.processOpts(); | ||
|
|
||
| useOneOfInterfaces = true; | ||
| // clear model and api doc template as this codegen | ||
| // does not support auto-generated markdown doc at the moment | ||
| //TODO: add doc templates | ||
|
|
@@ -880,4 +880,37 @@ public void setPerformBeanValidation(boolean performBeanValidation) { | |
| public void setUseOptional(boolean useOptional) { | ||
| this.useOptional = useOptional; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void postProcessParameter(CodegenParameter p) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the meantime, postProcessParameter() moved to AbstractJavaCodeGen with [Java][Jaxrs-Resteasy] Fixes generator devaultValues for int64/float/… (#8988) |
||
| // we use a custom version of this function to remove the l, d, and f suffixes from Long/Double/Float | ||
| // defaultValues | ||
| // remove the l because our users will use Long.parseLong(String defaultValue) | ||
| // remove the d because our users will use Double.parseDouble(String defaultValue) | ||
| // remove the f because our users will use Float.parseFloat(String defaultValue) | ||
| // NOTE: for CodegenParameters we DO need these suffixes because those defaultValues are used as java value | ||
| // literals assigned to Long/Double/Float | ||
| if (p.defaultValue == null) { | ||
| return; | ||
| } | ||
| Boolean fixLong = (p.isLong && "l".equals(p.defaultValue.substring(p.defaultValue.length()-1))); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
| Boolean fixDouble = (p.isDouble && "d".equals(p.defaultValue.substring(p.defaultValue.length()-1))); | ||
| Boolean fixFloat = (p.isFloat && "f".equals(p.defaultValue.substring(p.defaultValue.length()-1))); | ||
| if (fixLong || fixDouble || fixFloat) { | ||
| p.defaultValue = p.defaultValue.substring(0, p.defaultValue.length()-1); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void addImportsToOneOfInterface(List<Map<String, String>> imports) { | ||
| for (String i : Arrays.asList("JsonSubTypes", "JsonTypeInfo")) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the review. Can you please submit a PR against this branch with the suggested fix? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| Map<String, String> oneImport = new HashMap<String, String>() {{ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's faster and more readable. using
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
A lot of people would complain on this too |
||
| put("import", importMapping.get(i)); | ||
| }}; | ||
| if (!imports.contains(oneImport)) { | ||
| imports.add(oneImport); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| {{>additionalModelTypeAnnotations}}{{>generatedAnnotation}}{{>typeInfoAnnotation}}{{>xmlAnnotation}} | ||
| public interface {{classname}} {{#vendorExtensions.x-implements}}{{#-first}}extends {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{/vendorExtensions.x-implements}} { | ||
| {{#discriminator}} | ||
| public {{propertyType}} {{propertyGetter}}(); | ||
| {{/discriminator}} | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| openapi: 3.0.1 | ||
| info: | ||
| title: ByRefOrValue | ||
| description: > | ||
| This tests for a oneOf interface representation | ||
| version: 0.0.1 | ||
| servers: | ||
| - url: "http://localhost:8080" | ||
| tags: | ||
| - name: Foo | ||
| paths: | ||
| /foo: | ||
| get: | ||
| tags: | ||
| - Foo | ||
| summary: GET all Foos | ||
| operationId: getAllFoos | ||
| responses: | ||
| '200': | ||
| $ref: '#/components/responses/200FooArray' | ||
| post: | ||
| tags: | ||
| - Foo | ||
| summary: Create a Foo | ||
| operationId: createFoo | ||
| requestBody: | ||
| $ref: '#/components/requestBodies/Foo' | ||
| responses: | ||
| '201': | ||
| $ref: '#/components/responses/201Foo' | ||
|
|
||
| components: | ||
| schemas: | ||
| Entity: | ||
| type: object | ||
| allOf: | ||
| - "$ref": "#/components/schemas/Addressable" | ||
| - "$ref": "#/components/schemas/Extensible" | ||
|
|
||
| EntityRef: | ||
| description: Entity reference schema to be use for all entityRef class. | ||
| type: object | ||
| properties: | ||
| name: | ||
| type: string | ||
| description: Name of the related entity. | ||
| '@referredType': | ||
| type: string | ||
| description: The actual type of the target instance when needed for disambiguation. | ||
| allOf: | ||
| - $ref: '#/components/schemas/Addressable' | ||
| - "$ref": "#/components/schemas/Extensible" | ||
|
|
||
|
|
||
| Addressable: | ||
| type: object | ||
| properties: | ||
| href: | ||
| type: string | ||
| description: Hyperlink reference | ||
| id: | ||
| type: string | ||
| description: unique identifier | ||
| description: Base schema for adressable entities | ||
| Extensible: | ||
| type: object | ||
| properties: | ||
| "@schemaLocation": | ||
| type: string | ||
| description: A URI to a JSON-Schema file that defines additional attributes | ||
| and relationships | ||
| "@baseType": | ||
| type: string | ||
| description: When sub-classing, this defines the super-class | ||
| "@type": | ||
| type: string | ||
| description: When sub-classing, this defines the sub-class Extensible name | ||
| required: | ||
| - '@type' | ||
|
|
||
| FooRefOrValue: | ||
| type: object | ||
| oneOf: | ||
| - $ref: "#/components/schemas/Foo" | ||
| - $ref: "#/components/schemas/FooRef" | ||
| discriminator: | ||
| propertyName: "@type" | ||
| mapping: | ||
| Foo: "#/components/schemas/Foo" | ||
| FooRef: "#/components/schemas/FooRef" | ||
|
|
||
| Foo: | ||
| type: object | ||
| properties: | ||
| fooPropA: | ||
| type: string | ||
| fooPropB: | ||
| type: string | ||
| allOf: | ||
| - $ref: '#/components/schemas/Entity' | ||
|
|
||
| FooRef: | ||
| type: object | ||
| properties: | ||
| foorefPropA: | ||
| type: string | ||
| allOf: | ||
| - $ref: '#/components/schemas/EntityRef' | ||
|
|
||
| requestBodies: | ||
| Foo: | ||
| description: The Foo to be created | ||
| content: | ||
| application/json;charset=utf-8: | ||
| schema: | ||
| $ref: '#/components/schemas/Foo' | ||
| responses: | ||
| '204': | ||
| description: Deleted | ||
| content: { } | ||
| 201Foo: | ||
| description: Error | ||
| content: | ||
| application/json: | ||
| schema: | ||
| $ref: '#/components/schemas/FooRefOrValue' | ||
| 200FooArray: | ||
| description: Success | ||
| content: | ||
| application/json;charset=utf-8: | ||
| schema: | ||
| type: array | ||
| items: | ||
| $ref: '#/components/schemas/FooRefOrValue' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
new ArrayList<String>(1)as you know the size upfront