-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Binary property support #171
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Copyright © 2010-2013 Nokia | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.jsonschema2pojo.rules; | ||
|
||
import org.jsonschema2pojo.Schema; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.sun.codemodel.JClassContainer; | ||
import com.sun.codemodel.JType; | ||
|
||
/** | ||
* <p> | ||
* Applies the "media" hyper schema rule. | ||
* </p> | ||
* | ||
* @see <a href="http://json-schema.org/latest/json-schema-hypermedia.html#rfc.section.4.3"> | ||
* Section 4.3 media, JSON Hyper-Schema: Hypertext definitions for JSON Schema</a> | ||
* | ||
* @author Christian Trimble | ||
* @since 0.4.2 | ||
*/ | ||
public class MediaRule implements Rule<JType, JType> { | ||
|
||
public static final String BINARY_ENCODING = "binaryEncoding"; | ||
|
||
protected RuleFactory ruleFactory; | ||
|
||
/** | ||
* <p> | ||
* Constructs a new media rule. | ||
* </p> | ||
* | ||
* @param ruleFactory the rule factory that created this rule. | ||
* @since 0.4.2 | ||
*/ | ||
protected MediaRule(RuleFactory ruleFactory) { | ||
this.ruleFactory = ruleFactory; | ||
} | ||
|
||
/** | ||
* <p> | ||
* Applies this schema rule. | ||
* </p> | ||
* | ||
* @param nodeName | ||
* the name of the property. | ||
* @param mediaNode | ||
* the media node | ||
* @param baseType | ||
* the type with the media node. This must be java.lang.String. | ||
* @param schema | ||
* the schema containing the property. | ||
* @return byte[] when a binary encoding is specified, baseType otherwise. | ||
* @since 0.4.2 | ||
*/ | ||
@Override | ||
public JType apply(String nodeName, JsonNode mediaNode, JType baseType, Schema schema) { | ||
if (!mediaNode.has(BINARY_ENCODING)) { | ||
return baseType; | ||
} | ||
|
||
return baseType.owner().ref(byte[].class); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,6 +104,8 @@ public JType apply(String nodeName, JsonNode node, JClassContainer jClassContain | |
|
||
if (node.has("format")) { | ||
type = ruleFactory.getFormatRule().apply(nodeName, node.get("format"), type, schema); | ||
} else if(propertyTypeName.equals("string") && node.has("media")) { | ||
type = ruleFactory.getMediaRule().apply(nodeName, node.get("media"), type, schema); | ||
} | ||
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. @joelittlejohn does this seem like the correct way for 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. Yes, I think so. It'll minimize backwards-compatibility problems in the obscure case that someone is using both format and media. I think you could make an argument for either to win out in this case but I think you've taken the right path. |
||
|
||
return type; | ||
|
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.
This case has been narrowed, to only pass through items without
binaryEncoding
.